2011-11-29 22:20:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
|
|
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
function test_static_create_should_return_new_validator()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf('Respect\Validation\Validator', Validator::create());
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_invalid_rule_class_should_throw_component_exception()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Respect\Validation\Exceptions\ComponentException');
|
|
|
|
Validator::iDoNotExistSoIShouldThrowException();
|
|
|
|
}
|
2012-03-23 02:12:07 +01:00
|
|
|
function test_set_template_with_single_validator_should_use_template_as_main_message() {
|
2012-03-23 01:54:29 +01:00
|
|
|
try {
|
|
|
|
Validator::callback('is_int')->setTemplate('{{name}} is not tasty')->assert('something');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
2012-03-23 02:12:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function test_set_template_with_multiple_validators_should_use_template_as_main_message() {
|
|
|
|
try {
|
|
|
|
Validator::callback('is_int')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
|
|
|
}
|
2012-03-23 01:54:29 +01:00
|
|
|
}
|
2012-04-08 10:30:44 +02:00
|
|
|
function test_set_template_with_multiple_validators_should_use_template_as_full_message() {
|
|
|
|
try {
|
|
|
|
Validator::callback('is_string')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertEquals('\-"something" is not tasty
|
|
|
|
\-"something" must be greater than 1', $e->getFullMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function test_getFullMessage_should_include_all_validation_messages_in_a_chain() {
|
|
|
|
try {
|
2012-04-09 03:46:30 +02:00
|
|
|
Validator::string()->length(1,15)->assert('');
|
2012-04-08 10:30:44 +02:00
|
|
|
} catch (\Exception $e) {
|
2012-04-09 03:27:25 +02:00
|
|
|
$this->assertEquals('\-These rules must pass for ""
|
2012-04-08 10:30:44 +02:00
|
|
|
\-"" must have a length between 1 and 15', $e->getFullMessage());
|
|
|
|
}
|
|
|
|
}
|
2011-11-29 22:20:07 +01:00
|
|
|
}
|