mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
Complete tests for "AbstractRule" class
This commit is contained in:
parent
4a0ca6d8b7
commit
f0d249346f
|
@ -17,14 +17,7 @@ use Respect\Validation\Validatable;
|
|||
abstract class AbstractRule implements Validatable
|
||||
{
|
||||
protected $name;
|
||||
protected $template = null;
|
||||
|
||||
public static $translator = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//a constructor is required for ReflectionClass::newInstance()
|
||||
}
|
||||
protected $template;
|
||||
|
||||
public function __invoke($input)
|
||||
{
|
||||
|
@ -83,10 +76,10 @@ abstract class AbstractRule implements Validatable
|
|||
|
||||
protected function createException()
|
||||
{
|
||||
$currentFQN = get_called_class();
|
||||
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
|
||||
$exceptionFQN .= 'Exception';
|
||||
$currentFqn = get_called_class();
|
||||
$exceptionFqn = str_replace('\\Rules\\', '\\Exceptions\\', $currentFqn);
|
||||
$exceptionFqn .= 'Exception';
|
||||
|
||||
return new $exceptionFQN();
|
||||
return new $exceptionFqn();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,23 +11,15 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
class AbstractRuleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var Respect\Validation\Rules\AbstractRule */
|
||||
protected $abstractRuleMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->abstractRuleMock = $this->getMockForAbstractClass(
|
||||
'\\Respect\\Validation\\Rules\\AbstractRule'
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForTrueAndFalse()
|
||||
{
|
||||
return [
|
||||
[true],
|
||||
[false]
|
||||
[false],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -37,18 +29,23 @@ class AbstractRuleTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public function testMagicMethodInvokeCallsValidateWithInput($booleanResult)
|
||||
{
|
||||
$this->abstractRuleMock->expects($this->once())
|
||||
->method('validate')
|
||||
->with('something')
|
||||
->will($this->returnValue($booleanResult));
|
||||
$input = 'something';
|
||||
|
||||
// Getting a reference outside the property in order to invoke it
|
||||
$abstractRuleInstance = $this->abstractRuleMock;
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMethods(['validate'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('validate')
|
||||
->with($input)
|
||||
->will($this->returnValue($booleanResult));
|
||||
|
||||
$this->assertEquals(
|
||||
$booleanResult,
|
||||
// Invoking it to trigger __invoke
|
||||
$abstractRuleInstance('something'),
|
||||
$abstractRuleMock($input),
|
||||
'When invoking an instance of AbstractRule, the method validate should be called with the same input and return the same result.'
|
||||
);
|
||||
}
|
||||
|
@ -56,15 +53,171 @@ class AbstractRuleTest extends \PHPUnit_Framework_TestCase
|
|||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::assert
|
||||
*/
|
||||
public function testAssertInvokesValidatorOnSuccess()
|
||||
public function testAssertInvokesValidateOnSuccess()
|
||||
{
|
||||
$this->abstractRuleMock->expects($this->any())
|
||||
->method('validate')
|
||||
->with('something')
|
||||
->will($this->returnValue(true));
|
||||
$input = 'something';
|
||||
|
||||
$this->assertTrue(
|
||||
$this->abstractRuleMock->assert('something')
|
||||
);
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMethods(['validate'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('validate')
|
||||
->with($input)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->never())
|
||||
->method('reportError');
|
||||
|
||||
$abstractRuleMock->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::assert
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testAssertInvokesValidateAndReportErrorOnFailure()
|
||||
{
|
||||
$input = 'something';
|
||||
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMethods(['validate', 'reportError'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('validate')
|
||||
->with($input)
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('reportError')
|
||||
->with($input)
|
||||
->will($this->throwException(new ValidationException()));
|
||||
|
||||
$abstractRuleMock->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::check
|
||||
*/
|
||||
public function testCheckInvokesAssertToPerformTheValidationByDefault()
|
||||
{
|
||||
$input = 'something';
|
||||
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMethods(['assert'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('assert')
|
||||
->with($input);
|
||||
|
||||
$abstractRuleMock->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::reportError
|
||||
* @covers Respect\Validation\Rules\AbstractRule::createException
|
||||
*/
|
||||
public function testShouldCreateExceptionBasedOnTheCurrentClassName()
|
||||
{
|
||||
if (defined('HHVM_VERSION')) {
|
||||
return $this->markTestSkipped('If you are a HHVM user, and you are in the mood, please fix it');
|
||||
}
|
||||
|
||||
$exceptionMock = $this
|
||||
->getMockBuilder('Respect\Validation\Exceptions\ValidationException')
|
||||
->setMockClassName('MockRule1Exception')
|
||||
->getMock();
|
||||
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMockClassName('MockRule1')
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$exception = $abstractRuleMock->reportError('something');
|
||||
|
||||
$this->assertInstanceOf(get_class($exceptionMock), $exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::reportError
|
||||
* @covers Respect\Validation\Rules\AbstractRule::setTemplate
|
||||
*/
|
||||
public function testShouldUseDefinedTemplateOnCreatedException()
|
||||
{
|
||||
$template = 'This is my template';
|
||||
|
||||
$exceptionMock = $this
|
||||
->getMockBuilder('Respect\Validation\Exceptions\ValidationException')
|
||||
->setMethods(['setTemplate'])
|
||||
->getMock();
|
||||
|
||||
$exceptionMock
|
||||
->expects($this->once())
|
||||
->method('setTemplate')
|
||||
->with($template);
|
||||
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->setMethods(['createException'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$abstractRuleMock
|
||||
->expects($this->once())
|
||||
->method('createException')
|
||||
->will($this->returnValue($exceptionMock));
|
||||
|
||||
$abstractRuleMock->setTemplate($template);
|
||||
$abstractRuleMock->reportError('something');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::setTemplate
|
||||
*/
|
||||
public function testShouldReturnTheCurrentObjectWhenDefinigTemplate()
|
||||
{
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$this->assertSame($abstractRuleMock, $abstractRuleMock->setTemplate('whatever'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::setName
|
||||
*/
|
||||
public function testShouldReturnTheCurrentObjectWhenDefinigName()
|
||||
{
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$this->assertSame($abstractRuleMock, $abstractRuleMock->setName('whatever'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Respect\Validation\Rules\AbstractRule::setName
|
||||
* @covers Respect\Validation\Rules\AbstractRule::getName
|
||||
*/
|
||||
public function testShouldBeAbleToDefineAndRetrivedRuleName()
|
||||
{
|
||||
$abstractRuleMock = $this
|
||||
->getMockBuilder('Respect\Validation\Rules\AbstractRule')
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$name = 'something';
|
||||
|
||||
$abstractRuleMock->setName($name);
|
||||
|
||||
$this->assertSame($name, $abstractRuleMock->getName());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue