Skip to content

Commit cfcb882

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-39769-pk-autoincrement' into MAGETWO-43197-missing-interceptor
2 parents 1406400 + 66d4582 commit cfcb882

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

app/code/Magento/Tax/Model/Resource/Calculation.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class Calculation extends \Magento\Framework\Model\Resource\Db\AbstractDb
2424
*/
2525
protected $_ratesCache = [];
2626

27-
/**
28-
* Primary key auto increment flag
29-
*
30-
* @var bool
31-
*/
32-
protected $_isPkAutoIncrement = false;
33-
3427
/**
3528
* Tax data
3629
*

lib/internal/Magento/Framework/Model/Resource/Db/AbstractDb.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,9 @@ protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
768768
}
769769
$this->getConnection()->insert($this->getMainTable(), $bind);
770770

771-
$object->setId($this->getConnection()->lastInsertId($this->getMainTable()));
771+
if ($this->_isPkAutoIncrement) {
772+
$object->setId($this->getConnection()->lastInsertId($this->getMainTable()));
773+
}
772774

773775
if ($this->_useIsObjectNew) {
774776
$object->isObjectNew(false);

lib/internal/Magento/Framework/Model/Test/Unit/Resource/Db/AbstractDbTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,72 @@ public function testPrepareDataForUpdate()
487487

488488
$this->_model->save($abstractModelMock);
489489
}
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+
490558
}

0 commit comments

Comments
 (0)