respect-validation/tests/unit/Rules/AttributeTest.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2010-09-27 23:02:30 +02:00
<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2010-09-27 23:02:30 +02:00
namespace Respect\Validation\Rules;
class PrivClass
{
private $bar = 'foo';
}
2010-12-06 02:17:12 +01:00
class AttributeTest extends \PHPUnit_Framework_TestCase
2010-09-27 23:02:30 +02:00
{
public function testAttributeWithNoExtraValidationShouldCheckItsPresence()
2010-09-27 23:02:30 +02:00
{
2010-12-06 02:17:12 +01:00
$validator = new Attribute('bar');
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
2010-09-27 23:02:30 +02:00
$obj->bar = 'foo';
$this->assertTrue($validator->check($obj));
$this->assertTrue($validator->__invoke($obj));
2010-09-27 23:02:30 +02:00
$this->assertTrue($validator->assert($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\AttributeException
2010-09-27 23:02:30 +02:00
*/
public function testAbsentAttributeShouldRaiseAttributeException()
2010-09-27 23:02:30 +02:00
{
2010-12-06 02:17:12 +01:00
$validator = new Attribute('bar');
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
2010-09-27 23:02:30 +02:00
$obj->baraaaaa = 'foo';
$this->assertFalse($validator->__invoke($obj));
$this->assertFalse($validator->assert($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testAbsentAttributeShouldRaiseAttributeException_on_check()
{
$validator = new Attribute('bar');
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
$obj->baraaaaa = 'foo';
$this->assertFalse($validator->__invoke($obj));
$this->assertFalse($validator->check($obj));
2010-09-27 23:02:30 +02:00
}
/**
2013-02-26 00:15:23 +01:00
* @dataProvider providerForInvalidAttributeNames
* @expectedException Respect\Validation\Exceptions\ComponentException
*/
public function testInvalidConstructorArgumentsShouldThrowComponentException($attributeName)
{
2010-12-06 02:17:12 +01:00
$validator = new Attribute($attributeName);
}
2013-02-26 00:15:23 +01:00
public function providerForInvalidAttributeNames()
{
return array(
2015-06-08 16:47:14 +02:00
array(new \stdClass()),
array(123),
2015-06-08 16:47:14 +02:00
array(''),
);
}
public function testExtraValidatorRulesForAttribute()
2010-09-27 23:02:30 +02:00
{
$subValidator = new Length(1, 3);
2010-12-06 02:17:12 +01:00
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
2010-09-27 23:02:30 +02:00
$obj->bar = 'foo';
$this->assertTrue($validator->__invoke($obj));
2010-09-27 23:02:30 +02:00
$this->assertTrue($validator->assert($obj));
$this->assertTrue($validator->check($obj));
$this->assertTrue($validator->__invoke(''));
$this->assertTrue($validator->assert(''));
$this->assertTrue($validator->check(''));
2010-09-27 23:02:30 +02:00
}
public function testExtraValidatorRulesForAttribute_should_fail_if_invalid()
{
$subValidator = new Length(1, 3);
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
$obj->bar = 'foo hey this has more than 3 chars';
$this->assertFalse($validator->__invoke($obj));
}
/**
* @expectedException Respect\Validation\Exceptions\LengthException
*/
public function testExtraValidatorRulesForAttribute_should_raise_extra_validator_exception_on_check()
{
$subValidator = new Length(1, 3);
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$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 testExtraValidatorRulesForAttribute_should_raise_AttributeException_on_assert()
{
$subValidator = new Length(1, 3);
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
$obj->bar = 'foo hey this has more than 3 chars';
$this->assertFalse($validator->assert($obj));
}
2010-09-27 23:02:30 +02:00
public function testNotMandatoryAttributeShouldNotFailWhenAttributeIsAbsent()
{
$validator = new Attribute('bar', null, false);
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
$this->assertTrue($validator->__invoke($obj));
}
public function testNotMandatoryAttributeShouldNotFailWhenAttributeIsAbsent_with_extra_validator()
{
$subValidator = new Length(1, 3);
$validator = new Attribute('bar', $subValidator, false);
2015-06-08 16:47:14 +02:00
$obj = new \stdClass();
$this->assertTrue($validator->__invoke($obj));
}
public function testPrivateAttributeShouldAlsoBeChecked()
{
$subValidator = new Length(1, 3);
2010-12-06 02:17:12 +01:00
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$obj = new PrivClass();
$this->assertTrue($validator->assert($obj));
}
public function testPrivateAttributeShouldFailIfNotValid()
{
$subValidator = new Length(33333, 888888);
$validator = new Attribute('bar', $subValidator);
2015-06-08 16:47:14 +02:00
$obj = new PrivClass();
$this->assertFalse($validator->__invoke($obj));
}
}