3
3
namespace Knp \Bundle \MenuBundle \Tests \DependencyInjection \Compiler ;
4
4
5
5
use Knp \Bundle \MenuBundle \DependencyInjection \Compiler \RegisterMenusPass ;
6
+ use Knp \Menu \MenuItem ;
7
+ use Knp \Menu \Provider \LazyProvider ;
6
8
use PHPUnit \Framework \TestCase ;
7
9
use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
8
10
use Symfony \Component \DependencyInjection \ContainerBuilder ;
9
- use Symfony \Component \DependencyInjection \Definition ;
10
11
use Symfony \Component \DependencyInjection \Reference ;
11
12
12
13
class RegisterMenusPassTest extends TestCase
13
14
{
15
+ /**
16
+ * @var ContainerBuilder
17
+ */
14
18
private $ containerBuilder ;
15
- private $ definition ;
16
19
17
20
/**
18
21
* @var RegisterMenusPass
@@ -21,93 +24,94 @@ class RegisterMenusPassTest extends TestCase
21
24
22
25
protected function setUp (): void
23
26
{
24
- if (!\class_exists (ServiceClosureArgument::class)) {
25
- $ this ->markTestSkipped ('The RegisterMenuPass requires Symfony DI 3.3+. ' );
26
- }
27
+ $ this ->containerBuilder = new ContainerBuilder ();
28
+
29
+ $ this ->containerBuilder ->register ('knp_menu.menu_provider.lazy ' , LazyProvider::class)
30
+ ->setArgument (0 , null );
31
+ $ this ->containerBuilder ->register ('knp_menu.menu_provider.builder_service ' , \stdClass::class);
32
+ $ this ->containerBuilder ->register ('id ' , \stdClass::class)
33
+ ->addTag ('knp_menu.menu_builder ' , ['alias ' => 'foo ' , 'method ' => 'fooMenu ' ])
34
+ ->addTag ('knp_menu.menu_builder ' , ['alias ' => 'bar ' , 'method ' => 'bar ' ])
35
+ ->setPublic (true );
36
+ $ this ->containerBuilder ->register ('menu_id ' , MenuItem::class)
37
+ ->addTag ('knp_menu.menu ' , ['alias ' => 'baz ' ])
38
+ ->setPublic (true );
27
39
28
- $ this ->containerBuilder = $ this ->prophesize (ContainerBuilder::class);
29
- $ this ->definition = $ this ->prophesize (Definition::class);
30
40
$ this ->pass = new RegisterMenusPass ();
31
-
32
- $ this ->containerBuilder ->hasDefinition ('knp_menu.menu_provider.lazy ' )->willReturn (true );
33
- $ this ->containerBuilder ->getDefinition ('knp_menu.menu_provider.lazy ' )->willReturn ($ this ->definition );
34
-
35
- $ this ->containerBuilder ->removeDefinition ('knp_menu.menu_provider.builder_service ' )->shouldBeCalled ();
36
-
37
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->willReturn ([]);
38
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu ' , true )->willReturn ([]);
39
41
}
40
42
41
43
public function testNoopWithoutProvider (): void
42
44
{
43
- $ this ->containerBuilder ->hasDefinition ('knp_menu.menu_provider.lazy ' )-> willReturn ( false );
45
+ $ this ->containerBuilder ->removeDefinition ('knp_menu.menu_provider.lazy ' );
44
46
45
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->shouldNotBeCalled ();
46
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu ' , true )->shouldNotBeCalled ();
47
- $ this ->containerBuilder ->removeDefinition ('knp_menu.menu_provider.builder_service ' )->shouldNotBeCalled ();
47
+ $ this ->pass ->process ($ this ->containerBuilder );
48
48
49
- $ this ->pass -> process ($ this ->containerBuilder ->reveal ( ));
49
+ $ this ->assertTrue ($ this ->containerBuilder ->hasDefinition ( ' knp_menu.menu_provider.builder_service ' ));
50
50
}
51
51
52
52
public function testFailsWhenBuilderAliasIsMissing (): void
53
53
{
54
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->willReturn (['id ' => [['alias ' => '' ]]]);
54
+ $ this ->containerBuilder ->getDefinition ('id ' )
55
+ ->setTags (['knp_menu.menu_builder ' => [['alias ' => '' ]]]);
55
56
56
57
$ this ->expectException (\InvalidArgumentException::class);
57
58
$ this ->expectExceptionMessage ('The alias is not defined in the "knp_menu.menu_builder" tag for the service "id" ' );
58
- $ this ->pass ->process ($ this ->containerBuilder ->reveal ());
59
+
60
+ $ this ->pass ->process ($ this ->containerBuilder );
59
61
}
60
62
61
63
public function testFailsWhenBuilderMethodIsMissing (): void
62
64
{
63
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->willReturn (['id ' => [['alias ' => 'foo ' ]]]);
65
+ $ this ->containerBuilder ->getDefinition ('id ' )
66
+ ->setTags (['knp_menu.menu_builder ' => [['alias ' => 'foo ' ]]]);
64
67
65
68
$ this ->expectException (\InvalidArgumentException::class);
66
69
$ this ->expectExceptionMessage ('The method is not defined in the "knp_menu.menu_builder" tag for the service "id" ' );
67
- $ this ->pass ->process ($ this ->containerBuilder ->reveal ());
70
+
71
+ $ this ->pass ->process ($ this ->containerBuilder );
68
72
}
69
73
70
74
public function testFailsWhenMenuAliasIsMissing (): void
71
75
{
72
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu ' , true )->willReturn (['id ' => [['alias ' => '' ]]]);
76
+ $ this ->containerBuilder ->getDefinition ('menu_id ' )
77
+ ->setTags (['knp_menu.menu ' => [['alias ' => '' ]]]);
73
78
74
79
$ this ->expectException (\InvalidArgumentException::class);
75
- $ this ->expectExceptionMessage ('The alias is not defined in the "knp_menu.menu" tag for the service "id" ' );
76
- $ this ->pass ->process ($ this ->containerBuilder ->reveal ());
80
+ $ this ->expectExceptionMessage ('The alias is not defined in the "knp_menu.menu" tag for the service "menu_id" ' );
81
+
82
+ $ this ->pass ->process ($ this ->containerBuilder );
77
83
}
78
84
79
85
public function testRegisterMenuBuilderAndMenu (): void
80
86
{
81
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->willReturn ([
82
- 'id1 ' => [['alias ' => 'foo ' , 'method ' => 'fooMenu ' ], ['alias ' => 'bar ' , 'method ' => 'bar ' ]],
83
- 'id2 ' => [['alias ' => 'foo ' , 'method ' => 'fooBar ' ], ['alias ' => 'baz ' , 'method ' => 'bar ' ]],
84
- ]);
85
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu ' , true )->willReturn (['id3 ' => [['alias ' => 'test ' ]]]);
86
-
87
- $ menus = [
88
- 'foo ' => [new ServiceClosureArgument (new Reference ('id2 ' )), 'fooBar ' ],
89
- 'bar ' => [new ServiceClosureArgument (new Reference ('id1 ' )), 'bar ' ],
90
- 'baz ' => [new ServiceClosureArgument (new Reference ('id2 ' )), 'bar ' ],
91
- 'test ' => new ServiceClosureArgument (new Reference ('id3 ' )),
87
+ $ expectedMenuBuilders = [
88
+ 'foo ' => [new ServiceClosureArgument (new Reference ('id ' )), 'fooMenu ' ],
89
+ 'bar ' => [new ServiceClosureArgument (new Reference ('id ' )), 'bar ' ],
90
+ 'baz ' => new ServiceClosureArgument (new Reference ('menu_id ' )),
92
91
];
93
- $ this ->definition ->replaceArgument (0 , $ menus )->shouldBeCalled ();
94
92
95
- $ this ->pass ->process ($ this ->containerBuilder ->reveal ());
93
+ $ this ->pass ->process ($ this ->containerBuilder );
94
+
95
+ $ menuBuilders = $ this ->containerBuilder ->getDefinition ('knp_menu.menu_provider.lazy ' )->getArgument (0 );
96
+
97
+ $ this ->assertEquals ($ expectedMenuBuilders , $ menuBuilders );
98
+ $ this ->assertFalse ($ this ->containerBuilder ->hasDefinition ('knp_menu.menu_provider.builder_service ' ));
96
99
}
97
100
98
101
public function testMenuWinsOverBuilder (): void
99
102
{
100
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu_builder ' , true )->willReturn ([
101
- 'id1 ' => [['alias ' => 'foo ' , 'method ' => 'fooMenu ' ], ['alias ' => 'bar ' , 'method ' => 'bar ' ]],
102
- ]);
103
- $ this ->containerBuilder ->findTaggedServiceIds ('knp_menu.menu ' , true )->willReturn (['id3 ' => [['alias ' => 'foo ' ]]]);
104
-
105
- $ menus = [
106
- 'foo ' => new ServiceClosureArgument (new Reference ('id3 ' )),
107
- 'bar ' => [new ServiceClosureArgument (new Reference ('id1 ' )), 'bar ' ],
103
+ $ this ->containerBuilder ->getDefinition ('menu_id ' )
104
+ ->setTags (['knp_menu.menu ' => [['alias ' => 'foo ' ]]]);
105
+
106
+ $ expectedMenuBuilders = [
107
+ 'foo ' => new ServiceClosureArgument (new Reference ('menu_id ' )),
108
+ 'bar ' => [new ServiceClosureArgument (new Reference ('id ' )), 'bar ' ],
108
109
];
109
- $ this ->definition ->replaceArgument (0 , $ menus )->shouldBeCalled ();
110
110
111
- $ this ->pass ->process ($ this ->containerBuilder ->reveal ());
111
+ $ this ->pass ->process ($ this ->containerBuilder );
112
+
113
+ $ menuBuilders = $ this ->containerBuilder ->getDefinition ('knp_menu.menu_provider.lazy ' )->getArgument (0 );
114
+
115
+ $ this ->assertEquals ($ expectedMenuBuilders , $ menuBuilders );
112
116
}
113
117
}
0 commit comments