SerializerPassTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\SerializerPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Tests for the SerializerPass class.
  17. *
  18. * @author Javier Lopez <f12loalf@gmail.com>
  19. */
  20. class SerializerPassTest extends TestCase
  21. {
  22. /**
  23. * @expectedException \RuntimeException
  24. * @expectedExceptionMessage You must tag at least one service as "serializer.normalizer" to use the Serializer service
  25. */
  26. public function testThrowExceptionWhenNoNormalizers()
  27. {
  28. $container = new ContainerBuilder();
  29. $container->register('serializer');
  30. $serializerPass = new SerializerPass();
  31. $serializerPass->process($container);
  32. }
  33. /**
  34. * @expectedException \RuntimeException
  35. * @expectedExceptionMessage You must tag at least one service as "serializer.encoder" to use the Serializer service
  36. */
  37. public function testThrowExceptionWhenNoEncoders()
  38. {
  39. $container = new ContainerBuilder();
  40. $container->register('serializer')
  41. ->addArgument(array())
  42. ->addArgument(array());
  43. $container->register('normalizer')->addTag('serializer.normalizer');
  44. $serializerPass = new SerializerPass();
  45. $serializerPass->process($container);
  46. }
  47. public function testServicesAreOrderedAccordingToPriority()
  48. {
  49. $container = new ContainerBuilder();
  50. $serializerDefinition = $container->register('serializer')
  51. ->addArgument(array())
  52. ->addArgument(array());
  53. $container->register('normalizer3')->addTag('serializer.normalizer');
  54. $container->register('normalizer1')->addTag('serializer.normalizer', array('priority' => 200));
  55. $container->register('normalizer2')->addTag('serializer.normalizer', array('priority' => 100));
  56. $container->register('encoder')->addTag('serializer.encoder');
  57. $serializerPass = new SerializerPass();
  58. $serializerPass->process($container);
  59. $this->assertEquals(array(new Reference('normalizer1'), new Reference('normalizer2'), new Reference('normalizer3')), $serializerDefinition->getArgument(0));
  60. }
  61. }