@@ -2462,4 +2462,111 @@ public function testBelongsToManyIntegration() {
2462
2462
$ this ->assertEquals ($ tags , $ article ->tags );
2463
2463
}
2464
2464
2465
+ /**
2466
+ * Tests that it is possible to do a deep save and control what associations get saved,
2467
+ * while having control of the options passed to each level of the save
2468
+ *
2469
+ * @group save
2470
+ * @return void
2471
+ */
2472
+ public function testSaveDeepAssociationOptions () {
2473
+ $ articles = $ this ->getMock (
2474
+ '\Cake\ORM\Table ' ,
2475
+ ['_insert ' ],
2476
+ [['table ' => 'articles ' , 'connection ' => $ this ->connection ]]
2477
+ );
2478
+ $ authors = $ this ->getMock (
2479
+ '\Cake\ORM\Table ' ,
2480
+ ['_insert ' , '_processValidation ' ],
2481
+ [['table ' => 'authors ' , 'connection ' => $ this ->connection ]]
2482
+ );
2483
+ $ supervisors = $ this ->getMock (
2484
+ '\Cake\ORM\Table ' ,
2485
+ ['_insert ' , '_processValidation ' ],
2486
+ [[
2487
+ 'table ' => 'authors ' ,
2488
+ 'alias ' => 'supervisors ' ,
2489
+ 'connection ' => $ this ->connection
2490
+ ]]
2491
+ );
2492
+ $ tags = $ this ->getMock (
2493
+ '\Cake\ORM\Table ' ,
2494
+ ['_insert ' ],
2495
+ [['table ' => 'tags ' , 'connection ' => $ this ->connection ]]
2496
+ );
2497
+
2498
+ $ articles ->belongsTo ('authors ' , ['targetTable ' => $ authors ]);
2499
+ $ authors ->hasOne ('supervisors ' , ['targetTable ' => $ supervisors ]);
2500
+ $ supervisors ->belongsToMany ('tags ' , ['targetTable ' => $ tags ]);
2501
+
2502
+ $ entity = new \Cake \ORM \Entity ([
2503
+ 'title ' => 'bar ' ,
2504
+ 'author ' => new \Cake \ORM \Entity ([
2505
+ 'name ' => 'Juan ' ,
2506
+ 'supervisor ' => new \Cake \ORM \Entity (['name ' => 'Marc ' ]),
2507
+ 'tags ' => [
2508
+ new \Cake \ORM \Entity (['name ' => 'foo ' ])
2509
+ ]
2510
+ ]),
2511
+ ]);
2512
+ $ entity ->isNew (true );
2513
+ $ entity ->author ->isNew (true );
2514
+ $ entity ->author ->supervisor ->isNew (true );
2515
+ $ entity ->author ->tags [0 ]->isNew (true );
2516
+
2517
+ $ articles ->expects ($ this ->once ())
2518
+ ->method ('_insert ' )
2519
+ ->with ($ entity , ['title ' => 'bar ' ])
2520
+ ->will ($ this ->returnValue ($ entity ));
2521
+
2522
+ $ authors ->expects ($ this ->once ())
2523
+ ->method ('_insert ' )
2524
+ ->with ($ entity ->author , ['name ' => 'Juan ' ])
2525
+ ->will ($ this ->returnValue ($ entity ->author ));
2526
+
2527
+ $ options = new \ArrayObject ([
2528
+ 'validate ' => 'special ' ,
2529
+ 'atomic ' => true ,
2530
+ 'associated ' => ['supervisors ' => [
2531
+ 'atomic ' => false , 'validate ' => false , 'associated ' => false
2532
+ ]]
2533
+ ]);
2534
+ $ authors ->expects ($ this ->once ())
2535
+ ->method ('_processValidation ' )
2536
+ ->with ($ entity ->author , $ options )
2537
+ ->will ($ this ->returnValue (true ));
2538
+
2539
+ $ supervisors ->expects ($ this ->once ())
2540
+ ->method ('_insert ' )
2541
+ ->with ($ entity ->author ->supervisor , ['name ' => 'Marc ' ])
2542
+ ->will ($ this ->returnValue ($ entity ->author ->supervisor ));
2543
+
2544
+ $ options = new \ArrayObject ([
2545
+ 'validate ' => false ,
2546
+ 'atomic ' => false ,
2547
+ 'associated ' => []
2548
+ ]);
2549
+ $ supervisors ->expects ($ this ->once ())
2550
+ ->method ('_processValidation ' )
2551
+ ->with ($ entity ->author ->supervisor , $ options )
2552
+ ->will ($ this ->returnValue (true ));
2553
+
2554
+ $ tags ->expects ($ this ->never ())->method ('_insert ' );
2555
+
2556
+ $ this ->assertSame ($ entity , $ articles ->save ($ entity , [
2557
+ 'associated ' => [
2558
+ 'authors ' => [
2559
+ 'validate ' => 'special ' ,
2560
+ 'associated ' => [
2561
+ 'supervisors ' => [
2562
+ 'atomic ' => false ,
2563
+ 'validate ' => false ,
2564
+ 'associated ' => false
2565
+ ]
2566
+ ]
2567
+ ]
2568
+ ]
2569
+ ]));
2570
+ }
2571
+
2465
2572
}
0 commit comments