41 $this->_resourcesMock = $this->createMock(\
Magento\Framework\
App\ResourceConnection::class);
43 $this->relationProcessorMock =
45 $this->transactionManagerMock = $this->createMock(
49 $contextMock->expects($this->once())->method(
'getResources')->willReturn($this->_resourcesMock);
50 $contextMock->expects($this->once())
51 ->method(
'getObjectRelationProcessor')
52 ->willReturn($this->relationProcessorMock);
53 $contextMock->expects($this->once())
54 ->method(
'getTransactionManager')
55 ->willReturn($this->transactionManagerMock);
57 $this->_model = $this->getMockForAbstractClass(
64 [
'_prepareDataForTable']
75 $this->_model->addUniqueField($fieldNameType);
76 $this->assertEquals($expectedResult, $this->_model->getUniqueFields());
92 'FieldNameArraySecond',
97 'FieldNameArraySecond',
110 $this->assertInstanceOf(
112 $this->_model->addUniqueField([
'someField'])
122 $this->_model->getIdFieldName();
127 $data =
'MainTableName';
128 $idFieldNameProperty = new \ReflectionProperty(
132 $idFieldNameProperty->setAccessible(
true);
133 $idFieldNameProperty->setValue($this->_model,
$data);
134 $this->assertEquals(
$data, $this->_model->getIdFieldName());
143 $this->_model->getMainTable();
153 $mainTableProperty = new \ReflectionProperty(
157 $mainTableProperty->setAccessible(
true);
158 $mainTableProperty->setValue($this->_model,
$tableName);
159 $this->_resourcesMock->expects($this->once())
160 ->method(
'getTableName')
161 ->with($expectedResult)
162 ->will($this->returnValue($expectedResult));
163 $this->assertEquals($expectedResult, $this->_model->getMainTable());
181 'tableName_entity_suffix' 189 $this->_resourcesMock->expects($this->once())->method(
'getTableName')->with(
$data)->will(
190 $this->returnValue(
'tableName')
192 $tablesProperty = new \ReflectionProperty(
196 $tablesProperty->setAccessible(
true);
197 $tablesProperty->setValue($this->_model, [
$data]);
198 $this->assertEquals(
$data, $this->_model->getTable(
$data));
203 $this->assertEquals(
false, $this->_model->getChecksum(
null));
213 $connectionMock = $this->createMock(AdapterInterface::class);
214 $connectionMock->expects($this->once())->method(
'getTablesChecksum')->with($checksum)->will(
215 $this->returnValue([$checksum =>
'checksum'])
217 $this->_resourcesMock->expects($this->any())->method(
'getConnection')->will(
218 $this->returnValue($connectionMock)
220 $this->assertEquals($expected, $this->_model->getChecksum($checksum));
242 $uniqueFields = new \ReflectionProperty(
246 $uniqueFields->setAccessible(
true);
247 $uniqueFields->setValue($this->_model, [
'uniqueField1',
'uniqueField2']);
248 $this->_model->resetUniqueField();
249 $this->assertEquals([], $this->_model->getUniqueFields());
254 $uniqueFieldsReflection = new \ReflectionProperty(
258 $uniqueFieldsReflection->setAccessible(
true);
259 $uniqueFieldsReflection->setValue($this->_model,
null);
260 $this->assertEquals([], $this->_model->getUniqueFields());
265 $this->assertNull($this->_model->getValidationRulesBeforeSave());
268 public function testLoad()
271 $object = $this->getMockBuilder(\
Magento\Framework\Model\AbstractModel::class)
272 ->disableOriginalConstructor()
274 $object->expects($this->once())->method(
'beforeLoad')->with(
'some_value',
'field_name');
275 $object->expects($this->once())->method(
'afterLoad')->willReturnSelf();
276 $object->expects($this->once())->method(
'setOrigData')->willReturnSelf();
277 $object->expects($this->once())->method(
'setHasDataChanges')->with(
false)->willReturnSelf();
278 $result = $this->_model->load($object,
'some_value',
'field_name');
279 $this->assertEquals($this->_model,
$result);
280 $this->assertInstanceOf(
288 $connectionInterfaceMock = $this->createMock(AdapterInterface::class);
289 $contextMock = $this->createMock(\
Magento\Framework\Model\Context::class);
290 $registryMock = $this->createMock(\
Magento\Framework\Registry::class);
291 $abstractModelMock = $this->getMockForAbstractClass(
292 AbstractModel::class,
293 [$contextMock, $registryMock],
298 [
'__wakeup',
'getId',
'beforeDelete',
'afterDelete',
'afterDeleteCommit',
'getData']
300 $this->_resourcesMock->expects($this->any())
301 ->method(
'getConnection')
302 ->will($this->returnValue($connectionInterfaceMock));
304 $abstractModelMock->expects($this->atLeastOnce())->method(
'getId')->willReturn(1);
305 $abstractModelMock->expects($this->once())->method(
'getData')->willReturn([
'data' =>
'value']);
306 $connectionMock = $this->createMock(AdapterInterface::class);
307 $this->transactionManagerMock->expects($this->once())
309 ->with($connectionInterfaceMock)
310 ->willReturn($connectionMock);
312 $this->relationProcessorMock->expects($this->once())
315 $this->transactionManagerMock,
322 $this->transactionManagerMock->expects($this->once())->method(
'commit');
325 $this->_resourcesMock->expects($this->any())->method(
'getTableName')->with(
$data)->will(
326 $this->returnValue(
'tableName')
328 $mainTableReflection = new \ReflectionProperty(
332 $mainTableReflection->setAccessible(
true);
333 $mainTableReflection->setValue($this->_model,
'tableName');
334 $idFieldNameReflection = new \ReflectionProperty(
338 $idFieldNameReflection->setAccessible(
true);
339 $idFieldNameReflection->setValue($this->_model,
'idFieldName');
340 $connectionInterfaceMock->expects($this->any())->method(
'delete')->with(
'tableName',
'idFieldName');
341 $connectionInterfaceMock->expects($this->any())->method(
'quoteInto')->will($this->returnValue(
'idFieldName'));
342 $abstractModelMock->expects($this->once())->method(
'beforeDelete');
343 $abstractModelMock->expects($this->once())->method(
'afterDelete');
344 $abstractModelMock->expects($this->once())->method(
'afterDeleteCommit');
345 $this->assertInstanceOf(
347 $this->_model->delete($abstractModelMock)
353 $contextMock = $this->createMock(\
Magento\Framework\Model\Context::class);
354 $registryMock = $this->createMock(\
Magento\Framework\Registry::class);
355 $abstractModelMock = $this->getMockForAbstractClass(
356 AbstractModel::class,
357 [$contextMock, $registryMock],
362 [
'__wakeup',
'getOrigData']
364 $abstractModelMock->expects($this->any())->method(
'getOrigData')->will($this->returnValue(
false));
365 $this->assertTrue($this->_model->hasDataChanged($abstractModelMock));
375 $connectionInterfaceMock = $this->createMock(AdapterInterface::class);
376 $this->_resourcesMock->expects($this->any())->method(
'getConnection')->will(
377 $this->returnValue($connectionInterfaceMock)
379 $contextMock = $this->createMock(\
Magento\Framework\Model\Context::class);
380 $registryMock = $this->createMock(\
Magento\Framework\Registry::class);
381 $abstractModelMock = $this->getMockForAbstractClass(
382 AbstractModel::class,
383 [$contextMock, $registryMock],
388 [
'__wakeup',
'getOrigData',
'getData']
390 $mainTableProperty = new \ReflectionProperty(
394 $mainTableProperty->setAccessible(
true);
395 $mainTableProperty->setValue($this->_model,
'table');
397 $this->_resourcesMock->expects($this->once())
398 ->method(
'getTableName')
400 ->will($this->returnValue(
'tableName'));
401 $abstractModelMock->expects($this->at(0))->method(
'getOrigData')->will($this->returnValue(
true));
402 $abstractModelMock->expects($this->at(1))->method(
'getOrigData')->will($this->returnValue($getOriginData));
403 $connectionInterfaceMock->expects($this->any())->method(
'describeTable')->with(
'tableName')->will(
404 $this->returnValue([
'tableName'])
406 $this->assertEquals($expected, $this->_model->hasDataChanged($abstractModelMock));
425 $connectionMock = $this->getMockBuilder(AdapterInterface::class)
426 ->setMethods([
'save'])
427 ->getMockForAbstractClass();
428 $context = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
429 \
Magento\Framework\Model\Context::class
431 $registryMock = $this->createMock(\
Magento\Framework\Registry::class);
432 $resourceMock = $this->createPartialMock(AbstractDb::class, [
438 $connectionInterfaceMock = $this->createMock(AdapterInterface::class);
439 $resourceMock->expects($this->any())
440 ->method(
'getConnection')
441 ->will($this->returnValue($connectionInterfaceMock));
442 $resourceCollectionMock = $this->getMockBuilder(\
Magento\Framework\Data\Collection\AbstractDb::class)
443 ->disableOriginalConstructor()
444 ->getMockForAbstractClass();
445 $abstractModelMock = $this->getMockForAbstractClass(
446 AbstractModel::class,
447 [$context, $registryMock, $resourceMock, $resourceCollectionMock]
450 $this->_resourcesMock->expects($this->any())
451 ->method(
'getConnection')
452 ->will($this->returnValue($connectionMock));
453 $this->_resourcesMock->expects($this->any())->method(
'getTableName')->with(
$data)->will(
454 $this->returnValue(
'tableName')
456 $mainTableReflection = new \ReflectionProperty(
460 $mainTableReflection->setAccessible(
true);
461 $mainTableReflection->setValue($this->_model,
'tableName');
462 $idFieldNameReflection = new \ReflectionProperty(
466 $idFieldNameReflection->setAccessible(
true);
467 $idFieldNameReflection->setValue($this->_model,
'idFieldName');
468 $connectionMock->expects($this->any())->method(
'save')->with(
'tableName',
'idFieldName');
469 $connectionMock->expects($this->any())->method(
'quoteInto')->will($this->returnValue(
'idFieldName'));
470 $abstractModelMock->setIdFieldName(
'id');
471 $abstractModelMock->setData(
474 'name' =>
'Test Name',
475 'value' =>
'Test Value' 478 $abstractModelMock->afterLoad();
479 $this->assertEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
480 $newData = [
'value' =>
'Test Value New'];
481 $this->_model->expects($this->atLeastOnce())
482 ->method(
'_prepareDataForTable')
483 ->will($this->returnValue($newData));
484 $abstractModelMock->addData($newData);
485 $this->assertNotEquals($abstractModelMock->getData(), $abstractModelMock->getStoredData());
486 $abstractModelMock->isObjectNew(
false);
487 $connectionMock->expects($this->once())
494 $select = $this->getMockBuilder(\
Magento\Framework\DB\Select::class)
495 ->disableOriginalConstructor()
497 $select->expects($this->once())
501 $connectionMock->expects($this->once())
504 $select->expects($this->once())
506 ->with(\
Magento\Framework\DB\Select::WHERE);
507 $select->expects($this->exactly(2))
509 ->withConsecutive([
'uniqueField IS NULL'], [
'idFieldName!=?', 0]);
510 $this->_model->addUniqueField([
'field' =>
'uniqueField']);
511 $this->_model->save($abstractModelMock);
525 $model = $this->getMockBuilder(AbstractDb::class)
526 ->disableOriginalConstructor()
527 ->setMethods([
'_prepareDataForSave',
'getIdFieldName',
'getConnection',
'getMainTable'])
528 ->getMockForAbstractClass();
535 $reflectionMethod = new \ReflectionMethod(
$model,
'saveNewObject');
536 $reflectionMethod->setAccessible(
true);
537 $reflectionProperty = new \ReflectionProperty(
$model,
'_isPkAutoIncrement');
538 $reflectionProperty->setAccessible(
true);
539 $reflectionProperty->setValue(
$model, $pkIncrement);
542 $connectionMock = $this->getMockBuilder(AdapterInterface::class)
543 ->disableOriginalConstructor()
544 ->setMethods([
'lastInsertId'])
545 ->getMockForAbstractClass();
546 $getConnectionInvokedCount = $pkIncrement ? 2 : 1;
547 $model->expects($this->exactly($getConnectionInvokedCount))
548 ->method(
'getConnection')
549 ->willReturn($connectionMock);
551 $idFieldName =
'id_field_name';
552 $model->expects($this->once())->method(
'_prepareDataForSave')->willReturn([$idFieldName =>
'id']);
556 $getIdFieldNameInvokedCount = $pkIncrement ? 1 : 0;
557 $model->expects($this->exactly($getIdFieldNameInvokedCount))
558 ->method(
'getIdFieldName')
559 ->willReturn($idFieldName);
562 $setIdInvokedCount = $pkIncrement ? 1 : 0;
563 $inputObject = $this->getMockBuilder(AbstractModel::class)
564 ->disableOriginalConstructor()
566 $inputObject->expects($this->exactly($setIdInvokedCount))->method(
'setId');
569 $lastInsertIdInvokedCount = $pkIncrement ? 1 : 0;
570 $connectionMock->expects($this->exactly($lastInsertIdInvokedCount))->method(
'lastInsertId');
572 $reflectionMethod->invokeArgs(
$model, [$inputObject]);
580 return [[
true], [
false]];
586 public function testDuplicateExceptionProcessingOnSave()
588 $connection = $this->createMock(AdapterInterface::class);
589 $connection->expects($this->once())->method(
'rollback');
592 $model = $this->getMockBuilder(AbstractDb::class)
593 ->disableOriginalConstructor()
594 ->setMethods([
'getConnection'])
595 ->getMockForAbstractClass();
599 $object = $this->getMockBuilder(AbstractModel::class)
600 ->disableOriginalConstructor()
602 $object->expects($this->once())->method(
'hasDataChanges')->willReturn(
true);
603 $object->expects($this->once())->method(
'beforeSave')->willThrowException(
new DuplicateException());
604 $object->expects($this->once())->method(
'setHasDataChanges')->with(
true);
testAddUniqueFieldArray()
testGetIdFieldNameException()
testGetChecksum($checksum, $expected)
saveNewObjectDataProvider()
testGetMainTableException()
testGetDataChanged($getOriginData, $expected)
addUniqueFieldDataProvider()
testHasDataChangedNegative()
testSaveNewObject($pkIncrement)
hasDataChangedDataProvider()
testGetMainTable($tableName, $expectedResult)
testAddUniqueField($fieldNameType, $expectedResult)
testPrepareDataForUpdate()
testGetValidationRulesBeforeSave()
testGetChecksumNegative()