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

added test for InlineValidator #294

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
142 changes: 142 additions & 0 deletions Tests/Validator/InlineValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?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;

/**
* @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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of testing protected methods… you should only test them trough the public API of the object IMO : we don't really care about the implementation, only about the behavior of the system under test. If it is not simple to test like though, it might be justified…

Copy link
Contributor Author

@codebach codebach Jun 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, just trying to write test for every line which is possible. From my point of view the protected methods are not %100 encapsulated, so testing them when it is possible/easy is always beneficial.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do as you see fit, just making sure you know my point of view. If it is really a hassle to test the protected method through the public methods, then by all means, do the reflection dark magic (just my opinion though, not sure what @sonata-project/contributors think about this).

{
$inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mother of god… do all classes depend on the container or what? T___T

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mother of god… do all classes depend on the container or what? T___T

😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should have been done with tagged services or something like that…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but please lets do this in another PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but please lets do this in another PR

Sure goes without saying, this is more a call to action…


$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'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMock is deprecated in favor of createMock

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but createMock is not available under PHPUnit 4. So let it as is for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right… nevermind, then.


$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);
}
}