AddConstraintValidatorsPassTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. class AddConstraintValidatorsPassTest extends TestCase
  16. {
  17. public function testThatConstraintValidatorServicesAreProcessed()
  18. {
  19. $container = new ContainerBuilder();
  20. $constraintValidatorFactoryDefinition = $container->register('validator.validator_factory')
  21. ->setArguments(array(new Reference('service_container'), array()));
  22. $container->register('my_constraint_validator_service1', 'My\Fully\Qualified\Class\Named\Validator1')
  23. ->addTag('validator.constraint_validator', array('alias' => 'my_constraint_validator_alias1'));
  24. $container->register('my_constraint_validator_service2', 'My\Fully\Qualified\Class\Named\Validator2')
  25. ->addTag('validator.constraint_validator');
  26. $addConstraintValidatorsPass = new AddConstraintValidatorsPass();
  27. $addConstraintValidatorsPass->process($container);
  28. $this->assertEquals(
  29. array(
  30. 'My\Fully\Qualified\Class\Named\Validator1' => 'my_constraint_validator_service1',
  31. 'my_constraint_validator_alias1' => 'my_constraint_validator_service1',
  32. 'My\Fully\Qualified\Class\Named\Validator2' => 'my_constraint_validator_service2',
  33. ),
  34. $constraintValidatorFactoryDefinition->getArgument(1)
  35. );
  36. }
  37. public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition()
  38. {
  39. $addConstraintValidatorsPass = new AddConstraintValidatorsPass();
  40. $addConstraintValidatorsPass->process(new ContainerBuilder());
  41. // we just check that the pass does not fail if no constraint validator factory is registered
  42. $this->addToAssertionCount(1);
  43. }
  44. }