mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
Equals validator for equality/identity. Tests for AbstractRelated
This commit is contained in:
parent
8f206ede7c
commit
71b510cd51
25
library/Respect/Validation/Rules/Equals.php
Normal file
25
library/Respect/Validation/Rules/Equals.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Equals extends AbstractRule
|
||||
{
|
||||
|
||||
protected $param = null;
|
||||
protected $identical = false;
|
||||
|
||||
public function __construct($param, $identical=false)
|
||||
{
|
||||
$this->param = $param;
|
||||
$this->identical = $identical;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->identical)
|
||||
return $input === $this->param;
|
||||
else
|
||||
return $inptu == $this->param;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class AbstractRelatedTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected function createMock($referenceName, $validator, $mandatory,
|
||||
$hasReference)
|
||||
{
|
||||
$mock = $this->getMockForAbstractClass(
|
||||
'Respect\Validation\Rules\AbstractRelated',
|
||||
array($referenceName, $validator, $mandatory)
|
||||
);
|
||||
$mock
|
||||
->expects($this->any())
|
||||
->method('hasReference')
|
||||
->will($this->returnValue($hasReference));
|
||||
$mock
|
||||
->expects($this->any())
|
||||
->method('getReferenceValue')
|
||||
->will($this->returnValue($referenceName));
|
||||
return $mock;
|
||||
}
|
||||
|
||||
public function testValidateMandatoryTrue()
|
||||
{
|
||||
$mock = $this->createMock('whatever', new NotEmpty, true, false);
|
||||
$this->assertFalse($mock->validate('whatever'));
|
||||
}
|
||||
|
||||
public function testValidateMandatoryFalse()
|
||||
{
|
||||
$mock = $this->createMock('whatever', new NotEmpty, false, false);
|
||||
$this->assertTrue($mock->validate('whatever'));
|
||||
}
|
||||
|
||||
public function testValidateHasReference()
|
||||
{
|
||||
$mock = $this->createMock(
|
||||
'', new NotEmpty, false, false
|
||||
);
|
||||
$this->assertFalse($mock->validate('bla'));
|
||||
|
||||
$mock = $this->createMock(
|
||||
'this is not empty', new NotEmpty, false, false
|
||||
);
|
||||
$this->assertTrue($mock->validate('bla'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testAssert()
|
||||
{
|
||||
$mock = $this->createMock(
|
||||
'', new NotEmpty, false, false
|
||||
);
|
||||
//overriding exception cause mocks cant handle createException properly
|
||||
$mock->setException(new \InvalidArgumentException(''));
|
||||
$this->assertFalse($mock->assert('bla'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testAssertHasReference()
|
||||
{
|
||||
$mock = $this->createMock(
|
||||
'', null, true, false
|
||||
);
|
||||
//overriding exception cause mocks cant handle createException properly
|
||||
$mock->setException(new \InvalidArgumentException(''));
|
||||
$this->assertFalse($mock->assert('bla'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\NotEmptyException
|
||||
*/
|
||||
public function testCheck()
|
||||
{
|
||||
$mock = $this->createMock(
|
||||
'', new NotEmpty, true, true
|
||||
);
|
||||
$this->assertFalse($mock->check('bla'));
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedException Respect\Validation\Exceptions\ZendException
|
||||
*/
|
||||
public function testSimpleNot()
|
||||
{
|
||||
|
@ -39,7 +39,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
* @expectedException Respect\Validation\Exceptions\ZendException
|
||||
*/
|
||||
public function testParamsNot()
|
||||
{
|
||||
|
|
|
@ -59,35 +59,4 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testSample()
|
||||
{
|
||||
|
||||
$target = new \stdClass;
|
||||
$target->sex = 'foo';
|
||||
$target->id = 49549;
|
||||
|
||||
$validator = Validator::object()
|
||||
->oneOf(
|
||||
Validator::attribute(
|
||||
'screen_name', Validator::alnum('_')->noWhitespace()
|
||||
),
|
||||
Validator::attribute(
|
||||
'id', Validator::numeric()->between(1, 15)
|
||||
)
|
||||
)
|
||||
->attribute('created_at', Validator::date())
|
||||
->attribute('name', $v160 = Validator::stringLength(1, 160))
|
||||
->attribute('sex',
|
||||
Validator::oneOf(
|
||||
Validator::hexa(), Validator::float(), Validator::numeric()
|
||||
))
|
||||
->attribute('description', $v160, false)
|
||||
->attribute('location', $v160, false);
|
||||
try {
|
||||
$validator->assert($target);
|
||||
} catch (Exceptions\ValidationException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue