7
7
namespace Magento \ConfigurableProduct \Test \Unit \Plugin \Model \ResourceModel ;
8
8
9
9
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
10
+ use Magento \Catalog \Model \Product as ModelProduct ;
10
11
use Magento \Catalog \Model \Product \Type ;
11
12
use Magento \Catalog \Model \ProductAttributeSearchResults ;
12
13
use Magento \Catalog \Model \ResourceModel \Eav \Attribute as EavAttribute ;
14
+ use Magento \Catalog \Model \ResourceModel \Product as ResourceModelProduct ;
13
15
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
14
16
use Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute as ConfigurableAttribute ;
17
+ use Magento \ConfigurableProduct \Plugin \Model \ResourceModel \Product as PluginResourceModelProduct ;
15
18
use Magento \Framework \Api \ExtensionAttributesInterface ;
16
19
use Magento \Framework \Api \FilterBuilder ;
17
20
use Magento \Framework \Api \SearchCriteria ;
18
21
use Magento \Framework \Api \SearchCriteriaBuilder ;
22
+ use Magento \Framework \Exception \NoSuchEntityException ;
19
23
use Magento \Framework \Indexer \ActionInterface ;
24
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
25
+ use PHPUnit \Framework \MockObject \MockObject ;
26
+ use PHPUnit \Framework \TestCase ;
20
27
21
28
/**
22
29
* Unit test and integration test for plugin
23
30
*
24
31
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
32
*/
26
- class ProductTest extends \ PHPUnit \ Framework \ TestCase
33
+ class ProductTest extends TestCase
27
34
{
28
35
/**
29
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
36
+ * @var PluginResourceModelProduct
30
37
*/
31
- private $ objectManager ;
38
+ private $ model ;
32
39
33
40
/**
34
- * @var Configurable|\PHPUnit_Framework_MockObject_MockObject
41
+ * @var ObjectManagerHelper
35
42
*/
36
- private $ configurableMock ;
43
+ private $ objectManagerHelper ;
37
44
38
45
/**
39
- * @var ActionInterface|\PHPUnit_Framework_MockObject_MockObject
46
+ * @var Configurable|MockObject
40
47
*/
41
- private $ actionMock ;
48
+ private $ configurableMock ;
42
49
43
50
/**
44
- * @var \Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product
51
+ * @var ActionInterface|MockObject
45
52
*/
46
- private $ model ;
53
+ private $ actionMock ;
47
54
48
55
/**
49
- * @var ProductAttributeRepositoryInterface|\PHPUnit\Framework\MockObject\ MockObject
56
+ * @var ProductAttributeRepositoryInterface|MockObject
50
57
*/
51
58
private $ productAttributeRepositoryMock ;
52
59
53
60
/**
54
- * @var SearchCriteriaBuilder|\PHPUnit\Framework\MockObject\ MockObject
61
+ * @var SearchCriteriaBuilder|MockObject
55
62
*/
56
63
private $ searchCriteriaBuilderMock ;
57
64
58
65
/**
59
- * @var FilterBuilder|\PHPUnit\Framework\MockObject\ MockObject
66
+ * @var FilterBuilder|MockObject
60
67
*/
61
68
private $ filterBuilderMock ;
62
69
70
+ /**
71
+ * @inheritDoc
72
+ */
63
73
public function setUp ()
64
74
{
65
- $ this ->objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
75
+ $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
66
76
$ this ->configurableMock = $ this ->createMock (Configurable::class);
67
77
$ this ->actionMock = $ this ->createMock (ActionInterface::class);
68
78
$ this ->productAttributeRepositoryMock = $ this ->getMockBuilder (ProductAttributeRepositoryInterface::class)
@@ -78,8 +88,8 @@ public function setUp()
78
88
['setField ' , 'setConditionType ' , 'setValue ' , 'create ' ]
79
89
);
80
90
81
- $ this ->model = $ this ->objectManager ->getObject (
82
- \ Magento \ ConfigurableProduct \ Plugin \ Model \ ResourceModel \Product ::class,
91
+ $ this ->model = $ this ->objectManagerHelper ->getObject (
92
+ PluginResourceModelProduct ::class,
83
93
[
84
94
'configurable ' => $ this ->configurableMock ,
85
95
'productIndexer ' => $ this ->actionMock ,
@@ -90,13 +100,17 @@ public function setUp()
90
100
);
91
101
}
92
102
93
- public function testBeforeSaveConfigurable ()
103
+ /**
104
+ * @return void
105
+ * @throws NoSuchEntityException
106
+ */
107
+ public function testBeforeSaveConfigurable ():void
94
108
{
95
- /** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
96
- $ subject = $ this ->createMock (\ Magento \ Catalog \ Model \ ResourceModel \Product ::class);
97
- /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $object */
109
+ /** @var ResourceModelProduct|MockObject $subject */
110
+ $ subject = $ this ->createMock (ResourceModelProduct ::class);
111
+ /** @var ModelProduct|MockObject $object */
98
112
$ object = $ this ->createPartialMock (
99
- \ Magento \ Catalog \ Model \Product ::class,
113
+ ModelProduct ::class,
100
114
[
101
115
'getTypeId ' ,
102
116
'getTypeInstance ' ,
@@ -105,7 +119,7 @@ public function testBeforeSaveConfigurable()
105
119
]
106
120
);
107
121
$ type = $ this ->createPartialMock (
108
- \ Magento \ ConfigurableProduct \ Model \ Product \ Type \ Configurable::class,
122
+ Configurable::class,
109
123
['getSetAttributes ' ]
110
124
);
111
125
@@ -117,81 +131,128 @@ public function testBeforeSaveConfigurable()
117
131
ConfigurableAttribute::class,
118
132
['getAttributeId ' ]
119
133
);
120
- $ extensionAttributes ->expects ($ this ->exactly (2 ))->method ('getConfigurableProductOptions ' )
134
+ $ extensionAttributes ->expects ($ this ->exactly (2 ))
135
+ ->method ('getConfigurableProductOptions ' )
121
136
->willReturn ([$ option ]);
122
- $ object ->expects ($ this ->once ())->method ('getExtensionAttributes ' )
137
+ $ object ->expects ($ this ->once ())
138
+ ->method ('getExtensionAttributes ' )
123
139
->willReturn ($ extensionAttributes );
124
140
125
- $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())->method ('setField ' )->willReturnSelf ();
126
- $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())->method ('setValue ' )->willReturnSelf ();
127
- $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())->method ('setConditionType ' )->willReturnSelf ();
128
- $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturnSelf ();
141
+ $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())
142
+ ->method ('setField ' )
143
+ ->willReturnSelf ();
144
+ $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())
145
+ ->method ('setValue ' )
146
+ ->willReturnSelf ();
147
+ $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())
148
+ ->method ('setConditionType ' )
149
+ ->willReturnSelf ();
150
+ $ this ->filterBuilderMock ->expects ($ this ->atLeastOnce ())
151
+ ->method ('create ' )
152
+ ->willReturnSelf ();
129
153
$ searchCriteria = $ this ->createMock (SearchCriteria::class);
130
- $ this ->searchCriteriaBuilderMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchCriteria );
154
+ $ this ->searchCriteriaBuilderMock ->expects ($ this ->once ())
155
+ ->method ('create ' )
156
+ ->willReturn ($ searchCriteria );
131
157
132
158
$ searchResultMockClass = $ this ->createPartialMock (
133
159
ProductAttributeSearchResults::class,
134
160
['getItems ' ]
135
161
);
136
162
$ this ->productAttributeRepositoryMock ->expects ($ this ->once ())
137
- ->method ('getList ' )->with ($ searchCriteria )->willReturn ($ searchResultMockClass );
163
+ ->method ('getList ' )
164
+ ->with ($ searchCriteria )
165
+ ->willReturn ($ searchResultMockClass );
138
166
$ optionAttribute = $ this ->createPartialMock (
139
167
EavAttribute::class,
140
168
['getAttributeCode ' ]
141
169
);
142
- $ searchResultMockClass ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ optionAttribute ]);
143
- $ type ->expects ($ this ->once ())->method ('getSetAttributes ' )->with ($ object );
170
+ $ searchResultMockClass ->expects ($ this ->once ())
171
+ ->method ('getItems ' )
172
+ ->willReturn ([$ optionAttribute ]);
173
+ $ type ->expects ($ this ->once ())
174
+ ->method ('getSetAttributes ' )
175
+ ->with ($ object );
144
176
145
- $ object ->expects ($ this ->once ())->method ('getTypeId ' )->will ($ this ->returnValue (Configurable::TYPE_CODE ));
146
- $ object ->expects ($ this ->once ())->method ('getTypeInstance ' )->will ($ this ->returnValue ($ type ));
147
- $ object ->expects ($ this ->once ())->method ('setData ' );
148
- $ option ->expects ($ this ->once ())->method ('getAttributeId ' );
149
- $ optionAttribute ->expects ($ this ->once ())->method ('getAttributeCode ' );
177
+ $ object ->expects ($ this ->once ())
178
+ ->method ('getTypeId ' )
179
+ ->will ($ this ->returnValue (Configurable::TYPE_CODE ));
180
+ $ object ->expects ($ this ->once ())
181
+ ->method ('getTypeInstance ' )
182
+ ->will ($ this ->returnValue ($ type ));
183
+ $ object ->expects ($ this ->once ())
184
+ ->method ('setData ' );
185
+ $ option ->expects ($ this ->once ())
186
+ ->method ('getAttributeId ' );
187
+ $ optionAttribute ->expects ($ this ->once ())
188
+ ->method ('getAttributeCode ' );
150
189
151
190
$ this ->model ->beforeSave (
152
191
$ subject ,
153
192
$ object
154
193
);
155
194
}
156
195
157
- public function testBeforeSaveSimple ()
196
+ /**
197
+ * @return void
198
+ * @throws NoSuchEntityException
199
+ */
200
+ public function testBeforeSaveSimple ():void
158
201
{
159
- /** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
160
- $ subject = $ this ->createMock (\Magento \Catalog \Model \ResourceModel \Product::class);
161
- /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $object */
162
- $ object = $ this ->createPartialMock (\Magento \Catalog \Model \Product::class, ['getTypeId ' , 'getTypeInstance ' ]);
163
- $ object ->expects ($ this ->once ())->method ('getTypeId ' )->will ($ this ->returnValue (Type::TYPE_SIMPLE ));
164
- $ object ->expects ($ this ->never ())->method ('getTypeInstance ' );
202
+ /** @var ResourceModelProduct|MockObject $subject */
203
+ $ subject = $ this ->createMock (ResourceModelProduct::class);
204
+ /** @var ModelProduct|MockObject $object */
205
+ $ object = $ this ->createPartialMock (
206
+ ModelProduct::class,
207
+ [
208
+ 'getTypeId ' ,
209
+ 'getTypeInstance '
210
+ ]
211
+ );
212
+ $ object ->expects ($ this ->once ())
213
+ ->method ('getTypeId ' )
214
+ ->will ($ this ->returnValue (Type::TYPE_SIMPLE ));
215
+ $ object ->expects ($ this ->never ())
216
+ ->method ('getTypeInstance ' );
165
217
166
218
$ this ->model ->beforeSave (
167
219
$ subject ,
168
220
$ object
169
221
);
170
222
}
171
223
172
- public function testAroundDelete ()
224
+ /**
225
+ * @return void
226
+ */
227
+ public function testAroundDelete ():void
173
228
{
174
229
$ productId = '1 ' ;
175
230
$ parentConfigId = ['2 ' ];
176
- /** @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject $subject */
177
- $ subject = $ this ->createMock (\ Magento \ Catalog \ Model \ ResourceModel \Product ::class);
178
- /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */
231
+ /** @var ResourceModelProduct|MockObject $subject */
232
+ $ subject = $ this ->createMock (ResourceModelProduct ::class);
233
+ /** @var ModelProduct|MockObject $product */
179
234
$ product = $ this ->createPartialMock (
180
- \ Magento \ Catalog \ Model \Product ::class,
235
+ ModelProduct ::class,
181
236
['getId ' , 'delete ' ]
182
237
);
183
- $ product ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ productId );
184
- $ product ->expects ($ this ->once ())->method ('delete ' )->willReturn (true );
238
+ $ product ->expects ($ this ->once ())
239
+ ->method ('getId ' )
240
+ ->willReturn ($ productId );
241
+ $ product ->expects ($ this ->once ())
242
+ ->method ('delete ' )
243
+ ->willReturn (true );
185
244
$ this ->configurableMock ->expects ($ this ->once ())
186
245
->method ('getParentIdsByChild ' )
187
246
->with ($ productId )
188
247
->willReturn ($ parentConfigId );
189
- $ this ->actionMock ->expects ($ this ->once ())->method ('executeList ' )->with ($ parentConfigId );
248
+ $ this ->actionMock ->expects ($ this ->once ())
249
+ ->method ('executeList ' )
250
+ ->with ($ parentConfigId );
190
251
191
252
$ return = $ this ->model ->aroundDelete (
192
253
$ subject ,
193
- /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $prod */
194
- function (\ Magento \ Catalog \ Model \ Product $ prod ) use ($ subject ) {
254
+ /** @var ModelProduct|MockObject $prod */
255
+ function (ModelProduct $ prod ) use ($ subject ) {
195
256
$ prod ->delete ();
196
257
return $ subject ;
197
258
},
0 commit comments