respect-validation/tests/library/Respect/Validation/ValidatorTest.php

95 lines
3.4 KiB
PHP
Raw Normal View History

2011-11-29 22:20:07 +01:00
<?php
namespace Respect\Validation;
class ValidatorTest extends \PHPUnit_Framework_TestCase
{
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
public function testInvalidRuleClassShouldThrowComponentException()
2011-11-29 22:20:07 +01:00
{
$this->setExpectedException('Respect\Validation\Exceptions\ComponentException');
Validator::iDoNotExistSoIShouldThrowException();
}
public function testSetTemplateWithSingleValidatorShouldUseTemplateAsMainMessage()
{
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
}
}
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');
} catch (\Exception $e) {
$this->assertEquals('"something" is not tasty', $e->getMainMessage());
}
2012-03-23 01:54:29 +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');
} catch (\Exception $e) {
$this->assertEquals('\-"something" is not tasty
\-"something" must be greater than 1', $e->getFullMessage());
}
}
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('');
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());
}
}
2012-05-17 20:51:49 +02:00
public function testNotShouldWorkByBuilder()
2012-05-17 20:51:49 +02:00
{
$this->assertFalse(Validator::not(Validator::int())->validate(10));
}
public function testCountryCode()
2012-05-24 17:00:18 +02:00
{
$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 '));
}
2012-11-27 01:50:45 +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');
} catch (\InvalidArgumentException $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
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' => ''));
} catch (\InvalidArgumentException $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());
}
}
2012-05-17 20:51:49 +02:00
}