|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
15 | 15 | use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
16 | 18 |
|
17 | 19 | class LoggingTranslatorPassTest extends TestCase |
18 | 20 | { |
19 | 21 | public function testProcess() |
20 | 22 | { |
21 | | - $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); |
22 | | - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
23 | | - $parameterBag = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface')->getMock(); |
24 | | - |
25 | | - $container->expects($this->exactly(2)) |
26 | | - ->method('hasAlias') |
27 | | - ->will($this->returnValue(true)); |
28 | | - |
29 | | - $container->expects($this->once()) |
30 | | - ->method('getParameter') |
31 | | - ->will($this->returnValue(true)); |
32 | | - |
33 | | - $container->expects($this->once()) |
34 | | - ->method('getAlias') |
35 | | - ->will($this->returnValue('translation.default')); |
36 | | - |
37 | | - $container->expects($this->exactly(3)) |
38 | | - ->method('getDefinition') |
39 | | - ->will($this->returnValue($definition)); |
40 | | - |
41 | | - $container->expects($this->once()) |
42 | | - ->method('hasParameter') |
43 | | - ->with('translator.logging') |
44 | | - ->will($this->returnValue(true)); |
45 | | - |
46 | | - $definition->expects($this->once()) |
47 | | - ->method('getClass') |
48 | | - ->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Translation\Translator')); |
49 | | - |
50 | | - $parameterBag->expects($this->once()) |
51 | | - ->method('resolveValue') |
52 | | - ->will($this->returnValue("Symfony\Bundle\FrameworkBundle\Translation\Translator")); |
53 | | - |
54 | | - $container->expects($this->once()) |
55 | | - ->method('getParameterBag') |
56 | | - ->will($this->returnValue($parameterBag)); |
57 | | - |
58 | | - $container->expects($this->once()) |
59 | | - ->method('getReflectionClass') |
60 | | - ->with('Symfony\Bundle\FrameworkBundle\Translation\Translator') |
61 | | - ->will($this->returnValue(new \ReflectionClass('Symfony\Bundle\FrameworkBundle\Translation\Translator'))); |
62 | | - |
63 | | - $definition->expects($this->once()) |
64 | | - ->method('getTag') |
65 | | - ->with('container.service_subscriber') |
66 | | - ->willReturn(array(array('id' => 'translator'), array('id' => 'foo'))); |
67 | | - |
68 | | - $definition->expects($this->once()) |
69 | | - ->method('clearTag') |
70 | | - ->with('container.service_subscriber'); |
71 | | - |
72 | | - $definition->expects($this->any()) |
73 | | - ->method('addTag') |
74 | | - ->withConsecutive( |
75 | | - array('container.service_subscriber', array('id' => 'foo')), |
76 | | - array('container.service_subscriber', array('key' => 'translator', 'id' => 'translator.logging.inner')) |
77 | | - ); |
| 23 | + $container = new ContainerBuilder(); |
| 24 | + $container->setParameter('translator.logging', true); |
| 25 | + $container->setParameter('translator.class', 'Symfony\Component\Translation\Translator'); |
| 26 | + $container->register('monolog.logger'); |
| 27 | + $container->setAlias('logger', 'monolog.logger'); |
| 28 | + $container->register('translator.default', '%translator.class%'); |
| 29 | + $container->register('translator.logging', '%translator.class%'); |
| 30 | + $container->setAlias('translator', 'translator.default'); |
| 31 | + $translationWarmerDefinition = $container->register('translation.warmer') |
| 32 | + ->addArgument(new Reference('translator')) |
| 33 | + ->addTag('container.service_subscriber', array('id' => 'translator')) |
| 34 | + ->addTag('container.service_subscriber', array('id' => 'foo')); |
78 | 35 |
|
79 | 36 | $pass = new LoggingTranslatorPass(); |
80 | 37 | $pass->process($container); |
| 38 | + |
| 39 | + $this->assertEquals( |
| 40 | + array('container.service_subscriber' => array( |
| 41 | + array('id' => 'foo'), |
| 42 | + array('key' => 'translator', 'id' => 'translator.logging.inner'), |
| 43 | + )), |
| 44 | + $translationWarmerDefinition->getTags() |
| 45 | + ); |
81 | 46 | } |
82 | 47 |
|
83 | 48 | public function testThatCompilerPassIsIgnoredIfThereIsNotLoggerDefinition() |
84 | 49 | { |
85 | | - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
86 | | - $container->expects($this->once()) |
87 | | - ->method('hasAlias') |
88 | | - ->will($this->returnValue(false)); |
| 50 | + $container = new ContainerBuilder(); |
| 51 | + $container->register('identity_translator'); |
| 52 | + $container->setAlias('translator', 'identity_translator'); |
| 53 | + |
| 54 | + $definitionsBefore = count($container->getDefinitions()); |
| 55 | + $aliasesBefore = count($container->getAliases()); |
89 | 56 |
|
90 | 57 | $pass = new LoggingTranslatorPass(); |
91 | 58 | $pass->process($container); |
| 59 | + |
| 60 | + // the container is untouched (i.e. no new definitions or aliases) |
| 61 | + $this->assertCount($definitionsBefore, $container->getDefinitions()); |
| 62 | + $this->assertCount($aliasesBefore, $container->getAliases()); |
92 | 63 | } |
93 | 64 |
|
94 | 65 | public function testThatCompilerPassIsIgnoredIfThereIsNotTranslatorDefinition() |
95 | 66 | { |
96 | | - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
97 | | - $container->expects($this->at(0)) |
98 | | - ->method('hasAlias') |
99 | | - ->will($this->returnValue(true)); |
| 67 | + $container = new ContainerBuilder(); |
| 68 | + $container->register('monolog.logger'); |
| 69 | + $container->setAlias('logger', 'monolog.logger'); |
100 | 70 |
|
101 | | - $container->expects($this->at(0)) |
102 | | - ->method('hasAlias') |
103 | | - ->will($this->returnValue(false)); |
| 71 | + $definitionsBefore = count($container->getDefinitions()); |
| 72 | + $aliasesBefore = count($container->getAliases()); |
104 | 73 |
|
105 | 74 | $pass = new LoggingTranslatorPass(); |
106 | 75 | $pass->process($container); |
| 76 | + |
| 77 | + // the container is untouched (i.e. no new definitions or aliases) |
| 78 | + $this->assertCount($definitionsBefore, $container->getDefinitions()); |
| 79 | + $this->assertCount($aliasesBefore, $container->getAliases()); |
107 | 80 | } |
108 | 81 | } |
0 commit comments