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