|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass; |
| 13 | + |
| 14 | +class AddConstraintValidatorsPassTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + public function testThatConstraintValidatorServicesAreProcessed() |
| 17 | + { |
| 18 | + $services = array( |
| 19 | + 'my_constraint_validator_service1' => array(0 => array('alias' => 'my_constraint_validator_alias1')), |
| 20 | + 'my_constraint_validator_service2' => array(), |
| 21 | + ); |
| 22 | + |
| 23 | + $validatorFactoryDefinition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); |
| 24 | + $container = $this->getMock( |
| 25 | + 'Symfony\Component\DependencyInjection\ContainerBuilder', |
| 26 | + array('findTaggedServiceIds', 'getDefinition', 'hasDefinition') |
| 27 | + ); |
| 28 | + |
| 29 | + $validatorDefinition1 = $this->getMock('Symfony\Component\DependencyInjection\Definition', array('getClass')); |
| 30 | + $validatorDefinition2 = $this->getMock('Symfony\Component\DependencyInjection\Definition', array('getClass')); |
| 31 | + |
| 32 | + $validatorDefinition1->expects($this->atLeastOnce()) |
| 33 | + ->method('getClass') |
| 34 | + ->willReturn('My\Fully\Qualified\Class\Named\Validator1'); |
| 35 | + $validatorDefinition2->expects($this->atLeastOnce()) |
| 36 | + ->method('getClass') |
| 37 | + ->willReturn('My\Fully\Qualified\Class\Named\Validator2'); |
| 38 | + |
| 39 | + $container->expects($this->any()) |
| 40 | + ->method('getDefinition') |
| 41 | + ->with($this->anything()) |
| 42 | + ->will($this->returnValueMap(array( |
| 43 | + array('my_constraint_validator_service1', $validatorDefinition1), |
| 44 | + array('my_constraint_validator_service2', $validatorDefinition2), |
| 45 | + array('validator.validator_factory', $validatorFactoryDefinition), |
| 46 | + ))); |
| 47 | + |
| 48 | + $container->expects($this->atLeastOnce()) |
| 49 | + ->method('findTaggedServiceIds') |
| 50 | + ->will($this->returnValue($services)); |
| 51 | + $container->expects($this->atLeastOnce()) |
| 52 | + ->method('hasDefinition') |
| 53 | + ->with('validator.validator_factory') |
| 54 | + ->will($this->returnValue(true)); |
| 55 | + |
| 56 | + $validatorFactoryDefinition->expects($this->once()) |
| 57 | + ->method('replaceArgument') |
| 58 | + ->with(1, array( |
| 59 | + 'My\Fully\Qualified\Class\Named\Validator1' => 'my_constraint_validator_service1', |
| 60 | + 'my_constraint_validator_alias1' => 'my_constraint_validator_service1', |
| 61 | + 'My\Fully\Qualified\Class\Named\Validator2' => 'my_constraint_validator_service2', |
| 62 | + )); |
| 63 | + |
| 64 | + $addConstraintValidatorsPass = new AddConstraintValidatorsPass(); |
| 65 | + $addConstraintValidatorsPass->process($container); |
| 66 | + } |
| 67 | + |
| 68 | + public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition() |
| 69 | + { |
| 70 | + $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); |
| 71 | + $container = $this->getMock( |
| 72 | + 'Symfony\Component\DependencyInjection\ContainerBuilder', |
| 73 | + array('hasDefinition', 'findTaggedServiceIds', 'getDefinition') |
| 74 | + ); |
| 75 | + |
| 76 | + $container->expects($this->never())->method('findTaggedServiceIds'); |
| 77 | + $container->expects($this->never())->method('getDefinition'); |
| 78 | + $container->expects($this->atLeastOnce()) |
| 79 | + ->method('hasDefinition') |
| 80 | + ->with('validator.validator_factory') |
| 81 | + ->will($this->returnValue(false)); |
| 82 | + $definition->expects($this->never())->method('replaceArgument'); |
| 83 | + |
| 84 | + $addCacheWarmerPass = new AddConstraintValidatorsPass(); |
| 85 | + $addCacheWarmerPass->process($container); |
| 86 | + } |
| 87 | +} |
0 commit comments