13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Bridge \Doctrine \DependencyInjection \CompilerPass \RegisterEventListenersAndSubscribersPass ;
16
+ use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
16
17
use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
18
use Symfony \Component \DependencyInjection \Definition ;
18
19
use Symfony \Component \DependencyInjection \Reference ;
20
+ use Symfony \Component \DependencyInjection \ServiceLocator ;
19
21
20
22
class RegisterEventListenersAndSubscribersPassTest extends TestCase
21
23
{
@@ -68,7 +70,6 @@ public function testProcessEventListenersWithPriorities()
68
70
->addTag ('doctrine.event_listener ' , array (
69
71
'event ' => 'foo_bar ' ,
70
72
'priority ' => 3 ,
71
- 'lazy ' => true ,
72
73
))
73
74
;
74
75
$ container
@@ -86,25 +87,30 @@ public function testProcessEventListenersWithPriorities()
86
87
;
87
88
88
89
$ this ->process ($ container );
89
- $ methodCalls = $ container ->getDefinition ('doctrine.dbal.default_connection.event_manager ' )->getMethodCalls ();
90
+ $ eventManagerDef = $ container ->getDefinition ('doctrine.dbal.default_connection.event_manager ' );
91
+ $ methodCalls = $ eventManagerDef ->getMethodCalls ();
90
92
91
93
$ this ->assertEquals (
92
94
array (
93
- array ('addEventListener ' , array (array ('foo_bar ' ), new Reference ( 'c ' ) )),
94
- array ('addEventListener ' , array (array ('foo_bar ' ), new Reference ( 'a ' ) )),
95
- array ('addEventListener ' , array (array ('bar ' ), new Reference ( 'a ' ) )),
96
- array ('addEventListener ' , array (array ('foo ' ), new Reference ( 'b ' ) )),
97
- array ('addEventListener ' , array (array ('foo ' ), new Reference ( 'a ' ) )),
95
+ array ('addEventListener ' , array (array ('foo_bar ' ), 'c ' )),
96
+ array ('addEventListener ' , array (array ('foo_bar ' ), 'a ' )),
97
+ array ('addEventListener ' , array (array ('bar ' ), 'a ' )),
98
+ array ('addEventListener ' , array (array ('foo ' ), 'b ' )),
99
+ array ('addEventListener ' , array (array ('foo ' ), 'a ' )),
98
100
),
99
101
$ methodCalls
100
102
);
101
103
102
- // not lazy so must be reference
103
- $ this ->assertInstanceOf ('Symfony\Component\DependencyInjection\Reference ' , $ methodCalls [0 ][1 ][1 ]);
104
-
105
- // lazy so id instead of reference and must mark service public
106
- $ this ->assertSame ('a ' , $ methodCalls [1 ][1 ][1 ]);
107
- $ this ->assertTrue ($ container ->getDefinition ('a ' )->isPublic ());
104
+ $ serviceLocatorDef = $ container ->getDefinition ((string ) $ eventManagerDef ->getArgument (0 ));
105
+ $ this ->assertSame (ServiceLocator::class, $ serviceLocatorDef ->getClass ());
106
+ $ this ->assertEquals (
107
+ array (
108
+ 'c ' => new ServiceClosureArgument (new Reference ('c ' )),
109
+ 'a ' => new ServiceClosureArgument (new Reference ('a ' )),
110
+ 'b ' => new ServiceClosureArgument (new Reference ('b ' )),
111
+ ),
112
+ $ serviceLocatorDef ->getArgument (0 )
113
+ );
108
114
}
109
115
110
116
public function testProcessEventListenersWithMultipleConnections ()
@@ -136,20 +142,45 @@ public function testProcessEventListenersWithMultipleConnections()
136
142
137
143
$ this ->process ($ container );
138
144
145
+ $ eventManagerDef = $ container ->getDefinition ('doctrine.dbal.default_connection.event_manager ' );
146
+
147
+ // first connection
139
148
$ this ->assertEquals (
140
149
array (
141
- array ('addEventListener ' , array (array ('onFlush ' ), new Reference ( 'a ' ) )),
142
- array ('addEventListener ' , array (array ('onFlush ' ), new Reference ( 'b ' ) )),
150
+ array ('addEventListener ' , array (array ('onFlush ' ), 'a ' )),
151
+ array ('addEventListener ' , array (array ('onFlush ' ), 'b ' )),
143
152
),
144
- $ container -> getDefinition ( ' doctrine.dbal.default_connection.event_manager ' ) ->getMethodCalls ()
153
+ $ eventManagerDef ->getMethodCalls ()
145
154
);
146
155
156
+ $ serviceLocatorDef = $ container ->getDefinition ((string ) $ eventManagerDef ->getArgument (0 ));
157
+ $ this ->assertSame (ServiceLocator::class, $ serviceLocatorDef ->getClass ());
147
158
$ this ->assertEquals (
148
159
array (
149
- array ( ' addEventListener ' , array ( array ( ' onFlush ' ), new Reference ('a ' ) )),
150
- array ( ' addEventListener ' , array ( array ( ' onFlush ' ), new Reference ('c ' ) )),
160
+ ' a ' => new ServiceClosureArgument ( new Reference ('a ' )),
161
+ ' b ' => new ServiceClosureArgument ( new Reference ('b ' )),
151
162
),
152
- $ container ->getDefinition ('doctrine.dbal.second_connection.event_manager ' )->getMethodCalls ()
163
+ $ serviceLocatorDef ->getArgument (0 )
164
+ );
165
+
166
+ // second connection
167
+ $ secondEventManagerDef = $ container ->getDefinition ('doctrine.dbal.second_connection.event_manager ' );
168
+ $ this ->assertEquals (
169
+ array (
170
+ array ('addEventListener ' , array (array ('onFlush ' ), 'a ' )),
171
+ array ('addEventListener ' , array (array ('onFlush ' ), 'c ' )),
172
+ ),
173
+ $ secondEventManagerDef ->getMethodCalls ()
174
+ );
175
+
176
+ $ serviceLocatorDef = $ container ->getDefinition ((string ) $ secondEventManagerDef ->getArgument (0 ));
177
+ $ this ->assertSame (ServiceLocator::class, $ serviceLocatorDef ->getClass ());
178
+ $ this ->assertEquals (
179
+ array (
180
+ 'a ' => new ServiceClosureArgument (new Reference ('a ' )),
181
+ 'c ' => new ServiceClosureArgument (new Reference ('c ' )),
182
+ ),
183
+ $ serviceLocatorDef ->getArgument (0 )
153
184
);
154
185
}
155
186
@@ -269,11 +300,13 @@ private function createBuilder($multipleConnections = false)
269
300
270
301
$ connections = array ('default ' => 'doctrine.dbal.default_connection ' );
271
302
272
- $ container ->register ('doctrine.dbal.default_connection.event_manager ' , 'stdClass ' );
303
+ $ container ->register ('doctrine.dbal.default_connection.event_manager ' , 'stdClass ' )
304
+ ->addArgument (new Reference ('service_container ' ));
273
305
$ container ->register ('doctrine.dbal.default_connection ' , 'stdClass ' );
274
306
275
307
if ($ multipleConnections ) {
276
- $ container ->register ('doctrine.dbal.second_connection.event_manager ' , 'stdClass ' );
308
+ $ container ->register ('doctrine.dbal.second_connection.event_manager ' , 'stdClass ' )
309
+ ->addArgument (new Reference ('service_container ' ));
277
310
$ container ->register ('doctrine.dbal.second_connection ' , 'stdClass ' );
278
311
$ connections ['second ' ] = 'doctrine.dbal.second_connection ' ;
279
312
}
0 commit comments