|
3 | 3 | namespace Knp\Bundle\MenuBundle\Tests\DependencyInjection\Compiler;
|
4 | 4 |
|
5 | 5 | use Knp\Bundle\MenuBundle\DependencyInjection\Compiler\AddExtensionsPass;
|
6 |
| -use Knp\Menu\FactoryInterface; |
7 |
| -use Knp\Menu\ItemInterface; |
| 6 | +use Knp\Menu\MenuFactory; |
8 | 7 | use PHPUnit\Framework\TestCase;
|
9 | 8 | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
| 9 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
10 | 10 | use Symfony\Component\DependencyInjection\Reference;
|
11 | 11 |
|
12 | 12 | class AddExtensionsPassTest extends TestCase
|
13 | 13 | {
|
14 | 14 | public function testProcessWithoutProviderDefinition()
|
15 | 15 | {
|
16 |
| - $containerBuilder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
17 |
| - $containerBuilder->expects($this->once()) |
18 |
| - ->method('has') |
19 |
| - ->willReturn(false); |
20 |
| - $containerBuilder->expects($this->never()) |
21 |
| - ->method('findTaggedServiceIds'); |
| 16 | + $containerBuilder = new ContainerBuilder(); |
| 17 | + (new AddExtensionsPass())->process($containerBuilder); |
22 | 18 |
|
23 |
| - $menuPass = new AddExtensionsPass(); |
24 |
| - |
25 |
| - $menuPass->process($containerBuilder); |
| 19 | + self::assertFalse($containerBuilder->has('knp_menu.factory')); |
26 | 20 | }
|
27 | 21 |
|
28 | 22 | public function testProcessWithAlias()
|
29 | 23 | {
|
30 |
| - $menuFactoryClass = 'Knp\Bundle\MenuBundle\Tests\DependencyInjection\Compiler\MenuFactoryMock'; |
| 24 | + $containerBuilder = new ContainerBuilder(); |
| 25 | + $containerBuilder->register('knp_menu.factory', MenuFactory::class); |
31 | 26 |
|
32 |
| - $definitionMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition') |
33 |
| - ->disableOriginalConstructor() |
34 |
| - ->getMock(); |
35 |
| - $definitionMock->expects($this->at(0)) |
36 |
| - ->method('getClass') |
37 |
| - ->willReturn($menuFactoryClass); |
38 |
| - $definitionMock->expects($this->at(1)) |
39 |
| - ->method('addMethodCall') |
40 |
| - ->with($this->equalTo('addExtension'), $this->equalTo([new Reference('id'), 0])); |
41 |
| - $definitionMock->expects($this->at(2)) |
42 |
| - ->method('addMethodCall') |
43 |
| - ->with($this->equalTo('addExtension'), $this->equalTo([new Reference('id'), 12])); |
44 |
| - $definitionMock->expects($this->at(3)) |
45 |
| - ->method('addMethodCall') |
46 |
| - ->with($this->equalTo('addExtension'), $this->equalTo([new Reference('foo'), -4])); |
| 27 | + $containerBuilder->register('id', 'stdClass') |
| 28 | + ->addTag('knp_menu.factory_extension') |
| 29 | + ->addTag('knp_menu.factory_extension', ['priority' => 12]); |
47 | 30 |
|
48 |
| - $parameterBagMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface')->getMock(); |
49 |
| - $parameterBagMock->expects($this->once()) |
50 |
| - ->method('resolveValue') |
51 |
| - ->with($menuFactoryClass) |
52 |
| - ->willReturn($menuFactoryClass); |
53 |
| - |
54 |
| - $containerBuilderMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
55 |
| - $containerBuilderMock->expects($this->once()) |
56 |
| - ->method('has') |
57 |
| - ->willReturn(true); |
58 |
| - $containerBuilderMock->expects($this->once()) |
59 |
| - ->method('findTaggedServiceIds') |
60 |
| - ->with($this->equalTo('knp_menu.factory_extension')) |
61 |
| - ->willReturn(['id' => ['tag1' => [], 'tag2' => ['priority' => 12]], 'foo' => ['tag1' => ['priority' => -4]]]); |
62 |
| - $containerBuilderMock->expects($this->once()) |
63 |
| - ->method('findDefinition') |
64 |
| - ->with($this->equalTo('knp_menu.factory')) |
65 |
| - ->willReturn($definitionMock); |
66 |
| - $containerBuilderMock->expects($this->once()) |
67 |
| - ->method('getParameterBag') |
68 |
| - ->willReturn($parameterBagMock); |
| 31 | + $containerBuilder->register('foo', 'stdClass') |
| 32 | + ->addTag('knp_menu.factory_extension', ['priority' => -4]); |
69 | 33 |
|
70 | 34 | $menuPass = new AddExtensionsPass();
|
71 |
| - $menuPass->process($containerBuilderMock); |
| 35 | + $menuPass->process($containerBuilder); |
| 36 | + |
| 37 | + self::assertEquals( |
| 38 | + [ |
| 39 | + ['addExtension', [new Reference('id'), 0]], |
| 40 | + ['addExtension', [new Reference('id'), 12]], |
| 41 | + ['addExtension', [new Reference('foo'), -4]], |
| 42 | + ], |
| 43 | + $containerBuilder->getDefinition('knp_menu.factory')->getMethodCalls() |
| 44 | + ); |
72 | 45 | }
|
73 | 46 |
|
74 | 47 | public function testMissingAddExtension()
|
75 | 48 | {
|
76 |
| - $definitionMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition') |
77 |
| - ->disableOriginalConstructor() |
78 |
| - ->getMock(); |
79 |
| - $definitionMock->expects($this->at(0)) |
80 |
| - ->method('getClass') |
81 |
| - ->willReturn('SimpleMenuFactory'); |
82 |
| - |
83 |
| - $parameterBagMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface')->getMock(); |
84 |
| - $parameterBagMock->expects($this->once()) |
85 |
| - ->method('resolveValue') |
86 |
| - ->with('SimpleMenuFactory') |
87 |
| - ->willReturn('SimpleMenuFactory'); |
| 49 | + $containerBuilder = new ContainerBuilder(); |
88 | 50 |
|
89 |
| - $containerBuilderMock = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); |
90 |
| - $containerBuilderMock->expects($this->once()) |
91 |
| - ->method('has') |
92 |
| - ->willReturn(true); |
93 |
| - $containerBuilderMock->expects($this->once()) |
94 |
| - ->method('findTaggedServiceIds') |
95 |
| - ->with($this->equalTo('knp_menu.factory_extension')) |
96 |
| - ->willReturn(['id' => ['tag1' => [], 'tag2' => ['priority' => 12]], 'foo' => ['tag1' => ['priority' => -4]]]); |
97 |
| - $containerBuilderMock->expects($this->once()) |
98 |
| - ->method('findDefinition') |
99 |
| - ->with($this->equalTo('knp_menu.factory')) |
100 |
| - ->willReturn($definitionMock); |
101 |
| - $containerBuilderMock->expects($this->once()) |
102 |
| - ->method('getParameterBag') |
103 |
| - ->willReturn($parameterBagMock); |
104 |
| - |
105 |
| - $this->expectException(InvalidConfigurationException::class); |
| 51 | + $containerBuilder->register('knp_menu.factory', 'SimpleMenuFactory'); |
| 52 | + $containerBuilder->register('foo', 'stdClass')->addTag('knp_menu.factory_extension'); |
106 | 53 |
|
107 | 54 | $menuPass = new AddExtensionsPass();
|
108 |
| - $menuPass->process($containerBuilderMock); |
109 |
| - } |
110 |
| -} |
111 | 55 |
|
112 |
| -class MenuFactoryMock implements FactoryInterface |
113 |
| -{ |
114 |
| - public function createItem(string $name, array $options = []): ItemInterface |
115 |
| - { |
116 |
| - } |
117 |
| - |
118 |
| - public function addExtension() |
119 |
| - { |
| 56 | + $this->expectException(InvalidConfigurationException::class); |
| 57 | + $menuPass->process($containerBuilder); |
120 | 58 | }
|
121 | 59 | }
|
0 commit comments