|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Bundle\UiBundle\Tests\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Knp\Menu\ItemInterface; |
|
15
|
|
|
use Lug\Bundle\UiBundle\DependencyInjection\Compiler\RegisterMenuPass; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author GeLo <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class RegisterMenuPassTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var RegisterMenuPass |
|
28
|
|
|
*/ |
|
29
|
|
|
private $compiler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->compiler = new RegisterMenuPass(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testInheritance() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->assertInstanceOf(CompilerPassInterface::class, $this->compiler); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testProcess() |
|
45
|
|
|
{ |
|
46
|
|
|
$container = $this->createContainerBuilderMock(); |
|
47
|
|
|
$container |
|
|
|
|
|
|
48
|
|
|
->expects($this->once()) |
|
49
|
|
|
->method('findTaggedServiceIds') |
|
50
|
|
|
->with($this->identicalTo('lug.menu.builder')) |
|
51
|
|
|
->will($this->returnValue([$service = 'my.service' => [['alias' => $alias = 'my.alias']]])); |
|
52
|
|
|
|
|
53
|
|
|
$container |
|
54
|
|
|
->expects($this->once()) |
|
55
|
|
|
->method('setDefinition') |
|
56
|
|
|
->with( |
|
57
|
|
|
$this->identicalTo($service.'.menu'), |
|
58
|
|
|
$this->callback(function (Definition $definition) use ($service, $alias) { |
|
59
|
|
|
return $definition->getClass() === ItemInterface::class |
|
60
|
|
|
&& $definition->getTag('knp_menu.menu') === [['alias' => $alias]] |
|
61
|
|
|
&& is_array($factory = $definition->getFactory()) |
|
62
|
|
|
&& isset($factory[0]) |
|
63
|
|
|
&& $factory[0] instanceof Reference |
|
64
|
|
|
&& (string) $factory[0] === $service |
|
65
|
|
|
&& isset($factory[1]) |
|
66
|
|
|
&& $factory[1] === 'create'; |
|
67
|
|
|
}) |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$this->compiler->process($container); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @expectedException \Lug\Bundle\UiBundle\Exception\TagAttributeNotFoundException |
|
75
|
|
|
* @expectedExceptionMessage The attribute "alias" could not be found for the tag "lug.menu.builder" on the "my.service" service. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testProcessWithoutAlias() |
|
78
|
|
|
{ |
|
79
|
|
|
$container = $this->createContainerBuilderMock(); |
|
80
|
|
|
$container |
|
|
|
|
|
|
81
|
|
|
->expects($this->once()) |
|
82
|
|
|
->method('findTaggedServiceIds') |
|
83
|
|
|
->with($this->identicalTo('lug.menu.builder')) |
|
84
|
|
|
->will($this->returnValue([$service = 'my.service' => [[]]])); |
|
85
|
|
|
|
|
86
|
|
|
$this->compiler->process($container); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder |
|
91
|
|
|
*/ |
|
92
|
|
|
private function createContainerBuilderMock() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->getMockBuilder(ContainerBuilder::class) |
|
95
|
|
|
->setMethods(['findTaggedServiceIds', 'setDefinition']) |
|
96
|
|
|
->getMock(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: