Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
added test for InlineValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
codebach committed Jun 8, 2016
1 parent c9668d8 commit 8c0c0c3
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Tests/Fixtures/Bundle/Validator/FooValidatorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
}
144 changes: 144 additions & 0 deletions Tests/Validator/InlineValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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;

/**
* Test for InlineValidator.
*
* @author Ahmet Akbana <[email protected]>
*/
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);
}
}

0 comments on commit 8c0c0c3

Please sign in to comment.