2011-11-29 22:20:07 +01:00
|
|
|
<?php
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
2015-01-30 02:53:20 +01:00
|
|
|
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
|
|
|
|
use Respect\Validation\Exceptions\ValidationExceptionInterface;
|
|
|
|
|
2011-11-29 22:20:07 +01:00
|
|
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testStaticCreateShouldReturnNewValidator()
|
2011-11-29 22:20:07 +01:00
|
|
|
{
|
|
|
|
$this->assertInstanceOf('Respect\Validation\Validator', Validator::create());
|
|
|
|
}
|
2012-05-17 20:51:49 +02:00
|
|
|
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testInvalidRuleClassShouldThrowComponentException()
|
2011-11-29 22:20:07 +01:00
|
|
|
{
|
|
|
|
$this->setExpectedException('Respect\Validation\Exceptions\ComponentException');
|
|
|
|
Validator::iDoNotExistSoIShouldThrowException();
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testSetTemplateWithSingleValidatorShouldUseTemplateAsMainMessage()
|
|
|
|
{
|
2012-03-23 01:54:29 +01:00
|
|
|
try {
|
|
|
|
Validator::callback('is_int')->setTemplate('{{name}} is not tasty')->assert('something');
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
2012-03-23 01:54:29 +01:00
|
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
2012-03-23 02:12:07 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testSetTemplateWithMultipleValidatorsShouldUseTemplateAsMainMessage()
|
|
|
|
{
|
2012-03-23 02:12:07 +01:00
|
|
|
try {
|
|
|
|
Validator::callback('is_int')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
2012-03-23 02:12:07 +01:00
|
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
|
|
|
}
|
2012-03-23 01:54:29 +01:00
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testSetTemplateWithMultipleValidatorsShouldUseTemplateAsFullMessage()
|
|
|
|
{
|
2012-04-08 10:30:44 +02:00
|
|
|
try {
|
|
|
|
Validator::callback('is_string')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
2012-04-08 10:30:44 +02:00
|
|
|
$this->assertEquals('\-"something" is not tasty
|
|
|
|
\-"something" must be greater than 1', $e->getFullMessage());
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testGetFullMessageShouldIncludeAllValidationMessagesInAChain()
|
|
|
|
{
|
2012-04-08 10:30:44 +02:00
|
|
|
try {
|
2012-04-09 03:46:30 +02:00
|
|
|
Validator::string()->length(1,15)->assert('');
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $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());
|
|
|
|
}
|
|
|
|
}
|
2012-05-17 20:51:49 +02:00
|
|
|
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testNotShouldWorkByBuilder()
|
2012-05-17 20:51:49 +02:00
|
|
|
{
|
|
|
|
$this->assertFalse(Validator::not(Validator::int())->validate(10));
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testCountryCode()
|
2012-05-24 17:00:18 +02:00
|
|
|
{
|
|
|
|
$this->assertTrue(Validator::countryCode()->validate('BR'));
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testAlwaysValid()
|
2012-05-24 17:07:29 +02:00
|
|
|
{
|
|
|
|
$this->assertTrue(Validator::alwaysValid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
|
|
|
}
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testAlwaysInvalid()
|
2012-05-24 17:07:29 +02:00
|
|
|
{
|
|
|
|
$this->assertFalse(Validator::alwaysInvalid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
|
|
|
}
|
2012-11-27 01:50:45 +01:00
|
|
|
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testIssue85FindMessagesShouldNotTriggerCatchableFatalError()
|
2012-11-27 01:50:45 +01:00
|
|
|
{
|
|
|
|
$usernameValidator = Validator::alnum('_')->length(1,15)->noWhitespace();
|
|
|
|
try {
|
|
|
|
$usernameValidator->assert('really messed up screen#name');
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
2012-12-04 17:21:11 +01:00
|
|
|
$e->findMessages(array('alnum', 'length', 'noWhitespace'));
|
2012-11-27 01:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 17:30:59 +01:00
|
|
|
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testKeysAsValidatorNames()
|
2012-12-04 17:30:59 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Validator::key('username', Validator::length(1,32))
|
|
|
|
->key('birthdate', Validator::date())
|
|
|
|
->setName("User Subscription Form")
|
|
|
|
->assert(array('username' => '', 'birthdate' => ''));
|
2015-01-30 02:53:20 +01:00
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
2012-12-04 17:30:59 +01:00
|
|
|
$this->assertEquals('\-These rules must pass for User Subscription Form
|
|
|
|
|-Key username must be valid
|
|
|
|
| \-"" must have a length between 1 and 32
|
|
|
|
\-Key birthdate must be valid
|
|
|
|
\-"" must be a valid date', $e->getFullMessage());
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 20:25:34 +01:00
|
|
|
|
2015-01-19 23:12:07 +01:00
|
|
|
/**
|
|
|
|
* Regression test #174.
|
|
|
|
*/
|
|
|
|
public function testShouldReturnANewValidatorInstanceWhenTheNotRuleIsCalledWithoutAnyArgument()
|
|
|
|
{
|
|
|
|
$validator = new Validator();
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Respect\Validation\Validator', $validator->not());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regression test #174.
|
|
|
|
*/
|
|
|
|
public function testShouldReturnValidatorInstanceWhenTheNotRuleIsCalledWithArguments()
|
|
|
|
{
|
|
|
|
$validator = new Validator();
|
|
|
|
|
|
|
|
$this->assertSame($validator, $validator->not($validator->notEmpty()));
|
|
|
|
}
|
2015-01-30 02:53:20 +01:00
|
|
|
|
|
|
|
public function testDoNotRelyOnNestedValidationExceptionInterfaceForCheck()
|
|
|
|
{
|
|
|
|
$usernameValidator = Validator::alnum('_')->length(1, 15)->noWhitespace();
|
|
|
|
try {
|
|
|
|
$usernameValidator->check('really messed up screen#name');
|
|
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
|
|
$this->fail('Check used NestedValidationException');
|
|
|
|
} catch (ValidationExceptionInterface $e) {
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 23:12:07 +01:00
|
|
|
}
|