11
11
12
12
namespace Symfony \Tests \Component \Form \EventListener ;
13
13
14
+ use Symfony \Component \Form \Event \DataEvent ;
14
15
use Symfony \Component \Form \Event \FilterDataEvent ;
15
16
use Symfony \Component \Form \EventListener \ResizeFormListener ;
16
- use Symfony \Component \Form \Event \ DataEvent ;
17
+ use Symfony \Component \Form \FormBuilder ;
17
18
18
19
class ResizeFormListenerTest extends \PHPUnit_Framework_TestCase
19
20
{
@@ -22,63 +23,210 @@ class ResizeFormListenerTest extends \PHPUnit_Framework_TestCase
22
23
23
24
public function setUp ()
24
25
{
26
+ $ this ->dispatcher = $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
25
27
$ this ->factory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
26
- $ this ->form = $ this ->getMock ('Symfony\Component\Form\Form ' , array ('add ' , 'has ' ), array (), '' , false );
28
+ $ this ->form = $ this ->getForm ();
29
+ }
30
+
31
+ protected function getBuilder ($ name = 'name ' )
32
+ {
33
+ return new FormBuilder ($ name , $ this ->factory , $ this ->dispatcher );
27
34
}
28
35
29
- public function testResizePreSetData ( )
36
+ protected function getForm ( $ name = ' name ' )
30
37
{
31
- $ expectedType = "text " ;
38
+ return $ this ->getBuilder ($ name )->getForm ();
39
+ }
40
+
41
+ protected function getMockForm ()
42
+ {
43
+ return $ this ->getMock ('Symfony\Tests\Component\Form\FormInterface ' );
44
+ }
45
+
46
+ public function testPreSetDataResizesForm ()
47
+ {
48
+ $ this ->form ->add ($ this ->getForm ('0 ' ));
49
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
32
50
33
51
$ this ->factory ->expects ($ this ->at (0 ))
34
- ->method ('create ' )
35
- ->with ($ this -> equalTo ( $ expectedType ), $ this -> equalTo ( 0 ) , array ('property_path ' => '[0 ] ' ))
36
- ->will ($ this ->returnValue ($ this ->getMock ( ' Symfony\Tests\Component\Form\FormInterface ' )));
52
+ ->method ('create ' )
53
+ ->with (' text ' , 1 , array ('property_path ' => '[1 ] ' ))
54
+ ->will ($ this ->returnValue ($ this ->getForm ( ' 1 ' )));
37
55
$ this ->factory ->expects ($ this ->at (1 ))
38
- ->method ('create ' )
39
- ->with ($ this -> equalTo ( $ expectedType ), $ this -> equalTo ( 1 ) , array ('property_path ' => '[1 ] ' ))
40
- ->will ($ this ->returnValue ($ this ->getMock ( ' Symfony\Tests\Component\Form\FormInterface ' )));
56
+ ->method ('create ' )
57
+ ->with (' text ' , 2 , array ('property_path ' => '[2 ] ' ))
58
+ ->will ($ this ->returnValue ($ this ->getForm ( ' 2 ' )));
41
59
42
- $ data = array (" string " , " string " );
60
+ $ data = array (1 => ' string ' , 2 => ' string ' );
43
61
$ event = new DataEvent ($ this ->form , $ data );
44
- $ listener = new ResizeFormListener ($ this ->factory , $ expectedType , false);
62
+ $ listener = new ResizeFormListener ($ this ->factory , ' text ' , false );
45
63
$ listener ->preSetData ($ event );
64
+
65
+ $ this ->assertFalse ($ this ->form ->has ('0 ' ));
66
+ $ this ->assertTrue ($ this ->form ->has ('1 ' ));
67
+ $ this ->assertTrue ($ this ->form ->has ('2 ' ));
46
68
}
47
69
48
- public function testResizePreSetDataNoArrayThrowsException ()
70
+ public function testPreSetDataRemovesPrototypeRowIfNotResizeOnBind ()
49
71
{
50
- $ this ->setExpectedException ( ' Symfony\Component\Form\Exception\UnexpectedTypeException ' );
51
-
52
- $ data = " no array or traversable " ;
72
+ $ this ->form -> add ( $ this -> getForm ( ' $$name$$ ' ) );
73
+
74
+ $ data = array () ;
53
75
$ event = new DataEvent ($ this ->form , $ data );
54
- $ listener = new ResizeFormListener ($ this ->factory , " text " , false );
76
+ $ listener = new ResizeFormListener ($ this ->factory , ' text ' , false );
55
77
$ listener ->preSetData ($ event );
78
+
79
+ $ this ->assertFalse ($ this ->form ->has ('$$name$$ ' ));
56
80
}
57
81
58
- public function testResizePreSetDataNull ()
82
+ public function testPreSetDataDoesNotRemovePrototypeRowIfResizeOnBind ()
83
+ {
84
+ $ this ->form ->add ($ this ->getForm ('$$name$$ ' ));
85
+
86
+ $ data = array ();
87
+ $ event = new DataEvent ($ this ->form , $ data );
88
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
89
+ $ listener ->preSetData ($ event );
90
+
91
+ $ this ->assertTrue ($ this ->form ->has ('$$name$$ ' ));
92
+ }
93
+
94
+ /**
95
+ * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
96
+ */
97
+ public function testPreSetDataRequiresArrayOrTraversable ()
98
+ {
99
+ $ data = 'no array or traversable ' ;
100
+ $ event = new DataEvent ($ this ->form , $ data );
101
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , false );
102
+ $ listener ->preSetData ($ event );
103
+ }
104
+
105
+ public function testPreSetDataDealsWithNullData ()
59
106
{
60
107
$ this ->factory ->expects ($ this ->never ())->method ('create ' );
61
108
62
109
$ data = null ;
63
110
$ event = new DataEvent ($ this ->form , $ data );
64
- $ listener = new ResizeFormListener ($ this ->factory , " text " , false );
111
+ $ listener = new ResizeFormListener ($ this ->factory , ' text ' , false );
65
112
$ listener ->preSetData ($ event );
66
113
}
67
114
68
- public function testPreBind ()
115
+ public function testPreBindResizesFormIfResizable ()
69
116
{
70
- $ expectedType = "text " ;
117
+ $ this ->form ->add ($ this ->getForm ('0 ' ));
118
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
71
119
72
- $ this ->form ->expects ($ this ->once ())->method ('has ' )->with ($ this ->equalTo ('foo ' ))->will ($ this ->returnValue ( false ));
73
- $ this ->form ->expects ($ this ->once ())->method ('add ' )->with ($ this ->isInstanceOf ('Symfony\Tests\Component\Form\FormInterface ' ));
74
- $ this ->factory ->expects ($ this ->at (0 ))
75
- ->method ('create ' )
76
- ->with ($ this ->equalTo ($ expectedType ), $ this ->equalTo ('foo ' ), $ this ->equalTo (array ('property_path ' => '[foo] ' )))
77
- ->will ($ this ->returnValue ( $ this ->getMock ('Symfony\Tests\Component\Form\FormInterface ' ) ));
120
+ $ this ->factory ->expects ($ this ->once ())
121
+ ->method ('create ' )
122
+ ->with ('text ' , 2 , array ('property_path ' => '[2] ' ))
123
+ ->will ($ this ->returnValue ($ this ->getForm ('2 ' )));
124
+
125
+ $ data = array (0 => 'string ' , 2 => 'string ' );
126
+ $ event = new DataEvent ($ this ->form , $ data );
127
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
128
+ $ listener ->preBind ($ event );
129
+
130
+ $ this ->assertTrue ($ this ->form ->has ('0 ' ));
131
+ $ this ->assertFalse ($ this ->form ->has ('1 ' ));
132
+ $ this ->assertTrue ($ this ->form ->has ('2 ' ));
133
+ }
134
+
135
+ // fix for https://github.com/symfony/symfony/pull/493
136
+ public function testPreBindRemovesZeroKeys ()
137
+ {
138
+ $ this ->form ->add ($ this ->getForm ('0 ' ));
139
+
140
+ $ data = array ();
141
+ $ event = new DataEvent ($ this ->form , $ data );
142
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
143
+ $ listener ->preBind ($ event );
78
144
79
- $ data = array ("foo " => "bar " );
145
+ $ this ->assertFalse ($ this ->form ->has ('0 ' ));
146
+ }
147
+
148
+ public function testPreBindDoesNothingIfNotResizable ()
149
+ {
150
+ $ this ->form ->add ($ this ->getForm ('0 ' ));
151
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
152
+
153
+ $ data = array (0 => 'string ' , 2 => 'string ' );
154
+ $ event = new DataEvent ($ this ->form , $ data );
155
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , false );
156
+ $ listener ->preBind ($ event );
157
+
158
+ $ this ->assertTrue ($ this ->form ->has ('0 ' ));
159
+ $ this ->assertTrue ($ this ->form ->has ('1 ' ));
160
+ $ this ->assertFalse ($ this ->form ->has ('2 ' ));
161
+ }
162
+
163
+ /**
164
+ * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
165
+ */
166
+ public function testPreBindRequiresArrayOrTraversable ()
167
+ {
168
+ $ data = 'no array or traversable ' ;
80
169
$ event = new DataEvent ($ this ->form , $ data );
81
- $ listener = new ResizeFormListener ($ this ->factory , " text " , true );
170
+ $ listener = new ResizeFormListener ($ this ->factory , ' text ' , true );
82
171
$ listener ->preBind ($ event );
83
172
}
173
+
174
+ public function testPreBindDealsWithNullData ()
175
+ {
176
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
177
+
178
+ $ data = null ;
179
+ $ event = new DataEvent ($ this ->form , $ data );
180
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
181
+ $ listener ->preBind ($ event );
182
+
183
+ $ this ->assertFalse ($ this ->form ->has ('1 ' ));
184
+ }
185
+
186
+ public function testOnBindNormDataRemovesEntriesMissingInTheFormIfResizable ()
187
+ {
188
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
189
+
190
+ $ data = array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' );
191
+ $ event = new FilterDataEvent ($ this ->form , $ data );
192
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
193
+ $ listener ->onBindNormData ($ event );
194
+
195
+ $ this ->assertEquals (array (1 => 'second ' ), $ event ->getData ());
196
+ }
197
+
198
+ public function testOnBindNormDataDoesNothingIfNotResizable ()
199
+ {
200
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
201
+
202
+ $ data = array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' );
203
+ $ event = new FilterDataEvent ($ this ->form , $ data );
204
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , false );
205
+ $ listener ->onBindNormData ($ event );
206
+
207
+ $ this ->assertEquals ($ data , $ event ->getData ());
208
+ }
209
+
210
+ /**
211
+ * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
212
+ */
213
+ public function testOnBindNormDataRequiresArrayOrTraversable ()
214
+ {
215
+ $ data = 'no array or traversable ' ;
216
+ $ event = new FilterDataEvent ($ this ->form , $ data );
217
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
218
+ $ listener ->onBindNormData ($ event );
219
+ }
220
+
221
+ public function testOnBindNormDataDealsWithNullData ()
222
+ {
223
+ $ this ->form ->add ($ this ->getForm ('1 ' ));
224
+
225
+ $ data = null ;
226
+ $ event = new FilterDataEvent ($ this ->form , $ data );
227
+ $ listener = new ResizeFormListener ($ this ->factory , 'text ' , true );
228
+ $ listener ->onBindNormData ($ event );
229
+
230
+ $ this ->assertEquals (array (), $ event ->getData ());
231
+ }
84
232
}
0 commit comments