mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Improved several tests, fixed bug on Arr::check() that was calling methods on non-objects
This commit is contained in:
parent
edb2d855ab
commit
f2a37bcfa1
|
@ -7,25 +7,27 @@ use Respect\Validation\Validator as v;
|
|||
class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testAddRelated()
|
||||
public function test_getRelated_should_return_exception_added_by_addRelated()
|
||||
{
|
||||
$x = new AttributeException;
|
||||
$int = new IntException;
|
||||
$x->addRelated($int);
|
||||
$this->assertEquals(1, count($x->getRelated(true)));
|
||||
$composite = new AttributeException;
|
||||
$node = new IntException;
|
||||
$composite->addRelated($node);
|
||||
$this->assertEquals(1, count($composite->getRelated(true)));
|
||||
$this->assertContainsOnly($node, $composite->getRelated());
|
||||
}
|
||||
|
||||
public function testAddRelatedIdentity()
|
||||
public function test_adding_the_same_instance_should_add_just_a_single_reference()
|
||||
{
|
||||
$x = new AttributeException;
|
||||
$int = new IntException;
|
||||
$x->addRelated($int);
|
||||
$x->addRelated($int);
|
||||
$x->addRelated($int);
|
||||
$this->assertEquals(1, count($x->getRelated(true)));
|
||||
$composite = new AttributeException;
|
||||
$node = new IntException;
|
||||
$composite->addRelated($node);
|
||||
$composite->addRelated($node);
|
||||
$composite->addRelated($node);
|
||||
$this->assertEquals(1, count($composite->getRelated(true)));
|
||||
$this->assertContainsOnly($node, $composite->getRelated());
|
||||
}
|
||||
|
||||
public function testFindRelated()
|
||||
public function test_find_related_should_find_composite_exceptions()
|
||||
{
|
||||
$foo = new AttributeException;
|
||||
$bar = new AttributeException;
|
||||
|
@ -47,7 +49,7 @@ class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertSame(false, $foo->findRelated('bar.none'));
|
||||
}
|
||||
|
||||
public function testFindMessages()
|
||||
public function test_findMessages_should_return_composite_validation_messages_flattened()
|
||||
{
|
||||
$stringMax256 = v::string()->length(5, 256);
|
||||
$alnumDot = v::alnum('.');
|
||||
|
@ -86,7 +88,7 @@ class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
|
||||
public function testFindMessagesTemplates()
|
||||
public function test_findMessages_should_apply_templates_to_flattened_messages()
|
||||
{
|
||||
$stringMax256 = v::string()->length(5, 256);
|
||||
$alnumDot = v::alnum('.');
|
||||
|
|
|
@ -8,7 +8,7 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForFormat
|
||||
*/
|
||||
public function testFormat($template, $result, $vars)
|
||||
public function test_format_should_replace_placeholders_properly($template, $result, $vars)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
|
@ -19,7 +19,7 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForStringify
|
||||
*/
|
||||
public function testStringify($input, $result)
|
||||
public function test_stringify_should_convert_strings_properly($input, $result)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
|
@ -27,18 +27,18 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testGetMainMessage()
|
||||
public function test_getMainMessage_should_apply_template_placeholders()
|
||||
{
|
||||
$x = new ValidationException();
|
||||
$x->configure('foo', array('bar' => 1, 'baz' => 2));
|
||||
$x->setTemplate('{{name}} {{bar}} {{baz}}');
|
||||
$sampleValidationException = new ValidationException();
|
||||
$sampleValidationException->configure('foo', array('bar' => 1, 'baz' => 2));
|
||||
$sampleValidationException->setTemplate('{{name}} {{bar}} {{baz}}');
|
||||
$this->assertEquals(
|
||||
'foo 1 2',
|
||||
$x->getMainMessage()
|
||||
$sampleValidationException->getMainMessage()
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetTemplateSet()
|
||||
public function test_setting_templates()
|
||||
{
|
||||
$x = new ValidationException();
|
||||
$x->configure('bar');
|
||||
|
@ -49,7 +49,7 @@ class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForStringify
|
||||
*/
|
||||
public function testSetParam($input, $expected)
|
||||
public function test_setting_exception_params_makes_them_available($input, $expected)
|
||||
{
|
||||
$x = new ValidationException;
|
||||
$x->setParam('foo', $input);
|
||||
|
|
|
@ -1,232 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
class NegativeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/*
|
||||
* Toggle this to show an example of all validation
|
||||
* messages on the PHPUnit console.
|
||||
*/
|
||||
|
||||
protected $showMessages = false;
|
||||
protected $targetName = 'My Field';
|
||||
|
||||
protected function doTestValidator($validator, $invalidValue)
|
||||
{
|
||||
try {
|
||||
$validator->assert($invalidValue);
|
||||
$this->fail();
|
||||
} catch (ValidationException $e) {
|
||||
if ($this->showMessages)
|
||||
echo 'Default: ' . $e->getFullMessage() . PHP_EOL;
|
||||
}
|
||||
$validator->setName($this->targetName);
|
||||
try {
|
||||
$validator->assert($invalidValue);
|
||||
$this->fail();
|
||||
} catch (ValidationException $e) {
|
||||
if ($this->showMessages)
|
||||
echo 'Named: ' . $e->getFullMessage() . PHP_EOL . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
public function testAlnum()
|
||||
{
|
||||
$this->doTestValidator(v::alnum(), '#');
|
||||
$this->doTestValidator(v::alnum('_'), '#');
|
||||
}
|
||||
|
||||
public function testAlpha()
|
||||
{
|
||||
$this->doTestValidator(v::alpha(), '#');
|
||||
$this->doTestValidator(v::alpha('.'), '#');
|
||||
}
|
||||
|
||||
public function testArr()
|
||||
{
|
||||
$this->doTestValidator(v::arr(), '#');
|
||||
}
|
||||
|
||||
public function testAttribute()
|
||||
{
|
||||
$this->doTestValidator(v::attribute("foo", v::int()),
|
||||
(object) array("foo" => "bar"));
|
||||
$this->doTestValidator(v::attribute("foo", v::string()), null);
|
||||
}
|
||||
|
||||
public function testBetween()
|
||||
{
|
||||
$this->doTestValidator(v::between(5, 15), 999);
|
||||
$this->doTestValidator(v::between('a', 'f'), 15951);
|
||||
$this->doTestValidator(v::between(new \DateTime('now'),
|
||||
new \DateTime('tomorrow')), new \DateTime('yesterday'));
|
||||
}
|
||||
|
||||
public function testCall()
|
||||
{
|
||||
$this->doTestValidator(v::call('implode', v::int()), array('x', 2, 3, 4));
|
||||
}
|
||||
|
||||
public function testCallback()
|
||||
{
|
||||
$this->doTestValidator(v::callback('is_string'), 123);
|
||||
}
|
||||
|
||||
public function testDate()
|
||||
{
|
||||
$this->doTestValidator(v::date('Y-m-d'), '2010-30-10');
|
||||
$this->doTestValidator(v::date(), 'Jan 310 2008');
|
||||
}
|
||||
|
||||
public function testDigits()
|
||||
{
|
||||
$this->doTestValidator(v::digits(), 'x02384');
|
||||
}
|
||||
|
||||
public function testEach()
|
||||
{
|
||||
$this->doTestValidator(v::each(v::hexa()), array('AF', 'P1', '09'));
|
||||
}
|
||||
|
||||
public function testEquals()
|
||||
{
|
||||
$this->doTestValidator(v::equals('foobar'), 'aaar');
|
||||
}
|
||||
|
||||
public function testFloat()
|
||||
{
|
||||
$this->doTestValidator(v::float(), 'dance');
|
||||
}
|
||||
|
||||
public function testHexa()
|
||||
{
|
||||
$this->doTestValidator(v::hexa(), 'PPAFAF');
|
||||
}
|
||||
|
||||
public function testIn()
|
||||
{
|
||||
$this->doTestValidator(v::in(array(1, 1, 2, 3, 5, 8)), 9845984);
|
||||
}
|
||||
|
||||
public function testInstance()
|
||||
{
|
||||
$this->doTestValidator(v::instance('\ss'), new \stdClass);
|
||||
}
|
||||
|
||||
public function testInt()
|
||||
{
|
||||
$this->doTestValidator(v::int(), 15.48);
|
||||
}
|
||||
|
||||
public function testIp()
|
||||
{
|
||||
$this->doTestValidator(v::ip(), '200.999.220.222');
|
||||
}
|
||||
|
||||
public function testLength()
|
||||
{
|
||||
$this->doTestValidator(v::length(5, 10), 'foobarbalzobihbiy');
|
||||
$this->doTestValidator(v::length(2, 3), array(1, 2, 3, 4, 5));
|
||||
$this->doTestValidator(v::length(null, 3), array(1, 2, 3, 4, 5));
|
||||
$this->doTestValidator(v::length(15, null), array(1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
public function testMax()
|
||||
{
|
||||
$this->doTestValidator(v::max(5), 9854);
|
||||
$this->doTestValidator(v::max(5, true), 9854);
|
||||
}
|
||||
|
||||
public function testMin()
|
||||
{
|
||||
$this->doTestValidator(v::min(5), -9514);
|
||||
$this->doTestValidator(v::min(5, true), -9514);
|
||||
}
|
||||
|
||||
public function testNegative()
|
||||
{
|
||||
$this->doTestValidator(v::negative(), 5);
|
||||
}
|
||||
|
||||
public function testPositive()
|
||||
{
|
||||
$this->doTestValidator(v::positive(), -3);
|
||||
}
|
||||
|
||||
public function testNoWhitespace()
|
||||
{
|
||||
$this->doTestValidator(v::noWhitespace(), 'a bc');
|
||||
}
|
||||
|
||||
public function testNotEmpty()
|
||||
{
|
||||
$this->doTestValidator(v::notEmpty(), '');
|
||||
}
|
||||
|
||||
public function testNullValue()
|
||||
{
|
||||
$this->doTestValidator(v::nullValue(), true);
|
||||
}
|
||||
|
||||
public function testNumeric()
|
||||
{
|
||||
$this->doTestValidator(v::numeric(), null);
|
||||
}
|
||||
|
||||
public function testObject()
|
||||
{
|
||||
$this->doTestValidator(v::object(), null);
|
||||
}
|
||||
|
||||
public function testRegex()
|
||||
{
|
||||
$this->doTestValidator(v::regex('/^[a-f]+$/'), 'abcdxxxef');
|
||||
}
|
||||
|
||||
public function testString()
|
||||
{
|
||||
$this->doTestValidator(v::string(), null);
|
||||
}
|
||||
|
||||
public function testSartsWith()
|
||||
{
|
||||
$this->doTestValidator(v::startsWith('Xello'), 'Hello World');
|
||||
}
|
||||
|
||||
public function testEndsWith()
|
||||
{
|
||||
$this->doTestValidator(v::endsWith('Yorld'), 'Hello World');
|
||||
}
|
||||
|
||||
public function testAllOf()
|
||||
{
|
||||
$this->doTestValidator(v::allOf(
|
||||
v::string(), //any string
|
||||
v::length(5, 20), //between 5 and 20 chars
|
||||
v::noWhitespace() //no whitespace allowed
|
||||
), "# #");
|
||||
|
||||
//same as
|
||||
|
||||
$this->doTestValidator(v::string()
|
||||
->length(5, 20)
|
||||
->noWhitespace(), '# #');
|
||||
}
|
||||
|
||||
public function testOneOf()
|
||||
{
|
||||
$v = v::oneOf(
|
||||
v::int()->positive(), //positive integer or;
|
||||
v::float()->negative(), //negative float or;
|
||||
v::nullValue() //null
|
||||
);
|
||||
$this->doTestValidator($v, '');
|
||||
$this->doTestValidator($v, '');
|
||||
$this->doTestValidator($v, '');
|
||||
}
|
||||
|
||||
}
|
|
@ -9,14 +9,14 @@ use Respect\Validation\Validator;
|
|||
class AllOfTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testRemoveRules()
|
||||
public function test_removeRules_should_remove_all_rules()
|
||||
{
|
||||
$o = new AllOf(new Int, new Positive);
|
||||
$o->removeRules();
|
||||
$this->assertEquals(0, count($o->getRules()));
|
||||
}
|
||||
|
||||
public function testAddRulesArrayMulti()
|
||||
public function test_addRules_using_array_of_rules()
|
||||
{
|
||||
$o = new AllOf();
|
||||
$o->addRules(
|
||||
|
@ -28,14 +28,14 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($o->hasRule('Positive'));
|
||||
}
|
||||
|
||||
public function testAddRulesSpec()
|
||||
public function test_addRules_using_specification_array()
|
||||
{
|
||||
$o = new AllOf();
|
||||
$o->addRules(array("Between" => array(1, 2)));
|
||||
$this->assertTrue($o->hasRule('Between'));
|
||||
}
|
||||
|
||||
public function testValid()
|
||||
public function test_validation_should_work_if_all_rules_return_true()
|
||||
{
|
||||
$valid1 = new Callback(function() {
|
||||
return true;
|
||||
|
@ -47,59 +47,71 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
|
|||
return true;
|
||||
});
|
||||
$o = new AllOf($valid1, $valid2, $valid3);
|
||||
$this->assertTrue($o->validate('any'));
|
||||
$this->assertTrue($o->check('any'));
|
||||
$this->assertTrue($o->assert('any'));
|
||||
}
|
||||
public function testFilter()
|
||||
|
||||
public function test_filtering_should_work_for_composite_filters()
|
||||
{
|
||||
$valid = new Digits();
|
||||
$filter = new Int();
|
||||
$o = new AllOf($valid, $filter);
|
||||
$this->assertTrue($o->validate(12));
|
||||
$this->assertTrue($o->check(12));
|
||||
$this->assertTrue($o->assert(12));
|
||||
$this->assertSame(12, $o->filter('12 monkeys'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerStaticDummyRules
|
||||
* @expectedException Respect\Validation\Exceptions\AllOfException
|
||||
*/
|
||||
public function testInvalid()
|
||||
public function test_validation_assert_should_fail_if_any_rule_fails_and_return_all_exceptions_failed($v1, $v2, $v3)
|
||||
{
|
||||
$o = new AllOf($v1, $v2, $v3);
|
||||
$this->assertFalse($o->validate('any'));
|
||||
$this->assertFalse($o->assert('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerStaticDummyRules
|
||||
* @expectedException Respect\Validation\Exceptions\CallbackException
|
||||
*/
|
||||
public function test_validation_check_should_fail_if_any_rule_fails_and_throw_the_first_exception_only($v1, $v2, $v3)
|
||||
{
|
||||
$o = new AllOf($v1, $v2, $v3);
|
||||
$this->assertFalse($o->validate('any'));
|
||||
$this->assertFalse($o->check('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerStaticDummyRules
|
||||
*/
|
||||
public function test_validation_should_fail_if_any_rule_fails($v1, $v2, $v3)
|
||||
{
|
||||
$o = new AllOf($v1, $v2, $v3);
|
||||
$this->assertFalse($o->validate('any'));
|
||||
}
|
||||
|
||||
public function providerStaticDummyRules()
|
||||
{
|
||||
$theInvalidOne = new Callback(function() {
|
||||
return false;
|
||||
});
|
||||
$valid1 = new Callback(function() {
|
||||
return true;
|
||||
});
|
||||
$valid2 = new Callback(function() {
|
||||
return true;
|
||||
});
|
||||
$valid3 = new Callback(function() {
|
||||
return true;
|
||||
});
|
||||
$o = new AllOf($theInvalidOne, $valid2, $valid3);
|
||||
$this->assertFalse($o->assert('any'));
|
||||
$o = new AllOf($valid2, $theInvalidOne, $valid3);
|
||||
$this->assertFalse($o->assert('any'));
|
||||
$o = new AllOf($valid2, $valid3, $theInvalidOne);
|
||||
$this->assertFalse($o->assert('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testCheck()
|
||||
{
|
||||
$valid1 = new Callback(function() {
|
||||
return true;
|
||||
});
|
||||
$theInvalidOne = new Callback(function() {
|
||||
return false;
|
||||
});
|
||||
$valid3 = new Callback(function() {
|
||||
return true;
|
||||
});
|
||||
$o = new AllOf($valid1, $theInvalidOne, $valid3);
|
||||
$this->assertFalse($o->check('any'));
|
||||
$o = new AllOf($theInvalidOne, $valid3, $valid1);
|
||||
$this->assertFalse($o->check('any'));
|
||||
$o = new AllOf($valid3, $valid1, $theInvalidOne);
|
||||
$this->assertFalse($o->check('any'));
|
||||
return array(
|
||||
array($theInvalidOne, $valid1, $valid2),
|
||||
array($valid2, $valid1, $theInvalidOne),
|
||||
array($valid2, $theInvalidOne, $valid1),
|
||||
array($valid1, $valid2, $theInvalidOne),
|
||||
array($valid1, $theInvalidOne, $valid2)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,17 +8,19 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForValidAlnum
|
||||
*/
|
||||
public function testAlnumValid($validAlnum, $aditional)
|
||||
public function test_valid_alnum_chars_should_return_true($validAlnum, $aditional)
|
||||
{
|
||||
$validator = new Alnum($aditional);
|
||||
$this->assertTrue($validator->validate($validAlnum));
|
||||
$this->assertTrue($validator->check($validAlnum));
|
||||
$this->assertTrue($validator->assert($validAlnum));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlnum
|
||||
* @expectedException Respect\Validation\Exceptions\AlnumException
|
||||
*/
|
||||
public function testAlnumInvalid($invalidAlnum, $aditional)
|
||||
public function test_invalid_alnum_chars_should_throw_AlnumException_and_return_false($invalidAlnum, $aditional)
|
||||
{
|
||||
$validator = new Alnum($aditional);
|
||||
$this->assertFalse($validator->validate($invalidAlnum));
|
||||
|
@ -29,7 +31,7 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($aditional)
|
||||
public function test_invalid_constructor_params_should_throw_ComponentException_upon_instantiation($aditional)
|
||||
{
|
||||
$validator = new Alnum($aditional);
|
||||
}
|
||||
|
|
|
@ -8,17 +8,19 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForValidAlpha
|
||||
*/
|
||||
public function testAlphaValid($validAlpha, $aditional)
|
||||
public function test_valid_alphanumeric_chars_should_return_true($validAlpha, $aditional)
|
||||
{
|
||||
$validator = new Alpha($aditional);
|
||||
$this->assertTrue($validator->validate($validAlpha));
|
||||
$this->assertTrue($validator->check($validAlpha));
|
||||
$this->assertTrue($validator->assert($validAlpha));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlpha
|
||||
* @expectedException Respect\Validation\Exceptions\AlphaException
|
||||
*/
|
||||
public function testAlphaInvalid($invalidAlpha, $aditional)
|
||||
public function test_invalid_alphanumeric_chars_should_throw_AlphaException($invalidAlpha, $aditional)
|
||||
{
|
||||
$validator = new Alpha($aditional);
|
||||
$this->assertFalse($validator->validate($invalidAlpha));
|
||||
|
@ -29,7 +31,7 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($aditional)
|
||||
public function test_invalid_constructor_params_should_throw_ComponentExeption($aditional)
|
||||
{
|
||||
$validator = new Alpha($aditional);
|
||||
}
|
||||
|
|
|
@ -14,19 +14,19 @@ class ArrTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForArray
|
||||
*
|
||||
*/
|
||||
public function testArray($input)
|
||||
public function test_valid_array_or_ArrayObject_should_return_true($input)
|
||||
{
|
||||
$this->assertTrue($this->object->validate($input));
|
||||
$this->assertTrue($this->object->assert($input));
|
||||
$this->assertTrue($this->object->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotArray
|
||||
* @expectedException Respect\Validation\Exceptions\ArrException
|
||||
*/
|
||||
public function testNotArray($input)
|
||||
public function test_not_arrays_should_throw_ArrException($input)
|
||||
{
|
||||
$this->assertFalse($this->object->validate($input));
|
||||
$this->assertFalse($this->object->assert($input));
|
||||
|
|
|
@ -12,11 +12,12 @@ class PrivClass
|
|||
class AttributeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testAttribute()
|
||||
public function test_attribute_with_no_extra_validation_should_check_its_presence()
|
||||
{
|
||||
$validator = new Attribute('bar');
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo';
|
||||
$this->assertTrue($validator->check($obj));
|
||||
$this->assertTrue($validator->validate($obj));
|
||||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
@ -24,7 +25,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\AttributeException
|
||||
*/
|
||||
public function testNotNull()
|
||||
public function test_absent_attribute_should_raise_AttributeException()
|
||||
{
|
||||
$validator = new Attribute('bar');
|
||||
$obj = new \stdClass;
|
||||
|
@ -35,7 +36,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testNotNullCheck()
|
||||
public function test_absent_attribute_should_raise_AttributeException_on_check()
|
||||
{
|
||||
$validator = new Attribute('bar');
|
||||
$obj = new \stdClass;
|
||||
|
@ -48,7 +49,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForInvalidAtrributeNames
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($attributeName)
|
||||
public function test_invalid_constructor_arguments_should_throw_ComponentException($attributeName)
|
||||
{
|
||||
$validator = new Attribute($attributeName);
|
||||
}
|
||||
|
@ -62,7 +63,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
);
|
||||
}
|
||||
|
||||
public function testValidatorAttribute()
|
||||
public function test_extra_validator_rules_for_attribute()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
|
@ -72,8 +73,47 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($validator->assert($obj));
|
||||
$this->assertTrue($validator->check($obj));
|
||||
}
|
||||
|
||||
public function test_extra_validator_rules_for_attribute_should_fail_if_invalid()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo hey this has more than 3 chars';
|
||||
$this->assertFalse($validator->validate($obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\LengthException
|
||||
*/
|
||||
public function test_extra_validator_rules_for_attribute_should_raise_extra_validator_exception_on_check()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo hey this has more than 3 chars';
|
||||
$this->assertFalse($validator->check($obj));
|
||||
}
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\AttributeException
|
||||
*/
|
||||
public function test_extra_validator_rules_for_attribute_should_raise_AttributeException_on_assert()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
$obj = new \stdClass;
|
||||
$obj->bar = 'foo hey this has more than 3 chars';
|
||||
$this->assertFalse($validator->assert($obj));
|
||||
}
|
||||
|
||||
public function testNotMandatory()
|
||||
public function test_not_mandatory_attribute_should_not_fail_when_attribute_is_absent()
|
||||
{
|
||||
$validator = new Attribute('bar', null, false);
|
||||
$obj = new \stdClass;
|
||||
$this->assertTrue($validator->validate($obj));
|
||||
}
|
||||
|
||||
public function test_not_mandatory_attribute_should_not_fail_when_attribute_is_absent_with_extra_validator()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator, false);
|
||||
|
@ -81,7 +121,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($validator->validate($obj));
|
||||
}
|
||||
|
||||
public function testValidatorPrivateAttribute()
|
||||
public function test_private_attribute_should_also_be_checked()
|
||||
{
|
||||
$subValidator = new Length(1, 3);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
|
@ -89,4 +129,12 @@ class AttributeTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($validator->assert($obj));
|
||||
}
|
||||
|
||||
public function test_private_attribute_should_fail_if_not_valid()
|
||||
{
|
||||
$subValidator = new Length(33333, 888888);
|
||||
$validator = new Attribute('bar', $subValidator);
|
||||
$obj = new PrivClass;
|
||||
$this->assertFalse($validator->validate($obj));
|
||||
}
|
||||
|
||||
}
|
|
@ -50,18 +50,19 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerValid
|
||||
*/
|
||||
public function testBetweenBounds($min, $max, $inclusive, $input)
|
||||
public function test_values_between_bounds_should_pass($min, $max, $inclusive, $input)
|
||||
{
|
||||
$o = new Between($min, $max, $inclusive);
|
||||
$this->assertTrue($o->validate($input));
|
||||
$this->assertTrue($o->assert($input));
|
||||
$this->assertTrue($o->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalid
|
||||
* @expectedException Respect\Validation\Exceptions\BetweenException
|
||||
*/
|
||||
public function testNotBetweenBounds($min, $max, $inclusive, $input)
|
||||
public function test_values_out_bounds_should_raise_exception($min, $max, $inclusive, $input)
|
||||
{
|
||||
$o = new Between($min, $max, $inclusive);
|
||||
$this->assertFalse($o->validate($input));
|
||||
|
|
|
@ -5,20 +5,37 @@ namespace Respect\Validation\Rules;
|
|||
class BoolTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testBool()
|
||||
public function test_boolean_values_ONLY_should_return_true()
|
||||
{
|
||||
$validator = new Bool();
|
||||
$this->assertTrue($validator->validate(true));
|
||||
$this->assertTrue($validator->validate(false));
|
||||
$this->assertTrue($validator->assert(true));
|
||||
$this->assertTrue($validator->assert(false));
|
||||
$this->assertTrue($validator->check(true));
|
||||
$this->assertTrue($validator->check(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\BoolException
|
||||
*/
|
||||
public function testNotBool()
|
||||
public function test_invalid_boolean_should_raise_exception()
|
||||
{
|
||||
$validator = new Bool();
|
||||
$validator->check('foo');
|
||||
$this->assertFalse($validator->check('foo'));
|
||||
}
|
||||
|
||||
public function test_invalid_boolean_values_should_return_false()
|
||||
{
|
||||
$validator = new Bool();
|
||||
$this->assertFalse($validator->validate('foo'));
|
||||
$this->assertFalse($validator->validate(123123));
|
||||
$this->assertFalse($validator->validate(new \stdClass()));
|
||||
$this->assertFalse($validator->validate(array()));
|
||||
$this->assertFalse($validator->validate(1));
|
||||
$this->assertFalse($validator->validate(0));
|
||||
$this->assertFalse($validator->validate(null));
|
||||
$this->assertFalse($validator->validate(''));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,24 +5,24 @@ namespace Respect\Validation\Rules;
|
|||
class CallTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function callbackThis()
|
||||
public function thisIsASampleCallbackUsedInsideThisTest()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function testCallbackOk()
|
||||
public function test_callback_validator_should_accept_string_with_function_name()
|
||||
{
|
||||
$v = new Call('str_split', new Arr);
|
||||
$this->assertTrue($v->assert('test'));
|
||||
}
|
||||
|
||||
public function testCallbackObject()
|
||||
public function test_callback_validator_should_accept_array_callback_definition()
|
||||
{
|
||||
$v = new Call(array($this, 'callbackThis'), new Arr);
|
||||
$v = new Call(array($this, 'thisIsASampleCallbackUsedInsideThisTest'), new Arr);
|
||||
$this->assertTrue($v->assert('test'));
|
||||
}
|
||||
|
||||
public function testCallbackClosure()
|
||||
public function test_callback_validator_should_accept_closures()
|
||||
{
|
||||
$v = new Call(function() {
|
||||
return array();
|
||||
|
@ -33,7 +33,7 @@ class CallTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\CallException
|
||||
*/
|
||||
public function testCallbackNot()
|
||||
public function test_callback_failed_should_throw_CallException()
|
||||
{
|
||||
$v = new Call('strrev', new Arr);
|
||||
$this->assertFalse($v->validate('test'));
|
||||
|
|
|
@ -5,12 +5,12 @@ namespace Respect\Validation\Rules;
|
|||
class CallbackTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function callbackThis()
|
||||
public function thisIsASampleCallbackUsedInsideThisTest()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function testCallbackOk()
|
||||
public function test_callback_validator_should_return_true_if_callback_returns_true()
|
||||
{
|
||||
$v = new Callback(function() {
|
||||
return true;
|
||||
|
@ -21,7 +21,7 @@ class CallbackTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\CallbackException
|
||||
*/
|
||||
public function testCallbackNot()
|
||||
public function test_callback_validator_should_return_false_if_callback_returns_false()
|
||||
{
|
||||
$v = new Callback(function() {
|
||||
return false;
|
||||
|
@ -29,13 +29,13 @@ class CallbackTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($v->assert('w poiur'));
|
||||
}
|
||||
|
||||
public function testCallbackObject()
|
||||
public function test_callback_validator_should_accept_array_callback_definitions()
|
||||
{
|
||||
$v = new Callback(array($this, 'callbackThis'));
|
||||
$v = new Callback(array($this, 'thisIsASampleCallbackUsedInsideThisTest'));
|
||||
$this->assertTrue($v->assert('test'));
|
||||
}
|
||||
|
||||
public function testCallbackString()
|
||||
public function test_callback_validator_should_accept_function_names_as_string()
|
||||
{
|
||||
$v = new Callback('is_string');
|
||||
$this->assertTrue($v->assert('test'));
|
||||
|
@ -44,7 +44,7 @@ class CallbackTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters()
|
||||
public function test_invalid_callbacks_should_raise_ComponentException_upon_instantiation()
|
||||
{
|
||||
$v = new Callback(new \stdClass);
|
||||
$this->assertTrue($v->assert('w poiur'));
|
||||
|
|
|
@ -4,45 +4,45 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
class CpfTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
protected $cpf;
|
||||
protected $cpfValidator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->cpf = new Cpf;
|
||||
$this->cpfValidator = new Cpf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerValidFormattedCpf
|
||||
*/
|
||||
public function testValidFormattedCpf($input)
|
||||
public function test_formatted_cpfs_should_validate($input)
|
||||
{
|
||||
$this->assertTrue($this->cpf->assert($input));
|
||||
$this->assertTrue($this->cpfValidator->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerValidUnformattedCpf
|
||||
*/
|
||||
public function testValidUnformattedCpf($input)
|
||||
public function test_unformatted_cpfs_should_validates($input)
|
||||
{
|
||||
$this->assertTrue($this->cpf->assert($input));
|
||||
$this->assertTrue($this->cpfValidator->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidFormattedCpf
|
||||
* @expectedException Respect\Validation\Exceptions\CpfException
|
||||
*/
|
||||
public function testInvalidFormattedCpf($input)
|
||||
public function test_invalid_cpf_should_fail_when_formatted($input)
|
||||
{
|
||||
$this->assertFalse($this->cpf->assert($input));
|
||||
$this->assertFalse($this->cpfValidator->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidUnformattedCpf
|
||||
* @expectedException Respect\Validation\Exceptions\CpfException
|
||||
*/
|
||||
public function testInvalidUnformattedCpf($input)
|
||||
public function test_invalid_cpf_should_fail_when_not_formatted($input)
|
||||
{
|
||||
$this->assertFalse($this->cpf->assert($input));
|
||||
$this->assertFalse($this->cpfValidator->assert($input));
|
||||
}
|
||||
|
||||
|
||||
|
@ -50,9 +50,9 @@ class CpfTest extends \PHPUnit_Framework_TestCase {
|
|||
* @dataProvider providerInvalidFormattedAndUnformattedCpfLength
|
||||
* @expectedException Respect\Validation\Exceptions\CpfException
|
||||
*/
|
||||
public function testInvalidFormattedAndUnformattedCpfLength($input)
|
||||
public function test_cpfs_with_incorrect_length_should_fail($input)
|
||||
{
|
||||
$this->assertFalse($this->cpf->assert($input));
|
||||
$this->assertFalse($this->cpfValidator->assert($input));
|
||||
}
|
||||
|
||||
public function providerValidFormattedCpf()
|
||||
|
|
|
@ -7,51 +7,46 @@ use DateTime;
|
|||
class DateTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
protected $dateValidator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new Date;
|
||||
$this->dateValidator = new Date;
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
public function test_date_without_format_should_validate()
|
||||
{
|
||||
unset($this->object);
|
||||
$this->assertTrue($this->dateValidator->validate('today'));
|
||||
}
|
||||
|
||||
public function testDateWithoutFormat()
|
||||
public function test_DateTime_instances_should_always_validate()
|
||||
{
|
||||
$this->assertTrue($this->object->validate('today'));
|
||||
$this->assertTrue($this->dateValidator->validate(new DateTime('today')));
|
||||
}
|
||||
|
||||
public function testDateInstance()
|
||||
public function test_invalid_date_should_fail()
|
||||
{
|
||||
$this->assertTrue($this->object->validate(new DateTime('today')));
|
||||
$this->assertFalse($this->dateValidator->validate('aids'));
|
||||
}
|
||||
|
||||
public function testInvalidDateWithoutFormat()
|
||||
public function test_any_object_except_DateTime_instances_should_fail()
|
||||
{
|
||||
$this->assertFalse($this->object->validate('aids'));
|
||||
$this->assertFalse($this->dateValidator->validate(new \stdClass));
|
||||
}
|
||||
|
||||
public function testInvalidDateObject()
|
||||
public function test_formats_should_validate_date_strings()
|
||||
{
|
||||
$this->assertFalse($this->object->validate(new \stdClass));
|
||||
}
|
||||
|
||||
public function testDateFormat()
|
||||
{
|
||||
$this->object = new Date('Y-m-d');
|
||||
$this->assertTrue($this->object->assert('2009-09-09'));
|
||||
$this->dateValidator = new Date('Y-m-d');
|
||||
$this->assertTrue($this->dateValidator->assert('2009-09-09'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\DateException
|
||||
*/
|
||||
public function testInvalidDateFormat()
|
||||
public function test_formats_should_validate_date_strings_and_throw_DateException_on_failure()
|
||||
{
|
||||
$this->object = new Date('y-m-d');
|
||||
$this->assertFalse($this->object->assert('2009-09-09'));
|
||||
$this->dateValidator = new Date('y-m-d');
|
||||
$this->assertFalse($this->dateValidator->assert('2009-09-09'));
|
||||
}
|
||||
|
||||
}
|
|
@ -8,7 +8,7 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @dataProvider providerForValidDigits
|
||||
*/
|
||||
public function testDigitsValid($validDigits, $aditional='')
|
||||
public function test_valid_data_with_digits_should_return_true($validDigits, $aditional='')
|
||||
{
|
||||
$validator = new Digits($aditional);
|
||||
$this->assertTrue($validator->validate($validDigits));
|
||||
|
@ -18,7 +18,7 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForInvalidDigits
|
||||
* @expectedException Respect\Validation\Exceptions\DigitsException
|
||||
*/
|
||||
public function testDigitsInvalid($invalidDigits, $aditional='')
|
||||
public function test_invalid_digits_should_fail_and_throw_DigitsException($invalidDigits, $aditional='')
|
||||
{
|
||||
$validator = new Digits($aditional);
|
||||
$this->assertFalse($validator->validate($invalidDigits));
|
||||
|
@ -29,7 +29,7 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidParameters($aditional)
|
||||
public function test_invalid_constructor_params_should_throw_ComponentException_upon_instantiation($aditional)
|
||||
{
|
||||
$validator = new Digits($aditional);
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ class DomainTest extends \PHPUnit_Framework_TestCase
|
|||
* @dataProvider providerForDomain
|
||||
*
|
||||
*/
|
||||
public function testDomain($input)
|
||||
public function test_valid_domains_should_return_true($input)
|
||||
{
|
||||
$this->assertTrue($this->object->validate($input));
|
||||
$this->assertTrue($this->object->assert($input));
|
||||
|
|
|
@ -1,298 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
class ValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testAlnum()
|
||||
{
|
||||
$this->assertTrue(v::alnum()->assert('abc 123'));
|
||||
$this->assertTrue(v::alnum('_')->assert('a_bc _123'));
|
||||
}
|
||||
|
||||
public function testAlpha()
|
||||
{
|
||||
$this->assertTrue(v::alpha()->assert('ab c'));
|
||||
$this->assertTrue(v::alpha('.')->assert('a. b.c'));
|
||||
}
|
||||
|
||||
public function testArr()
|
||||
{
|
||||
$this->assertTrue(v::arr()->assert(array()));
|
||||
}
|
||||
|
||||
public function testAttribute()
|
||||
{
|
||||
$this->assertTrue(v::attribute("foo", v::string())->assert((object) array("foo" => "bar")));
|
||||
}
|
||||
|
||||
public function testBetween()
|
||||
{
|
||||
$this->assertTrue(v::between(5, 15)->assert(10));
|
||||
$this->assertTrue(v::between('a', 'f')->assert('b'));
|
||||
}
|
||||
|
||||
public function testCall()
|
||||
{
|
||||
$this->assertTrue(v::call('implode', v::int())->assert(array(1, 2, 3, 4)));
|
||||
}
|
||||
|
||||
public function testCallback()
|
||||
{
|
||||
$this->assertTrue(v::callback('is_string')->assert('something'));
|
||||
}
|
||||
|
||||
public function testDate()
|
||||
{
|
||||
$this->assertTrue(v::date('Y-m-d')->assert('2010-10-10'));
|
||||
$this->assertTrue(v::date()->assert('Jan 10 2008'));
|
||||
}
|
||||
|
||||
public function testDigits()
|
||||
{
|
||||
$this->assertTrue(v::digits()->assert('02384'));
|
||||
}
|
||||
|
||||
public function testDomain()
|
||||
{
|
||||
$this->assertTrue(v::domain()->assert('google.com'));
|
||||
}
|
||||
|
||||
public function testEach()
|
||||
{
|
||||
$this->assertTrue(v::each(v::hexa())->assert(array('AF', 'D1', '09')));
|
||||
}
|
||||
|
||||
public function testEquals()
|
||||
{
|
||||
$this->assertTrue(v::equals('foobar')->assert('foobar'));
|
||||
}
|
||||
|
||||
public function testFloat()
|
||||
{
|
||||
$this->assertTrue(v::float()->assert(1.5));
|
||||
}
|
||||
|
||||
public function testHexa()
|
||||
{
|
||||
$this->assertTrue(v::hexa()->assert('FAFAF'));
|
||||
}
|
||||
|
||||
public function testIn()
|
||||
{
|
||||
$this->assertTrue(v::in(array(1, 1, 2, 3, 5, 8))->assert(5));
|
||||
}
|
||||
|
||||
public function testInstance()
|
||||
{
|
||||
$this->assertTrue(v::instance('\stdClass')->assert(new \stdClass));
|
||||
}
|
||||
|
||||
public function testInt()
|
||||
{
|
||||
$this->assertTrue(v::int()->assert(1548));
|
||||
}
|
||||
|
||||
public function testIp()
|
||||
{
|
||||
$this->assertTrue(v::ip()->assert('200.226.220.222'));
|
||||
}
|
||||
|
||||
public function testLength()
|
||||
{
|
||||
$this->assertTrue(v::length(5, 10)->assert('foobar'));
|
||||
$this->assertTrue(v::length(5, 10)->assert(array(1, 2, 3, 4, 5)));
|
||||
}
|
||||
|
||||
public function testMax()
|
||||
{
|
||||
$this->assertTrue(v::max(5)->assert(3));
|
||||
}
|
||||
|
||||
public function testMin()
|
||||
{
|
||||
$this->assertTrue(v::min(5)->assert(7));
|
||||
}
|
||||
|
||||
public function testNegative()
|
||||
{
|
||||
$this->assertTrue(v::negative()->assert(-5));
|
||||
}
|
||||
|
||||
public function testPositive()
|
||||
{
|
||||
$this->assertTrue(v::positive()->assert(3));
|
||||
}
|
||||
|
||||
public function testNoWhitespace()
|
||||
{
|
||||
$this->assertTrue(v::noWhitespace()->assert('abc'));
|
||||
}
|
||||
|
||||
public function testNotEmpty()
|
||||
{
|
||||
$this->assertTrue(v::notEmpty()->assert('aaa'));
|
||||
}
|
||||
|
||||
public function testNullValue()
|
||||
{
|
||||
$this->assertTrue(v::nullValue()->assert(null));
|
||||
}
|
||||
|
||||
public function testNumeric()
|
||||
{
|
||||
$this->assertTrue(v::numeric()->assert(1.56e-5));
|
||||
}
|
||||
|
||||
public function testObject()
|
||||
{
|
||||
$this->assertTrue(v::object()->assert(new \DateTime()));
|
||||
}
|
||||
|
||||
public function testRegex()
|
||||
{
|
||||
$this->assertTrue(v::regex('/^[a-f]+$/')->assert('abcdef'));
|
||||
}
|
||||
|
||||
public function testString()
|
||||
{
|
||||
$this->assertTrue(v::string()->assert('Hello World'));
|
||||
}
|
||||
|
||||
public function testSartsWith()
|
||||
{
|
||||
$this->assertTrue(v::startsWith('Hello')->assert('Hello World'));
|
||||
}
|
||||
|
||||
public function testEndsWith()
|
||||
{
|
||||
$this->assertTrue(v::endsWith('World')->assert('Hello World'));
|
||||
}
|
||||
|
||||
public function testAllOf()
|
||||
{
|
||||
$this->assertTrue(v::allOf(
|
||||
v::string(), //any string v::length(5, 20), //between 5 and 20 chars
|
||||
v::noWhitespace() //no whitespace allowed
|
||||
)->assert('alganet'));
|
||||
|
||||
//same as
|
||||
|
||||
$this->assertTrue(v::string()
|
||||
->length(5, 20)
|
||||
->noWhitespace()
|
||||
->assert('alganet'));
|
||||
}
|
||||
|
||||
public function testOneOf()
|
||||
{
|
||||
$v = v::oneOf(
|
||||
v::int()->positive(), //positive integer or;
|
||||
v::float()->negative(), //negative float or;
|
||||
v::nullValue() //null
|
||||
);
|
||||
$this->assertTrue($v->assert(null));
|
||||
$this->assertTrue($v->assert(12));
|
||||
$this->assertTrue($v->assert(-1.1));
|
||||
}
|
||||
|
||||
public function testCallbackCustomMessage()
|
||||
{
|
||||
try {
|
||||
v::callback('is_int')
|
||||
->setTemplate('{{name}} is not tasty')
|
||||
->assert('something');
|
||||
} catch (\Exception $e) {
|
||||
//echo $e->getFullMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function testGmailSignInValidation()
|
||||
{
|
||||
$stringMax256 = v::string()->length(5, 256);
|
||||
$alnumDot = v::alnum('.');
|
||||
$stringMin8 = v::string()->length(8, null);
|
||||
$v = v::allOf(
|
||||
v::attribute('first_name', $stringMax256)->setName('First Name'),
|
||||
v::attribute('last_name', $stringMax256)->setName('Last Name'),
|
||||
v::attribute('desired_login', $alnumDot)->setName('Desired Login'),
|
||||
v::attribute('password', $stringMin8)->setName('Password'),
|
||||
v::attribute('password_confirmation', $stringMin8)->setName('Password Confirmation'),
|
||||
v::attribute('stay_signedin', v::notEmpty())->setName('Stay signed in'),
|
||||
v::attribute('enable_webhistory', v::notEmpty())->setName('Enabled Web History'),
|
||||
v::attribute('security_question', $stringMax256)->setName('Security Question')
|
||||
)->setName('Validation Form');
|
||||
try {
|
||||
$v->assert(
|
||||
(object) array(
|
||||
'first_name' => 'fiif',
|
||||
'last_name' => null,
|
||||
'desired_login' => null,
|
||||
'password' => null,
|
||||
'password_confirmation' => null,
|
||||
'stay_signedin' => null,
|
||||
'enable_webhistory' => null,
|
||||
'security_question' => null,
|
||||
)
|
||||
);
|
||||
} catch (Exceptions\ValidationException $e) {
|
||||
//echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
public function testReadme()
|
||||
{
|
||||
$number = 123;
|
||||
v::numeric()->validate($number); //true
|
||||
//From 1 to 15 non-whitespace alphanumeric characters
|
||||
$validUsername = v::alnum()
|
||||
->noWhitespace()
|
||||
->length(1, 15);
|
||||
|
||||
$validUsername->validate('alganet'); //true
|
||||
$validUser = v::attribute('username', $validUsername) //reusing!
|
||||
->attribute('birthdate', v::date('Y-m-d'))
|
||||
->attribute('birthdat', v::date('Y-m-d'));
|
||||
|
||||
$user = new \stdClass;
|
||||
$user->username = 'alganet';
|
||||
$user->birthdate = '1987-07-01';
|
||||
|
||||
$validUser->validate($user); //true
|
||||
|
||||
|
||||
$validUsername->validate('respect'); //true
|
||||
$validUsername->validate('alexandre gaigalas'); //false
|
||||
$validUsername->validate('#$%'); //false
|
||||
|
||||
|
||||
try {
|
||||
$validUsername->assert('really messed up screen#name');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
//echo $e->getFullMessage();
|
||||
}
|
||||
|
||||
$validBlogPost = v::allOf(v::allOf(v::object()
|
||||
->attribute('title', v::string()->length(1, 32))
|
||||
->attribute('author', $validUser) //reuse!
|
||||
->attribute('date', v::date())
|
||||
->attribute('text', v::string())))
|
||||
->setName('Blog Post')
|
||||
->setTemplate("Not nice {{name}}");
|
||||
|
||||
$blogPost = new \stdClass;
|
||||
$blogPost->author = clone $user;
|
||||
$blogPost->author->username = '# invalid #';
|
||||
|
||||
try {
|
||||
$validBlogPost->assert($blogPost);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
//echo $e->getFullMessage() . PHP_EOL;
|
||||
//echo $e->findRelated('author')->getMainMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -9,7 +9,7 @@
|
|||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
verbose="true"
|
||||
verbose="false"
|
||||
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
|
||||
<filter>
|
||||
<blacklist>
|
||||
|
|
Loading…
Reference in a new issue