13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Bundle \FrameworkBundle \DependencyInjection \Compiler \FormPass ;
16
+ use Symfony \Component \DependencyInjection \Argument \IteratorArgument ;
17
+ use Symfony \Component \DependencyInjection \Argument \ServiceLocatorArgument ;
16
18
use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
19
use Symfony \Component \DependencyInjection \Definition ;
18
20
use Symfony \Component \DependencyInjection \Reference ;
@@ -27,8 +29,7 @@ class FormPassTest extends TestCase
27
29
{
28
30
public function testDoNothingIfFormExtensionNotLoaded ()
29
31
{
30
- $ container = new ContainerBuilder ();
31
- $ container ->addCompilerPass (new FormPass ());
32
+ $ container = $ this ->createContainerBuilder ();
32
33
33
34
$ container ->compile ();
34
35
@@ -37,47 +38,33 @@ public function testDoNothingIfFormExtensionNotLoaded()
37
38
38
39
public function testAddTaggedTypes ()
39
40
{
40
- $ container = new ContainerBuilder ();
41
- $ container ->addCompilerPass (new FormPass ());
42
-
43
- $ extDefinition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' );
44
- $ extDefinition ->setArguments (array (
45
- new Reference ('service_container ' ),
46
- array (),
47
- array (),
48
- array (),
49
- ));
41
+ $ container = $ this ->createContainerBuilder ();
50
42
51
- $ container ->setDefinition ('form.extension ' , $ extDefinition );
43
+ $ container ->setDefinition ('form.extension ' , $ this -> createExtensionDefinition () );
52
44
$ container ->register ('my.type1 ' , __CLASS__ .'_Type1 ' )->addTag ('form.type ' );
53
45
$ container ->register ('my.type2 ' , __CLASS__ .'_Type2 ' )->addTag ('form.type ' );
54
46
55
47
$ container ->compile ();
56
48
57
49
$ extDefinition = $ container ->getDefinition ('form.extension ' );
58
50
59
- $ this ->assertEquals (array (
60
- __CLASS__ .'_Type1 ' => 'my.type1 ' ,
61
- __CLASS__ .'_Type2 ' => 'my.type2 ' ,
62
- ), $ extDefinition ->getArgument (1 ));
51
+ $ this ->assertEquals (
52
+ new ServiceLocatorArgument (array (
53
+ __CLASS__ .'_Type1 ' => new Reference ('my.type1 ' ),
54
+ __CLASS__ .'_Type2 ' => new Reference ('my.type2 ' ),
55
+ )),
56
+ $ extDefinition ->getArgument (0 )
57
+ );
63
58
}
64
59
65
60
/**
66
61
* @dataProvider addTaggedTypeExtensionsDataProvider
67
62
*/
68
63
public function testAddTaggedTypeExtensions (array $ extensions , array $ expectedRegisteredExtensions )
69
64
{
70
- $ container = new ContainerBuilder ();
71
- $ container ->addCompilerPass (new FormPass ());
65
+ $ container = $ this ->createContainerBuilder ();
72
66
73
- $ extDefinition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' , array (
74
- new Reference ('service_container ' ),
75
- array (),
76
- array (),
77
- array (),
78
- ));
79
-
80
- $ container ->setDefinition ('form.extension ' , $ extDefinition );
67
+ $ container ->setDefinition ('form.extension ' , $ this ->createExtensionDefinition ());
81
68
82
69
foreach ($ extensions as $ serviceId => $ tag ) {
83
70
$ container ->register ($ serviceId , 'stdClass ' )->addTag ('form.type_extension ' , $ tag );
@@ -86,7 +73,7 @@ public function testAddTaggedTypeExtensions(array $extensions, array $expectedRe
86
73
$ container ->compile ();
87
74
88
75
$ extDefinition = $ container ->getDefinition ('form.extension ' );
89
- $ this ->assertSame ($ expectedRegisteredExtensions , $ extDefinition ->getArgument (2 ));
76
+ $ this ->assertEquals ($ expectedRegisteredExtensions , $ extDefinition ->getArgument (1 ));
90
77
}
91
78
92
79
/**
@@ -102,8 +89,11 @@ public function addTaggedTypeExtensionsDataProvider()
102
89
'my.type_extension3 ' => array ('extended_type ' => 'type2 ' ),
103
90
),
104
91
array (
105
- 'type1 ' => array ('my.type_extension1 ' , 'my.type_extension2 ' ),
106
- 'type2 ' => array ('my.type_extension3 ' ),
92
+ 'type1 ' => new IteratorArgument (array (
93
+ new Reference ('my.type_extension1 ' ),
94
+ new Reference ('my.type_extension2 ' ),
95
+ )),
96
+ 'type2 ' => new IteratorArgument (array (new Reference ('my.type_extension3 ' ))),
107
97
),
108
98
),
109
99
array (
@@ -116,8 +106,16 @@ public function addTaggedTypeExtensionsDataProvider()
116
106
'my.type_extension6 ' => array ('extended_type ' => 'type2 ' , 'priority ' => 1 ),
117
107
),
118
108
array (
119
- 'type1 ' => array ('my.type_extension2 ' , 'my.type_extension1 ' , 'my.type_extension3 ' ),
120
- 'type2 ' => array ('my.type_extension4 ' , 'my.type_extension5 ' , 'my.type_extension6 ' ),
109
+ 'type1 ' => new IteratorArgument (array (
110
+ new Reference ('my.type_extension2 ' ),
111
+ new Reference ('my.type_extension1 ' ),
112
+ new Reference ('my.type_extension3 ' ),
113
+ )),
114
+ 'type2 ' => new IteratorArgument (array (
115
+ new Reference ('my.type_extension4 ' ),
116
+ new Reference ('my.type_extension5 ' ),
117
+ new Reference ('my.type_extension6 ' ),
118
+ )),
121
119
),
122
120
),
123
121
);
@@ -129,17 +127,9 @@ public function addTaggedTypeExtensionsDataProvider()
129
127
*/
130
128
public function testAddTaggedFormTypeExtensionWithoutExtendedTypeAttribute ()
131
129
{
132
- $ container = new ContainerBuilder ();
133
- $ container ->addCompilerPass (new FormPass ());
130
+ $ container = $ this ->createContainerBuilder ();
134
131
135
- $ extDefinition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' , array (
136
- new Reference ('service_container ' ),
137
- array (),
138
- array (),
139
- array (),
140
- ));
141
-
142
- $ container ->setDefinition ('form.extension ' , $ extDefinition );
132
+ $ container ->setDefinition ('form.extension ' , $ this ->createExtensionDefinition ());
143
133
$ container ->register ('my.type_extension ' , 'stdClass ' )
144
134
->addTag ('form.type_extension ' );
145
135
@@ -148,73 +138,71 @@ public function testAddTaggedFormTypeExtensionWithoutExtendedTypeAttribute()
148
138
149
139
public function testAddTaggedGuessers ()
150
140
{
151
- $ container = new ContainerBuilder ();
152
- $ container ->addCompilerPass (new FormPass ());
153
-
154
- $ extDefinition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' );
155
- $ extDefinition ->setArguments (array (
156
- new Reference ('service_container ' ),
157
- array (),
158
- array (),
159
- array (),
160
- ));
141
+ $ container = $ this ->createContainerBuilder ();
161
142
162
143
$ definition1 = new Definition ('stdClass ' );
163
144
$ definition1 ->addTag ('form.type_guesser ' );
164
145
$ definition2 = new Definition ('stdClass ' );
165
146
$ definition2 ->addTag ('form.type_guesser ' );
166
147
167
- $ container ->setDefinition ('form.extension ' , $ extDefinition );
148
+ $ container ->setDefinition ('form.extension ' , $ this -> createExtensionDefinition () );
168
149
$ container ->setDefinition ('my.guesser1 ' , $ definition1 );
169
150
$ container ->setDefinition ('my.guesser2 ' , $ definition2 );
170
151
171
152
$ container ->compile ();
172
153
173
154
$ extDefinition = $ container ->getDefinition ('form.extension ' );
174
155
175
- $ this ->assertSame (array (
176
- 'my.guesser1 ' ,
177
- 'my.guesser2 ' ,
178
- ), $ extDefinition ->getArgument (3 ));
156
+ $ this ->assertEquals (
157
+ new IteratorArgument (array (
158
+ new Reference ('my.guesser1 ' ),
159
+ new Reference ('my.guesser2 ' ),
160
+ )),
161
+ $ extDefinition ->getArgument (2 )
162
+ );
179
163
}
180
164
181
165
/**
182
166
* @dataProvider privateTaggedServicesProvider
183
167
*/
184
- public function testPrivateTaggedServices ($ id , $ tagName , $ expectedExceptionMessage )
168
+ public function testPrivateTaggedServices ($ id , $ tagName , array $ tagAttributes = array () )
185
169
{
186
- $ container = new ContainerBuilder ();
187
- $ container ->addCompilerPass (new FormPass ());
188
-
189
- $ extDefinition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' );
190
- $ extDefinition ->setArguments (array (
191
- new Reference ('service_container ' ),
192
- array (),
193
- array (),
194
- array (),
195
- ));
196
-
197
- $ container ->setDefinition ('form.extension ' , $ extDefinition );
198
- $ container ->register ($ id , 'stdClass ' )->setPublic (false )->addTag ($ tagName );
170
+ $ container = $ this ->createContainerBuilder ();
199
171
200
- if (method_exists ($ this , 'expectException ' )) {
201
- $ this ->expectException ('InvalidArgumentException ' );
202
- $ this ->expectExceptionMessage ($ expectedExceptionMessage );
203
- } else {
204
- $ this ->setExpectedException ('InvalidArgumentException ' , $ expectedExceptionMessage );
205
- }
172
+ $ container ->setDefinition ('form.extension ' , $ this ->createExtensionDefinition ());
173
+ $ container ->register ($ id , 'stdClass ' )->setPublic (false )->addTag ($ tagName , $ tagAttributes );
206
174
207
175
$ container ->compile ();
208
176
}
209
177
210
178
public function privateTaggedServicesProvider ()
211
179
{
212
180
return array (
213
- array ('my.type ' , 'form.type ' , ' The service "my.type" must be public as form types are lazy-loaded ' ),
214
- array ('my.type_extension ' , 'form.type_extension ' , ' The service "my.type_extension" must be public as form type extensions are lazy-loaded ' ),
215
- array ('my.guesser ' , 'form.type_guesser ' , ' The service "my.guesser" must be public as form type guessers are lazy-loaded ' ),
181
+ array ('my.type ' , 'form.type ' ),
182
+ array ('my.type_extension ' , 'form.type_extension ' , array ( ' extended_type ' => ' Symfony\Component\Form\Extension\Core\Type\FormType ' ) ),
183
+ array ('my.guesser ' , 'form.type_guesser ' ),
216
184
);
217
185
}
186
+
187
+ private function createContainerBuilder ()
188
+ {
189
+ $ container = new ContainerBuilder ();
190
+ $ container ->addCompilerPass (new FormPass ());
191
+
192
+ return $ container ;
193
+ }
194
+
195
+ private function createExtensionDefinition ()
196
+ {
197
+ $ definition = new Definition ('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension ' );
198
+ $ definition ->setArguments (array (
199
+ new ServiceLocatorArgument (array ()),
200
+ array (),
201
+ new IteratorArgument (array ()),
202
+ ));
203
+
204
+ return $ definition ;
205
+ }
218
206
}
219
207
220
208
class FormPassTest_Type1 extends AbstractType
0 commit comments