-
Notifications
You must be signed in to change notification settings - Fork 137
added test for InlineValidator #294
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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() | ||
{ | ||
$inlineValidator = new InlineValidator($this->container, $this->constraintValidatorFactory); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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… There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but please lets do this in another PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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…
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).