mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 22:35:45 +01:00
* `ExceptionInterface`: all Respect\Validation\Exceptions implement through inheritance
* `ValidatorExceptionInterface`: implemented only by AllOfException. This allows the end users to know when there has been a Validator exception rather than just any of our exceptions.
Fixed formatting issues
Created two new exception types
* Created ValidationExceptionInterface
* Created NestedValidationExceptionInterface which extends ValidationExceptionInterface
* Renamed from ValidatorExceptionInterface
* ValidationException implements ValidationExceptionInterface and ValidationExceptionTest checks for the implementation.
* AbstractNestedException implements NestedValidationExceptionInterface and AbstractNestedExceptionTest checks for the implementation.
* CheckExceptionsTest now checks all Rule exceptions to make sure they implement ValidationExceptionInterface
* ValidatorTest now contains test that shows that only ValidationExceptionInterface can be used reliably with `::check()`
* Updated documentation for new exception types
* Reworked examples to show how to catch the exception interfaces
Minor changes in readme.md and ExceptionInterfaces
* Removed `import` statements (hahaha)
* Renamed `$e` to `$exception`
* `ValidationExceptionInterface` now extends `ExceptionInterface`. Changed `ValidationException` to match
128 lines
4.7 KiB
PHP
128 lines
4.7 KiB
PHP
<?php
|
|
namespace Respect\Validation;
|
|
|
|
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
|
|
use Respect\Validation\Exceptions\ValidationExceptionInterface;
|
|
|
|
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
public function testStaticCreateShouldReturnNewValidator()
|
|
{
|
|
$this->assertInstanceOf('Respect\Validation\Validator', Validator::create());
|
|
}
|
|
|
|
public function testInvalidRuleClassShouldThrowComponentException()
|
|
{
|
|
$this->setExpectedException('Respect\Validation\Exceptions\ComponentException');
|
|
Validator::iDoNotExistSoIShouldThrowException();
|
|
}
|
|
public function testSetTemplateWithSingleValidatorShouldUseTemplateAsMainMessage()
|
|
{
|
|
try {
|
|
Validator::callback('is_int')->setTemplate('{{name}} is not tasty')->assert('something');
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
|
}
|
|
}
|
|
public function testSetTemplateWithMultipleValidatorsShouldUseTemplateAsMainMessage()
|
|
{
|
|
try {
|
|
Validator::callback('is_int')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
|
|
}
|
|
}
|
|
public function testSetTemplateWithMultipleValidatorsShouldUseTemplateAsFullMessage()
|
|
{
|
|
try {
|
|
Validator::callback('is_string')->between(1,2)->setTemplate('{{name}} is not tasty')->assert('something');
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$this->assertEquals('\-"something" is not tasty
|
|
\-"something" must be greater than 1', $e->getFullMessage());
|
|
}
|
|
}
|
|
public function testGetFullMessageShouldIncludeAllValidationMessagesInAChain()
|
|
{
|
|
try {
|
|
Validator::string()->length(1,15)->assert('');
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$this->assertEquals('\-These rules must pass for ""
|
|
\-"" must have a length between 1 and 15', $e->getFullMessage());
|
|
}
|
|
}
|
|
|
|
public function testNotShouldWorkByBuilder()
|
|
{
|
|
$this->assertFalse(Validator::not(Validator::int())->validate(10));
|
|
}
|
|
public function testCountryCode()
|
|
{
|
|
$this->assertTrue(Validator::countryCode()->validate('BR'));
|
|
}
|
|
public function testAlwaysValid()
|
|
{
|
|
$this->assertTrue(Validator::alwaysValid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
|
}
|
|
public function testAlwaysInvalid()
|
|
{
|
|
$this->assertFalse(Validator::alwaysInvalid()->validate('sojdnfjsdnfojsdnfos dfsdofj sodjf '));
|
|
}
|
|
|
|
public function testIssue85FindMessagesShouldNotTriggerCatchableFatalError()
|
|
{
|
|
$usernameValidator = Validator::alnum('_')->length(1,15)->noWhitespace();
|
|
try {
|
|
$usernameValidator->assert('really messed up screen#name');
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$e->findMessages(array('alnum', 'length', 'noWhitespace'));
|
|
}
|
|
}
|
|
|
|
public function testKeysAsValidatorNames()
|
|
{
|
|
try {
|
|
Validator::key('username', Validator::length(1,32))
|
|
->key('birthdate', Validator::date())
|
|
->setName("User Subscription Form")
|
|
->assert(array('username' => '', 'birthdate' => ''));
|
|
} catch (NestedValidationExceptionInterface $e) {
|
|
$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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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()));
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|