Skip to content

Commit 2f18f6d

Browse files
author
Maksym Savich
committed
MAGETWO-35462: Refactor \Magento\Framework\Model\Resource\Db\AbstractDb only update changed fields
- Unit tests implemented
1 parent fc3331a commit 2f18f6d

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

lib/internal/Magento/Framework/Model/Test/Unit/AbstractModelTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,22 @@ public function testDelete()
108108
$this->resourceMock->expects($this->once())->method('delete')->with($this->model);
109109
$this->model->delete();
110110
}
111+
112+
public function testUpdateStoredData()
113+
{
114+
$this->model->setData(
115+
array(
116+
'id' => 1000,
117+
'name' => 'Test Name'
118+
)
119+
);
120+
$this->assertEmpty($this->model->getStoredData());
121+
$this->model->afterLoad();
122+
$this->assertEquals($this->model->getData(), $this->model->getStoredData());
123+
$this->model->setData('value', 'Test Value');
124+
$this->model->afterSave();
125+
$this->assertEquals($this->model->getData(), $this->model->getStoredData());
126+
$this->model->afterDelete();
127+
$this->assertEmpty($this->model->getStoredData());
128+
}
111129
}

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

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ protected function setUp()
6161

6262
$this->_model = $this->getMockForAbstractClass(
6363
'Magento\Framework\Model\Resource\Db\AbstractDb',
64-
[$contextMock]
64+
[$contextMock],
65+
'',
66+
true,
67+
true,
68+
true,
69+
['_prepareDataForTable']
6570
);
6671
}
6772

@@ -422,4 +427,105 @@ public function hasDataChangedDataProvider()
422427
[null, false]
423428
];
424429
}
430+
431+
public function testPrepareDataForUpdate()
432+
{
433+
$adapterInterfaceMock = $this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface', [], [], '', false);
434+
$contextMock = new \Magento\Framework\Model\Context(
435+
$this->getMock('Psr\Log\LoggerInterface'),
436+
$this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false),
437+
$this->getMock('Magento\Framework\App\CacheInterface', [], [], '', false),
438+
$this->getMock('Magento\Framework\App\State', [], [], '', false),
439+
$this->getMock('\Magento\Framework\Model\ActionValidator\RemoveAction', [], [], '', false),
440+
$this->getMock('\Magento\Framework\Model\Resource\Db\ObjectRelationProcessor', [], [], '', false)
441+
);
442+
$registryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false);
443+
$resourceMock = $this->getMock(
444+
'Magento\Framework\Model\Resource\Db\AbstractDb',
445+
[
446+
'_construct',
447+
'_getReadAdapter',
448+
'_getWriteAdapter',
449+
'__wakeup',
450+
'commit',
451+
'delete',
452+
'getIdFieldName',
453+
'rollBack'
454+
],
455+
[],
456+
'',
457+
false
458+
);
459+
$adapterMock = $this->getMock(
460+
'Magento\Framework\DB\Adapter\AdapterInterface',
461+
[],
462+
[],
463+
'',
464+
false
465+
);
466+
$resourceMock->expects($this->any())
467+
->method('_getWriteAdapter')
468+
->will($this->returnValue($adapterMock));
469+
$resourceCollectionMock = $this->getMock(
470+
'Magento\Framework\Data\Collection\Db',
471+
[],
472+
[],
473+
'',
474+
false
475+
);
476+
$abstractModelMock = $this->getMockForAbstractClass(
477+
'Magento\Framework\Model\AbstractModel',
478+
[$contextMock, $registryMock, $resourceMock, $resourceCollectionMock]
479+
);
480+
$data = 'tableName';
481+
$this->_resourcesMock->expects($this->any())
482+
->method('getConnection')
483+
->will($this->returnValue($adapterInterfaceMock)
484+
);
485+
$this->_resourcesMock->expects($this->any())->method('getTableName')->with($data)->will(
486+
$this->returnValue('tableName')
487+
);
488+
$this->_resourcesMock->expects($this->any())
489+
->method('_getWriteAdapter')
490+
->will($this->returnValue($adapterInterfaceMock));
491+
$mainTableReflection = new \ReflectionProperty(
492+
'Magento\Framework\Model\Resource\Db\AbstractDb',
493+
'_mainTable'
494+
);
495+
$mainTableReflection->setAccessible(true);
496+
$mainTableReflection->setValue($this->_model, 'tableName');
497+
$idFieldNameReflection = new \ReflectionProperty(
498+
'Magento\Framework\Model\Resource\Db\AbstractDb',
499+
'_idFieldName'
500+
);
501+
$idFieldNameReflection->setAccessible(true);
502+
$idFieldNameReflection->setValue($this->_model, 'idFieldName');
503+
$adapterInterfaceMock->expects($this->any())->method('save')->with('tableName', 'idFieldName');
504+
$adapterInterfaceMock->expects($this->any())->method('quoteInto')->will($this->returnValue('idFieldName'));
505+
506+
$abstractModelMock->setIdFieldName('id');
507+
$abstractModelMock->setData(
508+
array(
509+
'id' => 12345,
510+
'name' => 'Test Name',
511+
'value' => 'Test Value'
512+
)
513+
);
514+
$abstractModelMock->afterLoad();
515+
$this->assertEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
516+
$newData = array ('value' => 'Test Value New');
517+
$this->_model->expects($this->once())->method('_prepareDataForTable')->will($this->returnValue($newData));
518+
$abstractModelMock->addData($newData);
519+
$this->assertNotEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
520+
$abstractModelMock->isObjectNew(false);
521+
$adapterInterfaceMock->expects($this->once())
522+
->method('update')
523+
->with(
524+
'tableName',
525+
$newData,
526+
'idFieldName'
527+
);
528+
529+
$this->_model->save($abstractModelMock);
530+
}
425531
}

0 commit comments

Comments
 (0)