Equals validator for equality/identity. Tests for AbstractRelated

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-07 10:02:57 -02:00
parent 8f206ede7c
commit 71b510cd51
4 changed files with 115 additions and 33 deletions

View 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;
}
}

View file

@ -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'));
}
}

View file

@ -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()
{

View file

@ -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();
}
}
}