AbstractEntityTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Test\Unit\Model\Entity\VersionControl;
  7. use Magento\Eav\Model\Entity\VersionControl\AbstractEntity;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  9. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  10. /**
  11. * Test for version control abstract entity model.
  12. */
  13. class AbstractEntityTest extends \Magento\Eav\Test\Unit\Model\Entity\AbstractEntityTest
  14. {
  15. /**
  16. * @var \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $entitySnapshot;
  19. /**
  20. * @var RelationComposite|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $entityRelationComposite;
  23. protected function setUp()
  24. {
  25. $this->entitySnapshot = $this->createPartialMock(
  26. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class,
  27. ['isModified', 'registerSnapshot']
  28. );
  29. $this->entityRelationComposite = $this->createPartialMock(
  30. \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite::class,
  31. ['processRelations']
  32. );
  33. parent::setUp();
  34. }
  35. /**
  36. * @param string $attributeCode
  37. * @param int $attributeSetId
  38. * @param array $productData
  39. * @param array $productOrigData
  40. *
  41. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  42. * @dataProvider productAttributesDataProvider
  43. */
  44. public function testSave($attributeCode, $attributeSetId, $productData, $productOrigData)
  45. {
  46. $object = $this->createPartialMock(
  47. \Magento\Catalog\Model\Product::class,
  48. ['getOrigData', '__wakeup', 'beforeSave', 'afterSave', 'validateBeforeSave']
  49. );
  50. $object->setEntityTypeId(1);
  51. foreach ($productData as $key => $value) {
  52. $object->setData($key, $value);
  53. }
  54. $object->expects($this->any())->method('getOrigData')->will($this->returnValue($productOrigData));
  55. $entityType = new \Magento\Framework\DataObject();
  56. $entityType->setEntityTypeCode('test');
  57. $entityType->setEntityTypeId(0);
  58. $entityType->setEntityTable('table');
  59. $attributes = $this->_getAttributes();
  60. $attribute = $this->_getAttributeMock($attributeCode, $attributeSetId);
  61. /** @var $backendModel \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend */
  62. $backendModel = $this->createPartialMock(
  63. \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
  64. [
  65. 'getBackend',
  66. 'getBackendTable',
  67. 'getAffectedFields',
  68. 'isStatic',
  69. 'getEntityValueId',
  70. ]
  71. );
  72. $backendModel->expects(
  73. $this->once()
  74. )->method(
  75. 'getAffectedFields'
  76. )->will(
  77. $this->returnValue(['test_table' => [['value_id' => 0, 'attribute_id' => $attributeCode]]])
  78. );
  79. $backendModel->expects($this->any())->method('isStatic')->will($this->returnValue(false));
  80. $backendModel->expects($this->never())->method('getEntityValueId');
  81. $backendModel->setAttribute($attribute);
  82. $attribute->expects($this->any())->method('getBackend')->will($this->returnValue($backendModel));
  83. $attribute->setId(222);
  84. $attributes[$attributeCode] = $attribute;
  85. $eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $objectManager = new ObjectManager($this);
  89. $this->entitySnapshot->expects($this->once())->method('isModified')->willReturn(true);
  90. $this->entitySnapshot->expects($this->once())->method('registerSnapshot')->with($object);
  91. $this->entityRelationComposite->expects($this->once())->method('processRelations')->with($object);
  92. $arguments = $objectManager->getConstructArguments(
  93. \Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class,
  94. [
  95. 'eavConfig' => $eavConfig,
  96. 'entitySnapshot' => $this->entitySnapshot,
  97. 'entityRelationComposite' => $this->entityRelationComposite,
  98. 'data' => [
  99. 'type' => $entityType,
  100. 'entityTable' => 'entityTable',
  101. 'attributesByCode' => $attributes
  102. ]
  103. ]
  104. );
  105. /** @var $model AbstractEntity|\PHPUnit_Framework_MockObject_MockObject */
  106. $model = $this->getMockBuilder(\Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class)
  107. ->setConstructorArgs($arguments)
  108. ->setMethods(['_getValue', 'beginTransaction', 'commit', 'rollback', 'getConnection'])
  109. ->getMock();
  110. $model->expects($this->any())->method('_getValue')->will($this->returnValue($eavConfig));
  111. $model->expects($this->any())->method('getConnection')->will($this->returnValue($this->_getConnectionMock()));
  112. $eavConfig->expects($this->any())->method('getAttribute')->will(
  113. $this->returnCallback(
  114. function ($entityType, $attributeCode) use ($attributes) {
  115. return $entityType && isset($attributes[$attributeCode]) ? $attributes[$attributeCode] : null;
  116. }
  117. )
  118. );
  119. $model->isPartialSave(true);
  120. $model->save($object);
  121. }
  122. public function testSaveNotModified()
  123. {
  124. $objectManager = new ObjectManager($this);
  125. /** @var $object \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
  126. $object = $this->createMock(\Magento\Catalog\Model\Product::class);
  127. $arguments = $objectManager->getConstructArguments(
  128. \Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class,
  129. [
  130. 'entitySnapshot' => $this->entitySnapshot,
  131. 'entityRelationComposite' => $this->entityRelationComposite,
  132. ]
  133. );
  134. /** @var $model AbstractEntity|\PHPUnit_Framework_MockObject_MockObject */
  135. $model = $this->getMockBuilder(\Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class)
  136. ->setConstructorArgs($arguments)
  137. ->setMethods(['beginTransaction', 'commit'])
  138. ->getMock();
  139. $this->entitySnapshot->expects($this->once())->method('isModified')->willReturn(false);
  140. $this->entitySnapshot->expects($this->never())->method('registerSnapshot');
  141. $this->entityRelationComposite->expects($this->once())->method('processRelations')->with($object);
  142. $model->save($object);
  143. }
  144. }