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