Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractEntityTest.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
20  protected $entitySnapshot;
21 
26 
27  protected function setUp()
28  {
29  $this->entitySnapshot = $this->createPartialMock(
30  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot::class,
31  ['isModified', 'registerSnapshot']
32  );
33 
34  $this->entityRelationComposite = $this->createPartialMock(
35  \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite::class,
36  ['processRelations']
37  );
38 
39  parent::setUp();
40  }
41 
51  public function testSave($attributeCode, $attributeSetId, $productData, $productOrigData)
52  {
53  $object = $this->createPartialMock(
54  \Magento\Catalog\Model\Product::class,
55  ['getOrigData', '__wakeup', 'beforeSave', 'afterSave', 'validateBeforeSave']
56  );
57 
58  $object->setEntityTypeId(1);
59  foreach ($productData as $key => $value) {
60  $object->setData($key, $value);
61  }
62  $object->expects($this->any())->method('getOrigData')->will($this->returnValue($productOrigData));
63 
64  $entityType = new \Magento\Framework\DataObject();
65  $entityType->setEntityTypeCode('test');
66  $entityType->setEntityTypeId(0);
67  $entityType->setEntityTable('table');
68 
69  $attributes = $this->_getAttributes();
70 
72 
74  $backendModel = $this->createPartialMock(
75  \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
76  [
77  'getBackend',
78  'getBackendTable',
79  'getAffectedFields',
80  'isStatic',
81  'getEntityValueId',
82  ]
83  );
84 
85  $backendModel->expects(
86  $this->once()
87  )->method(
88  'getAffectedFields'
89  )->will(
90  $this->returnValue(['test_table' => [['value_id' => 0, 'attribute_id' => $attributeCode]]])
91  );
92 
93  $backendModel->expects($this->any())->method('isStatic')->will($this->returnValue(false));
94  $backendModel->expects($this->never())->method('getEntityValueId');
95  $backendModel->setAttribute($attribute);
96 
97  $attribute->expects($this->any())->method('getBackend')->will($this->returnValue($backendModel));
98  $attribute->setId(222);
100  $eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103 
104  $objectManager = new ObjectManager($this);
105 
106  $this->entitySnapshot->expects($this->once())->method('isModified')->willReturn(true);
107  $this->entitySnapshot->expects($this->once())->method('registerSnapshot')->with($object);
108 
109  $this->entityRelationComposite->expects($this->once())->method('processRelations')->with($object);
110 
111  $arguments = $objectManager->getConstructArguments(
112  \Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class,
113  [
114  'eavConfig' => $eavConfig,
115  'entitySnapshot' => $this->entitySnapshot,
116  'entityRelationComposite' => $this->entityRelationComposite,
117  'data' => [
118  'type' => $entityType,
119  'entityTable' => 'entityTable',
120  'attributesByCode' => $attributes
121  ]
122  ]
123  );
124 
126  $model = $this->getMockBuilder(\Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class)
127  ->setConstructorArgs($arguments)
128  ->setMethods(['_getValue', 'beginTransaction', 'commit', 'rollback', 'getConnection'])
129  ->getMock();
130 
131  $model->expects($this->any())->method('_getValue')->will($this->returnValue($eavConfig));
132  $model->expects($this->any())->method('getConnection')->will($this->returnValue($this->_getConnectionMock()));
133 
134  $eavConfig->expects($this->any())->method('getAttribute')->will(
135  $this->returnCallback(
136  function ($entityType, $attributeCode) use ($attributes) {
138  }
139  )
140  );
141 
142  $model->isPartialSave(true);
143  $model->save($object);
144  }
145 
146  public function testSaveNotModified()
147  {
148  $objectManager = new ObjectManager($this);
149 
151  $object = $this->createMock(\Magento\Catalog\Model\Product::class);
152 
153  $arguments = $objectManager->getConstructArguments(
154  \Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class,
155  [
156  'entitySnapshot' => $this->entitySnapshot,
157  'entityRelationComposite' => $this->entityRelationComposite,
158  ]
159  );
160 
162  $model = $this->getMockBuilder(\Magento\Eav\Model\Entity\VersionControl\AbstractEntity::class)
163  ->setConstructorArgs($arguments)
164  ->setMethods(['beginTransaction', 'commit'])
165  ->getMock();
166 
167  $this->entitySnapshot->expects($this->once())->method('isModified')->willReturn(false);
168  $this->entitySnapshot->expects($this->never())->method('registerSnapshot');
169 
170  $this->entityRelationComposite->expects($this->once())->method('processRelations')->with($object);
171 
172  $model->save($object);
173  }
174 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
$productData
$arguments