@@ -487,4 +487,72 @@ public function testPrepareDataForUpdate()
487
487
488
488
$ this ->_model ->save ($ abstractModelMock );
489
489
}
490
+
491
+ /**
492
+ * Test that we only set/override id on object if PK autoincrement is enabled
493
+ * @param bool $pkIncrement
494
+ * @dataProvider saveNewObjectDataProvider
495
+ * @SuppressWarnings(PHPMD.NPathComplexity)
496
+ */
497
+ public function testSaveNewObject ($ pkIncrement )
498
+ {
499
+ /**
500
+ * Mock SUT so as not to test extraneous logic
501
+ */
502
+ $ model = $ this ->getMockBuilder ('Magento\Framework\Model\Resource\Db\AbstractDb ' )
503
+ ->disableOriginalConstructor ()
504
+ ->setMethods (['_prepareDataForSave ' , 'getIdFieldName ' , 'getConnection ' , 'getMainTable ' ])
505
+ ->getMockForAbstractClass ();
506
+ /**
507
+ * Only testing the logic in a protected method and property, must use reflection to avoid dealing with large
508
+ * amounts of unrelated logic in save function
509
+ *
510
+ * make saveNewObject and _isPkAutoIncrement public
511
+ */
512
+ $ reflectionMethod = new \ReflectionMethod ($ model , 'saveNewObject ' );
513
+ $ reflectionMethod ->setAccessible (true );
514
+ $ reflectionProperty = new \ReflectionProperty ($ model , '_isPkAutoIncrement ' );
515
+ $ reflectionProperty ->setAccessible (true );
516
+ $ reflectionProperty ->setValue ($ model , $ pkIncrement );
517
+
518
+ // Mocked behavior
519
+ $ connectionMock = $ this ->getMockBuilder ('\Magento\Framework\DB\Adapter\AdapterInterface ' )
520
+ ->disableOriginalConstructor ()
521
+ ->setMethods (['lastInsertId ' ])
522
+ ->getMockForAbstractClass ();
523
+ $ getConnectionInvokedCount = $ pkIncrement ? 2 : 1 ;
524
+ $ model ->expects ($ this ->exactly ($ getConnectionInvokedCount ))
525
+ ->method ('getConnection ' )
526
+ ->willReturn ($ connectionMock );
527
+
528
+ $ idFieldName = 'id_field_name ' ;
529
+ $ model ->expects ($ this ->once ())->method ('_prepareDataForSave ' )->willReturn ([$ idFieldName => 'id ' ,]);
530
+
531
+
532
+ // Test expectations
533
+ // Only get object's id field name if not PK autoincrement
534
+ $ getIdFieldNameInvokedCount = $ pkIncrement ? 1 : 0 ;
535
+ $ model ->expects ($ this ->exactly ($ getIdFieldNameInvokedCount ))
536
+ ->method ('getIdFieldName ' )
537
+ ->willReturn ($ idFieldName );
538
+
539
+ // Only set object id if not PK autoincrement
540
+ $ setIdInvokedCount = $ pkIncrement ? 1 : 0 ;
541
+ $ inputObject = $ this ->getMockBuilder ('\Magento\Framework\Model\AbstractModel ' )
542
+ ->disableOriginalConstructor ()
543
+ ->getMock ();
544
+ $ inputObject ->expects ($ this ->exactly ($ setIdInvokedCount ))->method ('setId ' );
545
+
546
+ // Only call lastInsertId if not PK autoincrement
547
+ $ lastInsertIdInvokedCount = $ pkIncrement ? 1 : 0 ;
548
+ $ connectionMock ->expects ($ this ->exactly ($ lastInsertIdInvokedCount ))->method ('lastInsertId ' );
549
+
550
+ $ reflectionMethod ->invokeArgs ($ model , [$ inputObject ]);
551
+ }
552
+
553
+ public function saveNewObjectDataProvider ()
554
+ {
555
+ return [[true ], [false ]];
556
+ }
557
+
490
558
}
0 commit comments