@@ -24,6 +24,13 @@ public function addAxis($axis) {}
24
24
public function removeAxis ($ axis ) {}
25
25
}
26
26
27
+ class MergeCollectionListenerTest_CarCustomPrefix
28
+ {
29
+ public function fooAxis ($ axis ) {}
30
+
31
+ public function barAxis ($ axis ) {}
32
+ }
33
+
27
34
abstract class MergeCollectionListenerTest extends \PHPUnit_Framework_TestCase
28
35
{
29
36
private $ dispatcher ;
@@ -219,11 +226,12 @@ public function testDontDealWithNullOriginalDataIfNotAllowAdd()
219
226
public function testCallAdderIfAllowAdd ()
220
227
{
221
228
$ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
222
- $ parentForm = $ this ->getForm ('article ' );
229
+ $ parentForm = $ this ->getForm ('car ' );
223
230
$ parentForm ->setData ($ parentData );
224
231
$ parentForm ->add ($ this ->form );
225
232
226
- $ originalData = $ this ->getData (array (1 => 'second ' ));
233
+ $ originalDataArray = array (1 => 'second ' );
234
+ $ originalData = $ this ->getData ($ originalDataArray );
227
235
$ newData = $ this ->getData (array (0 => 'first ' , 1 => 'seco
F42D
nd ' , 2 => 'third ' ));
228
236
229
237
$ this ->form ->setData ($ originalData );
@@ -239,20 +247,24 @@ public function testCallAdderIfAllowAdd()
239
247
$ listener = new MergeCollectionListener (true , false );
240
248
$ listener ->onBindNormData ($ event );
241
249
242
- // The original object was modified
243
250
if (is_object ($ originalData )) {
244
251
$ this ->assertSame ($ originalData , $ event ->getData ());
245
252
}
253
+
254
+ // The data was not modified directly
255
+ // Thus it should not be written back into the parent data!
256
+ $ this ->assertEquals ($ this ->getData ($ originalDataArray ), $ event ->getData ());
246
257
}
247
258
248
259
public function testDontCallAdderIfNotAllowAdd ()
249
260
{
250
261
$ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
251
- $ parentForm = $ this ->getForm ('article ' );
262
+ $ parentForm = $ this ->getForm ('car ' );
252
263
$ parentForm ->setData ($ parentData );
253
264
$ parentForm ->add ($ this ->form );
254
265
255
- $ originalData = $ this ->getData (array (1 => 'second ' ));
266
+ $ originalDataArray = array (1 => 'second ' );
267
+ $ originalData = $ this ->getData ($ originalDataArray );
256
268
$ newData = $ this ->getData (array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' ));
257
269
258
270
$ this ->form ->setData ($ originalData );
@@ -264,20 +276,50 @@ public function testDontCallAdderIfNotAllowAdd()
264
276
$ listener = new MergeCollectionListener (false , false );
265
277
$ listener ->onBindNormData ($ event );
266
278
267
- // The original object was modified
268
279
if (is_object ($ originalData )) {
269
280
$ this ->assertSame ($ originalData , $ event ->getData ());
270
281
}
282
+
283
+ // The data was not modified
284
+ $ this ->assertEquals ($ this ->getData ($ originalDataArray ), $ event ->getData ());
285
+ }
286
+
287
+ public function testDontCallAdderIfNotUseAccessors ()
288
+ {
289
+ $ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
290
+ $ parentForm = $ this ->getForm ('car ' );
291
+ $ parentForm ->setData ($ parentData );
292
+ $ parentForm ->add ($ this ->form );
293
+
294
+ $ originalData = $ this ->getData (array (1 => 'second ' ));
295
+ $ newData = $ this ->getData (array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' ));
296
+
297
+ $ this ->form ->setData ($ originalData );
298
+
299
+ $ parentData ->expects ($ this ->never ())
300
+ ->method ('addAxis ' );
301
+
302
+ $ event = new FilterDataEvent ($ this ->form , $ newData );
303
+ $ listener = new MergeCollectionListener (true , false , false );
304
+ $ listener ->onBindNormData ($ event );
305
+
306
+ if (is_object ($ originalData )) {
307
+ $ this ->assertSame ($ originalData , $ event ->getData ());
308
+ }
309
+
310
+ // The data was modified without accessors
311
+ $ this ->assertEquals ($ newData , $ event ->getData ());
271
312
}
272
313
273
314
public function testCallRemoverIfAllowDelete ()
274
315
{
275
316
$ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
276
- $ parentForm = $ this ->getForm ('article ' );
317
+ $ parentForm = $ this ->getForm ('car ' );
277
318
$ parentForm ->setData ($ parentData );
278
319
$ parentForm ->add ($ this ->form );
279
320
280
- $ originalData = $ this ->getData (array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' ));
321
+ $ originalDataArray = array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' );
322
+ $ originalData = $ this ->getData ($ originalDataArray );
281
323
$ newData = $ this ->getData (array (1 => 'second ' ));
282
324
283
325
$ this ->form ->setData ($ originalData );
@@ -293,20 +335,24 @@ public function testCallRemoverIfAllowDelete()
293
335
$ listener = new MergeCollectionListener (false , true );
294
336
$ listener ->onBindNormData ($ event );
295
337
296
- // The original object was modified
297
338
if (is_object ($ originalData )) {
298
339
$ this ->assertSame ($ originalData , $ event ->getData ());
299
340
}
341
+
342
+ // The data was not modified directly
343
+ // Thus it should not be written back into the parent data!
344
+ $ this ->assertEquals ($ this ->getData ($ originalDataArray ), $ event ->getData ());
300
345
}
301
346
302
347
public function testDontCallRemoverIfNotAllowDelete ()
303
348
{
304
349
$ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
305
- $ parentForm = $ this ->getForm ('article ' );
350
+ $ parentForm = $ this ->getForm ('car ' );
306
351
$ parentForm ->setData ($ parentData );
307
352
$ parentForm ->add ($ this ->form );
308
353
309
- $ originalData = $ this ->getData (array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' ));
354
+ $ originalDataArray = array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' );
355
+ $ originalData = $ this ->getData ($ originalDataArray );
310
356
$ newData = $ this ->getData (array (1 => 'second ' ));
311
357
312
358
$ this ->form ->setData ($ originalData );
@@ -318,9 +364,71 @@ public function testDontCallRemoverIfNotAllowDelete()
318
364
$ listener = new MergeCollectionListener (false , false );
319
365
$ listener ->onBindNormData ($ event );
320
366
321
- // The original object was modified
322
367
if (is_object ($ originalData )) {
323
368
$ this ->assertSame ($ originalData , $ event ->getData ());
324
369
}
370
+
371
+ // The data was not modified
372
+ $ this ->assertEquals ($ this ->getData ($ originalDataArray ), $ event ->getData ());
373
+ }
374
+
375
+ public function testDontCallRemoverIfNotUseAccessors ()
376
+ {
377
+ $ parentData = $ this ->getMock (__CLASS__ . '_Car ' );
378
+ $ parentForm = $ this ->getForm ('car ' );
379
+ $ parentForm ->setData ($ parentData );
380
+ $ parentForm ->add ($ this ->form );
381
+
382
+ $ originalData = $ this ->getData (array (0 => 'first ' , 1 => 'second ' , 2 => 'third ' ));
383
+ $ newData = $ this ->getData (array (1 => 'second ' ));
384
+
385
+ $ this ->form ->setData ($ originalData );
386
+
387
+ $ parentData ->expects ($ this ->never ())
388
+ ->method ('removeAxis ' );
389
+
390
+ $ event = new FilterDataEvent ($ this ->form , $ newData );
391
+ $ listener = new MergeCollectionListener (false , true , false );
392
+ $ listener ->onBindNormData ($ event );
393
+
394
+ if (is_object ($ originalData )) {
395
+ $ this ->assertSame ($ originalData , $ event ->getData ());
396
+ }
397
+
398
+ // The data was modified directly
399
+ $ this ->assertEquals ($ newData , $ event ->getData ());
400
+ }
401
+
402
+ public function testCallAccessorsWithCustomPrefixes ()
403
+ {
404
+ $ parentData = $ this ->getMock (__CLASS__ . '_CarCustomPrefix ' );
405
+ $ parentForm = $ this ->getForm ('car ' );
406
+ $ parentForm ->setData ($ parentData );
407
+ $ parentForm ->add ($ this ->form );
408
+
409
+ $ originalDataArray = array (1 => 'second ' );
410
+ $ originalData = $ this ->getData ($ originalDataArray );
411
+ $ newData = $ this ->getData (array (0 => 'first ' ));
412
+
413
+ $ this ->form ->setData ($ originalData );
414
+
415
+ $ parentData ->expects ($ this ->once ())
416
+ ->method ('fooAxis ' )
417
+ ->with ('first ' );
418
+ $ parentData ->expects ($ this ->once ())
419
+ ->method ('barAxis ' )
420
+ ->with ('second ' );
421
+
422
+ $ event = new FilterDataEvent ($ this ->form , $ newData );
423
+ $ listener = new MergeCollectionListener (true , true , true , 'foo ' , 'bar ' );
424
+ $ listener ->onBindNormData ($ event );
425
+
426
+ if (is_object ($ originalData )) {
427
+ $ this ->assertSame ($ originalData , $ event ->getData ());
428
+ }
429
+
430
+ // The data was not modified directly
431
+ // Thus it should not be written back into the parent data!
432
+ $ this ->assertEquals ($ this ->getData ($ originalDataArray ), $ event ->getData ());
325
433
}
326
434
}
0 commit comments