@@ -61,7 +61,12 @@ protected function setUp()
61
61
62
62
$ this ->_model = $ this ->getMockForAbstractClass (
63
63
'Magento\Framework\Model\Resource\Db\AbstractDb ' ,
64
- [$ contextMock ]
64
+ [$ contextMock ],
65
+ '' ,
66
+ true ,
67
+ true ,
68
+ true ,
69
+ ['_prepareDataForTable ' ]
65
70
);
66
71
}
67
72
@@ -422,4 +427,105 @@ public function hasDataChangedDataProvider()
422
427
[null , false ]
423
428
];
424
429
}
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
+ }
425
531
}
0 commit comments