9
9
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
- use Magento \Catalog \Model \ProductFactory ;
13
12
use Magento \ConfigurableProduct \Api \Data \OptionInterface ;
14
13
use Magento \ConfigurableProduct \Model \Plugin \ProductRepositorySave ;
15
14
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
16
15
use Magento \ConfigurableProduct \Test \Unit \Model \Product \ProductExtensionAttributes ;
17
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
18
- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
17
+ use PHPUnit \Framework \MockObject \MockObject ;
18
+ use PHPUnit \Framework \TestCase ;
19
19
20
20
/**
21
- * Class ProductRepositorySaveTest
21
+ * Test for ProductRepositorySave plugin
22
+ *
22
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
24
*/
24
- class ProductRepositorySaveTest extends \ PHPUnit \ Framework \ TestCase
25
+ class ProductRepositorySaveTest extends TestCase
25
26
{
26
27
/**
27
28
* @var ProductAttributeRepositoryInterface|MockObject
28
29
*/
29
30
private $ productAttributeRepository ;
30
31
31
- /**
32
- * @var ProductFactory|MockObject
33
- */
34
- private $ productFactory ;
35
-
36
32
/**
37
33
* @var Product|MockObject
38
34
*/
@@ -68,15 +64,13 @@ class ProductRepositorySaveTest extends \PHPUnit\Framework\TestCase
68
64
*/
69
65
private $ plugin ;
70
66
67
+ /**
68
+ * @inheritdoc
69
+ */
71
70
protected function setUp ()
72
71
{
73
72
$ this ->productAttributeRepository = $ this ->getMockForAbstractClass (ProductAttributeRepositoryInterface::class);
74
73
75
- $ this ->productFactory = $ this ->getMockBuilder (ProductFactory::class)
76
- ->disableOriginalConstructor ()
77
- ->setMethods (['create ' ])
78
- ->getMock ();
79
-
80
74
$ this ->product = $ this ->getMockBuilder (Product::class)
81
75
->disableOriginalConstructor ()
82
76
->setMethods (['getTypeId ' , 'getExtensionAttributes ' ])
@@ -102,12 +96,15 @@ protected function setUp()
102
96
ProductRepositorySave::class,
103
97
[
104
98
'productAttributeRepository ' => $ this ->productAttributeRepository ,
105
- 'productFactory ' => $ this ->productFactory
99
+ 'productRepository ' => $ this ->productRepository
106
100
]
107
101
);
108
102
}
109
103
110
- public function testAfterSaveWhenProductIsSimple ()
104
+ /**
105
+ * Validating the result after saving a configurable product
106
+ */
107
+ public function testBeforeSaveWhenProductIsSimple ()
111
108
{
112
109
$ this ->product ->expects (static ::once ())
113
110
->method ('getTypeId ' )
@@ -116,18 +113,21 @@ public function testAfterSaveWhenProductIsSimple()
116
113
->method ('getExtensionAttributes ' );
117
114
118
115
$ this ->assertEquals (
119
- $ this ->result ,
120
- $ this ->plugin ->afterSave ($ this ->productRepository , $ this ->result , $ this -> product )
116
+ $ this ->product ,
117
+ $ this ->plugin ->beforeSave ($ this ->productRepository , $ this ->product )[ 0 ]
121
118
);
122
119
}
123
120
124
- public function testAfterSaveWithoutOptions ()
121
+ /**
122
+ * Test saving a configurable product without attribute options
123
+ */
124
+ public function testBeforeSaveWithoutOptions ()
125
125
{
126
126
$ this ->product ->expects (static ::once ())
127
127
->method ('getTypeId ' )
128
128
->willReturn (Configurable::TYPE_CODE );
129
129
130
- $ this ->result ->expects (static ::once ())
130
+ $ this ->product ->expects (static ::once ())
131
131
->method ('getExtensionAttributes ' )
132
132
->willReturn ($ this ->extensionAttributes );
133
133
@@ -142,23 +142,25 @@ public function testAfterSaveWithoutOptions()
142
142
->method ('get ' );
143
143
144
144
$ this ->assertEquals (
145
- $ this ->result ,
146
- $ this ->plugin ->afterSave ($ this ->productRepository , $ this ->result , $ this -> product )
145
+ $ this ->product ,
146
+ $ this ->plugin ->beforeSave ($ this ->productRepository , $ this ->product )[ 0 ]
147
147
);
148
148
}
149
149
150
150
/**
151
+ * Test saving a configurable product with same set of attribute values
152
+ *
151
153
* @expectedException \Magento\Framework\Exception\InputException
152
154
* @expectedExceptionMessage Products "5" and "4" have the same set of attribute values.
153
155
*/
154
- public function testAfterSaveWithLinks ()
156
+ public function testBeforeSaveWithLinks ()
155
157
{
156
158
$ links = [4 , 5 ];
157
159
$ this ->product ->expects (static ::once ())
158
160
->method ('getTypeId ' )
159
161
->willReturn (Configurable::TYPE_CODE );
160
162
161
- $ this ->result ->expects (static ::once ())
163
+ $ this ->product ->expects (static ::once ())
162
164
->method ('getExtensionAttributes ' )
163
165
->willReturn ($ this ->extensionAttributes );
164
166
$ this ->extensionAttributes ->expects (static ::once ())
@@ -173,27 +175,26 @@ public function testAfterSaveWithLinks()
173
175
174
176
$ product = $ this ->getMockBuilder (Product::class)
175
177
->disableOriginalConstructor ()
176
- ->setMethods (['load ' , ' getData ' , '__wakeup ' ])
178
+ ->setMethods (['getData ' , '__wakeup ' ])
177
179
->getMock ();
178
180
179
- $ this ->productFactory ->expects (static ::exactly (2 ))
180
- ->method ('create ' )
181
+ $ this ->productRepository ->expects (static ::exactly (2 ))
182
+ ->method ('getById ' )
181
183
->willReturn ($ product );
182
184
183
- $ product ->expects (static ::exactly (2 ))
184
- ->method ('load ' )
185
- ->willReturnSelf ();
186
185
$ product ->expects (static ::never ())
187
186
->method ('getData ' );
188
187
189
- $ this ->plugin ->afterSave ($ this ->productRepository , $ this -> result , $ this ->product );
188
+ $ this ->plugin ->beforeSave ($ this ->productRepository , $ this ->product );
190
189
}
191
190
192
191
/**
192
+ * Test saving a configurable product with missing attribute
193
+ *
193
194
* @expectedException \Magento\Framework\Exception\InputException
194
195
* @expectedExceptionMessage Product with id "4" does not contain required attribute "color".
195
196
*/
196
- public function testAfterSaveWithLinksWithMissingAttribute ()
197
+ public function testBeforeSaveWithLinksWithMissingAttribute ()
197
198
{
198
199
$ simpleProductId = 4 ;
199
200
$ links = [$ simpleProductId , 5 ];
@@ -208,7 +209,7 @@ public function testAfterSaveWithLinksWithMissingAttribute()
208
209
->method ('getTypeId ' )
209
210
->willReturn (Configurable::TYPE_CODE );
210
211
211
- $ this ->result ->expects (static ::once ())
212
+ $ this ->product ->expects (static ::once ())
212
213
->method ('getExtensionAttributes ' )
213
214
->willReturn ($ this ->extensionAttributes );
214
215
$ this ->extensionAttributes ->expects (static ::once ())
@@ -228,29 +229,28 @@ public function testAfterSaveWithLinksWithMissingAttribute()
228
229
229
230
$ product = $ this ->getMockBuilder (Product::class)
230
231
->disableOriginalConstructor ()
231
- ->setMethods (['load ' , ' getData ' , '__wakeup ' ])
232
+ ->setMethods (['getData ' , '__wakeup ' ])
232
233
->getMock ();
233
234
234
- $ this ->productFactory ->expects (static ::once ())
235
- ->method ('create ' )
235
+ $ this ->productRepository ->expects (static ::once ())
236
+ ->method ('getById ' )
236
237
->willReturn ($ product );
237
- $ product ->expects (static ::once ())
238
- ->method ('load ' )
239
- ->with ($ simpleProductId )
240
- ->willReturnSelf ();
238
+
241
239
$ product ->expects (static ::once ())
242
240
->method ('getData ' )
243
241
->with ($ attributeCode )
244
242
->willReturn (false );
245
243
246
- $ this ->plugin ->afterSave ($ this ->productRepository , $ this -> result , $ this ->product );
244
+ $ this ->plugin ->beforeSave ($ this ->productRepository , $ this ->product );
247
245
}
248
246
249
247
/**
248
+ * Test saving a configurable product with duplicate attributes
249
+ *
250
250
* @expectedException \Magento\Framework\Exception\InputException
251
251
* @expectedExceptionMessage Products "5" and "4" have the same set of attribute values.
252
252
*/
253
- public function testAfterSaveWithLinksWithDuplicateAttributes ()
253
+ public function testBeforeSaveWithLinksWithDuplicateAttributes ()
254
254
{
255
255
$ links = [4 , 5 ];
256
256
$ attributeCode = 'color ' ;
@@ -264,7 +264,7 @@ public function testAfterSaveWithLinksWithDuplicateAttributes()
264
264
->method ('getTypeId ' )
265
265
->willReturn (Configurable::TYPE_CODE );
266
266
267
- $ this ->result ->expects (static ::once ())
267
+ $ this ->product ->expects (static ::once ())
268
268
->method ('getExtensionAttributes ' )
269
269
->willReturn ($ this ->extensionAttributes );
270
270
$ this ->extensionAttributes ->expects (static ::once ())
@@ -284,20 +284,18 @@ public function testAfterSaveWithLinksWithDuplicateAttributes()
284
284
285
285
$ product = $ this ->getMockBuilder (Product::class)
286
286
->disableOriginalConstructor ()
287
- ->setMethods (['load ' , ' getData ' , '__wakeup ' ])
287
+ ->setMethods (['getData ' , '__wakeup ' ])
288
288
->getMock ();
289
289
290
- $ this ->productFactory ->expects (static ::exactly (2 ))
291
- ->method ('create ' )
290
+ $ this ->productRepository ->expects (static ::exactly (2 ))
291
+ ->method ('getById ' )
292
292
->willReturn ($ product );
293
- $ product ->expects (static ::exactly (2 ))
294
- ->method ('load ' )
295
- ->willReturnSelf ();
293
+
296
294
$ product ->expects (static ::exactly (4 ))
297
295
->method ('getData ' )
298
296
->with ($ attributeCode )
299
297
->willReturn ($ attributeId );
300
298
301
- $ this ->plugin ->afterSave ($ this ->productRepository , $ this -> result , $ this ->product );
299
+ $ this ->plugin ->beforeSave ($ this ->productRepository , $ this ->product );
302
300
}
303
301
}
0 commit comments