From 56c83eb1c48b1cd87bfc771c7acab4d78d71b9d9 Mon Sep 17 00:00:00 2001 From: Ahmet Akbana Date: Tue, 7 Jun 2016 16:24:53 +0200 Subject: [PATCH] added test for InlineValidator --- .../Bundle/Validator/FooValidatorService.php | 33 ++++ Tests/Validator/InlineValidatorTest.php | 142 ++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 Tests/Fixtures/Bundle/Validator/FooValidatorService.php create mode 100644 Tests/Validator/InlineValidatorTest.php diff --git a/Tests/Fixtures/Bundle/Validator/FooValidatorService.php b/Tests/Fixtures/Bundle/Validator/FooValidatorService.php new file mode 100644 index 00000000..bc3c651e --- /dev/null +++ b/Tests/Fixtures/Bundle/Validator/FooValidatorService.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\CoreBundle\Tests\Fixtures\Bundle\Validator; + +use Sonata\CoreBundle\Validator\ErrorElement; +use Symfony\Component\Validator\Exception\ValidatorException; + +/** + * Validator service to create exception for test. + * + * @author Ahmet Akbana + */ +final class FooValidatorService +{ + /** + * @param ErrorElement $errorElement + * @param string $value + * @throws ValidatorException + */ + public function fooValidatorMethod(ErrorElement $errorElement, $value) + { + throw new ValidatorException($errorElement->getSubject().' is equal to '.$value); + } +} diff --git a/Tests/Validator/InlineValidatorTest.php b/Tests/Validator/InlineValidatorTest.php new file mode 100644 index 00000000..0d7e9b88 --- /dev/null +++ b/Tests/Validator/InlineValidatorTest.php @@ -0,0 +1,142 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\CoreBundle\Tests\Validator; + +use Sonata\CoreBundle\Tests\Fixtures\Bundle\Validator\FooValidatorService; +use Sonata\CoreBundle\Validator\ErrorElement; +use Sonata\CoreBundle\Validator\InlineValidator; +use Symfony\Component\Validator\Exception\ValidatorException; + +/** + * @author Ahmet Akbana + */ +final class InlineValidatorTest extends \PHPUnit_Framework_TestCase +{ + private $container; + private $constraintValidatorFactory; + private $context; + + public function setUp() + { + $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $this->constraintValidatorFactory = $this->getMock( + 'Symfony\Component\Validator\ConstraintValidatorFactoryInterface' + ); + $this->context = $this->getMock( + interface_exists('Symfony\Component\Validator\Context\ExecutionContextInterface') ? + 'Symfony\Component\Validator\Context\ExecutionContextInterface' : + 'Symfony\Component\Validator\ExecutionContextInterface' + ); + } + + public function testGetErrorElement() + { + $inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory); + + $inlineValidator->initialize($this->context); + + $reflectorObject = new \ReflectionObject($inlineValidator); + $reflectedMethod = $reflectorObject->getMethod('getErrorElement'); + $reflectedMethod->setAccessible(true); + + $errorElement = $reflectedMethod->invokeArgs($inlineValidator, array('foo')); + + $this->assertInstanceOf('Sonata\CoreBundle\Validator\ErrorElement', $errorElement); + $this->assertSame('foo', $errorElement->getSubject()); + } + + public function testValidateWithConstraintIsClosure() + { + $this->setExpectedException('Symfony\Component\Validator\Exception\ValidatorException', 'foo is equal to foo'); + + $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array('isClosure', 'getClosure')); + + $constraint->expects($this->once()) + ->method('isClosure') + ->willReturn(true); + + $constraint->expects($this->once()) + ->method('getClosure') + ->willReturn(function (ErrorElement $errorElement, $value) { + throw new ValidatorException($errorElement->getSubject().' is equal to '.$value); + }); + + $inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory); + + $inlineValidator->initialize($this->context); + + $inlineValidator->validate('foo', $constraint); + } + + public function testValidateWithConstraintGetServiceIsString() + { + $this->setExpectedException('Symfony\Component\Validator\Exception\ValidatorException', 'foo is equal to foo'); + + $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array( + 'isClosure', + 'getService', + 'getMethod', + )); + + $constraint->expects($this->once()) + ->method('isClosure') + ->willReturn(false); + + $constraint->expects($this->any()) + ->method('getService') + ->willReturn('string'); + + $constraint->expects($this->once()) + ->method('getMethod') + ->willReturn('fooValidatorMethod'); + + $this->container->expects($this->once()) + ->method('get') + ->with('string') + ->willReturn(new FooValidatorService()); + + $inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory); + + $inlineValidator->initialize($this->context); + + $inlineValidator->validate('foo', $constraint); + } + + public function testValidateWithConstraintGetServiceIsNotString() + { + $this->setExpectedException('Symfony\Component\Validator\Exception\ValidatorException', 'foo is equal to foo'); + + $constraint = $this->getMock('Symfony\Component\Validator\Constraint', array( + 'isClosure', + 'getService', + 'getMethod', + )); + + $constraint->expects($this->once()) + ->method('isClosure') + ->willReturn(false); + + $constraint->expects($this->any()) + ->method('getService') + ->willReturn(new FooValidatorService()); + + $constraint->expects($this->once()) + ->method('getMethod') + ->willReturn('fooValidatorMethod'); + + $inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory); + + $inlineValidator->initialize($this->context); + + $inlineValidator->validate('foo', $constraint); + } +}