|
| 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 | +namespace Symfony\Component\Serializer\Tests\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Reference; |
| 15 | +use Symfony\Component\Serializer\DependencyInjection\SerializerPass; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for the SerializerPass class. |
| 19 | + * |
| 20 | + * @author Javier Lopez <[email protected]> |
| 21 | + */ |
| 22 | +class SerializerPassTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + public function testThrowExceptionWhenNoNormalizers() |
| 25 | + { |
| 26 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock(); |
| 27 | + |
| 28 | + $container->expects($this->once()) |
| 29 | + ->method('hasDefinition') |
| 30 | + ->with('serializer') |
| 31 | + ->will($this->returnValue(true)); |
| 32 | + |
| 33 | + $container->expects($this->once()) |
| 34 | + ->method('findTaggedServiceIds') |
| 35 | + ->with('serializer.normalizer') |
| 36 | + ->will($this->returnValue(array())); |
| 37 | + |
| 38 | + $this->setExpectedException('RuntimeException'); |
| 39 | + |
| 40 | + $serializerPass = new SerializerPass(); |
| 41 | + $serializerPass->process($container); |
| 42 | + } |
| 43 | + |
| 44 | + public function testThrowExceptionWhenNoEncoders() |
| 45 | + { |
| 46 | + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); |
| 47 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); |
| 48 | + |
| 49 | + $container->expects($this->once()) |
| 50 | + ->method('hasDefinition') |
| 51 | + ->with('serializer') |
| 52 | + ->will($this->returnValue(true)); |
| 53 | + |
| 54 | + $container->expects($this->any()) |
| 55 | + ->method('findTaggedServiceIds') |
| 56 | + ->will($this->onConsecutiveCalls( |
| 57 | + array('n' => array('serializer.normalizer')), |
| 58 | + array() |
| 59 | + )); |
| 60 | + |
| 61 | + $container->expects($this->once()) |
| 62 | + ->method('getDefinition') |
| 63 | + ->will($this->returnValue($definition)); |
| 64 | + |
| 65 | + $this->setExpectedException('RuntimeException'); |
| 66 | + |
| 67 | + $serializerPass = new SerializerPass(); |
| 68 | + $serializerPass->process($container); |
| 69 | + } |
| 70 | + |
| 71 | + public function testServicesAreOrderedAccordingToPriority() |
| 72 | + { |
| 73 | + $services = array( |
| 74 | + 'n3' => array('tag' => array()), |
| 75 | + 'n1' => array('tag' => array('priority' => 200)), |
| 76 | + 'n2' => array('tag' => array('priority' => 100)), |
| 77 | + ); |
| 78 | + |
| 79 | + $expected = array( |
| 80 | + new Reference('n1'), |
| 81 | + new Reference('n2'), |
| 82 | + new Reference('n3'), |
| 83 | + ); |
| 84 | + |
| 85 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock(); |
| 86 | + |
| 87 | + $container->expects($this->any()) |
| 88 | + ->method('findTaggedServiceIds') |
| 89 | + ->will($this->returnValue($services)); |
| 90 | + |
| 91 | + $serializerPass = new SerializerPass(); |
| 92 | + |
| 93 | + $method = new \ReflectionMethod( |
| 94 | + SerializerPass::class, |
| 95 | + 'findAndSortTaggedServices' |
| 96 | + ); |
| 97 | + $method->setAccessible(true); |
| 98 | + |
| 99 | + $actual = $method->invoke($serializerPass, 'tag', $container); |
| 100 | + |
| 101 | + $this->assertEquals($expected, $actual); |
| 102 | + } |
| 103 | +} |
0 commit comments