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 
13 
18 class AbstractEntityTest extends \PHPUnit\Framework\TestCase
19 {
24  protected $_model;
25 
27  protected $eavConfig;
28 
29  protected function setUp()
30  {
31  $objectManager = new ObjectManager($this);
32  $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class);
33  $arguments = $objectManager->getConstructArguments(
34  AbstractEntity::class,
35  ['eavConfig' => $this->eavConfig]
36  );
37  $this->_model = $this->getMockForAbstractClass(
38  AbstractEntity::class,
40  );
41  }
42 
43  protected function tearDown()
44  {
45  $this->_model = null;
46  }
47 
55  public function testCompareAttributes($attribute1Sort, $attribute2Sort, $expected)
56  {
57  $attribute1 = $this->createPartialMock(\Magento\Eav\Model\Entity\Attribute::class, ['__wakeup']);
58  $attribute1->setAttributeSetInfo([0 => $attribute1Sort]);
59  $attribute2 = $this->createPartialMock(\Magento\Eav\Model\Entity\Attribute::class, ['__wakeup']);
60  $attribute2->setAttributeSetInfo([0 => $attribute2Sort]);
61  $this->assertEquals($expected, $this->_model->attributesCompare($attribute1, $attribute2));
62  }
63 
67  public static function compareAttributesDataProvider()
68  {
69  return [
70  'attribute1 bigger than attribute2' => [
71  'attribute1Sort' => ['group_sort' => 7, 'sort' => 5],
72  'attribute2Sort' => ['group_sort' => 5, 'sort' => 10],
73  'expected' => 1,
74  ],
75  'attribute1 smaller than attribute2' => [
76  'attribute1Sort' => ['group_sort' => 7, 'sort' => 5],
77  'attribute2Sort' => ['group_sort' => 7, 'sort' => 10],
78  'expected' => -1,
79  ],
80  'attribute1 equals to attribute2' => [
81  'attribute1Sort' => ['group_sort' => 7, 'sort' => 5],
82  'attribute2Sort' => ['group_sort' => 7, 'sort' => 5],
83  'expected' => 0,
84  ]
85  ];
86  }
87 
93  protected function _getAttributes()
94  {
95  $attributes = [];
96  $codes = ['entity_type_id', 'attribute_set_id', 'created_at', 'updated_at', 'parent_id', 'increment_id'];
97  foreach ($codes as $code) {
98  $mock = $this->createPartialMock(
99  \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
100  ['getBackend', 'getBackendTable', '__wakeup']
101  );
102  $mock->setAttributeId($code);
103 
105  $backendModel = $this->createPartialMock(
106  \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
107  ['getBackend', 'getBackendTable']
108  );
109 
110  $backendModel->setAttribute($mock);
111 
112  $mock->expects($this->any())->method('getBackend')->will($this->returnValue($backendModel));
113 
114  $mock->expects($this->any())->method('getBackendTable')->will($this->returnValue($code . '_table'));
115 
116  $attributes[$code] = $mock;
117  }
118  return $attributes;
119  }
120 
126  protected function _getConnectionMock()
127  {
128  $connection = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [
129  'describeTable',
130  'getIndexList',
131  'lastInsertId',
132  'insert',
133  'prepareColumnValue',
134  'select',
135  'query',
136  'delete'
137  ]);
138  $statement = $this->createPartialMock(
139  \Zend_Db_Statement::class,
140  ['closeCursor', 'columnCount', 'errorCode', 'errorInfo', 'fetch', 'nextRowset', 'rowCount']
141  );
142 
143  $select = $this->createMock(\Magento\Framework\DB\Select::class);
144  $select->expects($this->any())
145  ->method('from')
146  ->willReturnSelf();
147 
148  $connection->expects($this->any())->method('query')->will($this->returnValue($statement));
149 
150  $connection->expects(
151  $this->any()
152  )->method(
153  'describeTable'
154  )->will(
155  $this->returnValue(['value' => ['test']])
156  );
157 
158  $connection->expects($this->any())->method('prepareColumnValue')->will($this->returnArgument(2));
159 
160  $connection->expects(
161  $this->once()
162  )->method(
163  'delete'
164  )->with(
165  $this->equalTo('test_table')
166  )->will(
167  $this->returnValue(true)
168  );
169 
170  $connection->expects($this->any())
171  ->method('select')
172  ->willReturn($select);
173 
174  $connection->expects($this->any())
175  ->method('getIndexList')
176  ->willReturn(
177  [
178  'PK_ENTITYTABLE' => [
179  'COLUMNS_LIST' => [
180  'entity_id'
181  ]
182  ]
183  ]
184  );
185 
186  return $connection;
187  }
188 
197  {
198  $attribute = $this->createPartialMock(
199  \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
200  ['getBackend', 'getBackendTable', 'isInSet', 'getApplyTo', 'getAttributeCode', '__wakeup']
201  );
202  $attribute->setAttributeId($attributeCode);
203 
204  $attribute->expects(
205  $this->any()
206  )->method(
207  'getBackendTable'
208  )->will(
209  $this->returnValue($attributeCode . '_table')
210  );
211 
212  $attribute->expects(
213  $this->any()
214  )->method(
215  'isInSet'
216  )->with(
217  $this->equalTo($attributeSetId)
218  )->will(
219  $this->returnValue(false)
220  );
221 
222  $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode));
223 
224  return $attribute;
225  }
226 
235  public function testSave($attributeCode, $attributeSetId, $productData, $productOrigData)
236  {
237  $object = $this->createPartialMock(
238  \Magento\Catalog\Model\Product::class,
239  ['getOrigData', '__wakeup', 'beforeSave', 'afterSave', 'validateBeforeSave']
240  );
241  $object->setEntityTypeId(1);
242  foreach ($productData as $key => $value) {
243  $object->setData($key, $value);
244  }
245  $object->expects($this->any())->method('getOrigData')->will($this->returnValue($productOrigData));
246 
247  $entityType = new \Magento\Framework\DataObject();
248  $entityType->setEntityTypeCode('test');
249  $entityType->setEntityTypeId(0);
250  $entityType->setEntityTable('table');
251 
252  $attributes = $this->_getAttributes();
253 
255 
257  $backendModel = $this->createPartialMock(
258  \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
259  [
260  'getBackend',
261  'getBackendTable',
262  'getAffectedFields',
263  'isStatic',
264  'getEntityValueId',
265  ]
266  );
267 
268  $backendModel->expects(
269  $this->once()
270  )->method(
271  'getAffectedFields'
272  )->will(
273  $this->returnValue(['test_table' => [['value_id' => 0, 'attribute_id' => $attributeCode]]])
274  );
275 
276  $backendModel->expects($this->any())->method('isStatic')->will($this->returnValue(false));
277 
278  $backendModel->expects($this->never())->method('getEntityValueId');
279 
280  $backendModel->setAttribute($attribute);
281  $attribute->expects($this->any())->method('getBackend')->will($this->returnValue($backendModel));
282  $attribute->setId(222);
284  $eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
285  ->disableOriginalConstructor()
286  ->getMock();
287  $objectManager = new ObjectManager($this);
288  $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class);
289  $arguments = $objectManager->getConstructArguments(
290  AbstractEntity::class,
291  [
292  'eavConfig' => $eavConfig,
293  'data' => [
294  'type' => $entityType,
295  'entityTable' => 'entityTable',
296  'attributesByCode' => $attributes
297  ]
298  ]
299  );
301  $model = $this->getMockBuilder(AbstractEntity::class)
302  ->setConstructorArgs($arguments)
303  ->setMethods(['_getValue', 'beginTransaction', 'commit', 'rollback', 'getConnection'])
304  ->getMock();
305  $model->expects($this->any())->method('_getValue')->will($this->returnValue($eavConfig));
306  $model->expects($this->any())->method('getConnection')->will($this->returnValue($this->_getConnectionMock()));
307 
308  $eavConfig->expects($this->any())->method('getAttribute')->will(
309  $this->returnCallback(
310  function ($entityType, $attributeCode) use ($attributes) {
312  }
313  )
314  );
315  $model->isPartialSave(true);
316  $model->save($object);
317  }
318 
323  {
324  $attributeSetId = 10;
325  return [
326  [
327  'test_attr',
329  [
330  'test_attr' => 'test_attr',
331  'attribute_set_id' => $attributeSetId,
332  'entity_id' => null,
333  'store_id' => 1
334  ],
335  null,
336  ],
337  [
338  'test_attr',
340  [
341  'test_attr' => 'test_attr',
342  'attribute_set_id' => $attributeSetId,
343  'entity_id' => 12345,
344  'store_id' => 1
345  ],
346  ['test_attr' => 'test_attr']
347  ],
348  [
349  'test_attr',
351  ['test_attr' => '99.99', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345, 'store_id' => 1],
352  ['test_attr' => '99.9900']
353  ]
354  ];
355  }
356 
360  public function testDuplicateExceptionProcessingOnSave()
361  {
362  $connection = $this->createMock(AdapterInterface::class);
363  $connection->expects($this->once())->method('rollback');
364 
366  $model = $this->getMockBuilder(AbstractEntity::class)
367  ->disableOriginalConstructor()
368  ->setMethods(['getConnection'])
369  ->getMockForAbstractClass();
370  $model->expects($this->any())->method('getConnection')->willReturn($connection);
371 
373  $object = $this->getMockBuilder(AbstractModel::class)
374  ->disableOriginalConstructor()
375  ->getMock();
376  $object->expects($this->once())->method('hasDataChanges')->willReturn(true);
377  $object->expects($this->once())->method('beforeSave')->willThrowException(new DuplicateException());
378  $object->expects($this->once())->method('setHasDataChanges')->with(true);
379 
380  $model->save($object);
381  }
382 }
$objectManager
Definition: bootstrap.php:17
testCompareAttributes($attribute1Sort, $attribute2Sort, $expected)
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
$productData
$arguments
$connection
Definition: bulk.php:13
$code
Definition: info.phtml:12