14
14
use Magento \Framework \Registry ;
15
15
use Magento \Cms \Model \Wysiwyg \Config as WysiwygConfig ;
16
16
use Magento \Framework \App \Request \Http ;
17
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
18
+ use Magento \Framework \Exception \NoSuchEntityException ;
19
+ use Magento \Catalog \Model \Product \Type as ProductTypes ;
17
20
18
21
/**
19
22
* Class BuilderTest
@@ -67,6 +70,11 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
67
70
*/
68
71
protected $ storeFactoryMock ;
69
72
73
+ /**
74
+ * @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
75
+ */
76
+ protected $ productRepositoryMock ;
77
+
70
78
/**
71
79
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
72
80
*/
@@ -90,9 +98,10 @@ protected function setUp()
90
98
->setMethods (['load ' ])
91
99
->getMockForAbstractClass ();
92
100
93
- $ this ->storeFactoryMock ->expects ($ this ->any ())
94
- ->method ('create ' )
95
- ->willReturn ($ this ->storeMock );
101
+ $ this ->productRepositoryMock = $ this ->getMockBuilder (ProductRepositoryInterface::class)
102
+ ->setMethods (['getById ' ])
103
+ ->disableOriginalConstructor ()
104
+ ->getMockForAbstractClass ();
96
105
97
106
$ this ->builder = $ this ->objectManager ->getObject (
98
107
Builder::class,
@@ -102,140 +111,198 @@ protected function setUp()
102
111
'registry ' => $ this ->registryMock ,
103
112
'wysiwygConfig ' => $ this ->wysiwygConfigMock ,
104
113
'storeFactory ' => $ this ->storeFactoryMock ,
114
+ 'productRepository ' => $ this ->productRepositoryMock
105
115
]
106
116
);
107
117
}
108
118
109
119
public function testBuildWhenProductExistAndPossibleToLoadProduct ()
110
120
{
121
+ $ productId = 2 ;
122
+ $ productType = 'type_id ' ;
123
+ $ productStore = 'store ' ;
124
+ $ productSet = 3 ;
125
+
111
126
$ valueMap = [
112
- ['id ' , null , 2 ],
113
- ['store ' , 0 , 'some_store ' ],
114
- ['type ' , null , 'type_id ' ],
115
- ['set ' , null , 3 ],
116
- ['store ' , null , 'store ' ],
127
+ ['id ' , null , $ productId ],
128
+ ['type ' , null , $ productType ],
129
+ ['set ' , null , $ productSet ],
130
+ ['store ' , 0 , $ productStore ],
117
131
];
132
+
118
133
$ this ->requestMock ->expects ($ this ->any ())
119
134
->method ('getParam ' )
120
135
->willReturnMap ($ valueMap );
121
- $ this ->productFactoryMock ->expects ($ this ->once ())
136
+
137
+ $ this ->productRepositoryMock ->expects ($ this ->any ())
138
+ ->method ('getById ' )
139
+ ->with ($ productId , true , $ productStore )
140
+ ->willReturn ($ this ->productMock );
141
+
142
+ $ this ->storeFactoryMock ->expects ($ this ->any ())
122
143
->method ('create ' )
123
- ->will ($ this ->returnValue ($ this ->productMock ));
124
- $ this ->productMock ->expects ($ this ->once ())
125
- ->method ('setStoreId ' )
126
- ->with ('some_store ' )
127
- ->willReturnSelf ();
128
- $ this ->productMock ->expects ($ this ->never ())
129
- ->method ('setTypeId ' );
130
- $ this ->productMock ->expects ($ this ->once ())
144
+ ->willReturn ($ this ->storeMock );
145
+
146
+ $ this ->storeMock ->expects ($ this ->any ())
131
147
->method ('load ' )
132
- ->with (2 )
133
- ->will ($ this ->returnSelf ());
134
- $ this ->productMock ->expects ($ this ->once ())
135
- ->method ('setAttributeSetId ' )
136
- ->with (3 )
137
- ->will ($ this ->returnSelf ());
148
+ ->with ($ productStore )
149
+ ->willReturnSelf ();
150
+
138
151
$ registryValueMap = [
139
152
['product ' , $ this ->productMock , $ this ->registryMock ],
140
153
['current_product ' , $ this ->productMock , $ this ->registryMock ],
154
+ ['current_store ' , $ this ->registryMock , $ this ->storeMock ],
141
155
];
156
+
142
157
$ this ->registryMock ->expects ($ this ->any ())
143
158
->method ('register ' )
144
159
->willReturn ($ registryValueMap );
160
+
145
161
$ this ->wysiwygConfigMock ->expects ($ this ->once ())
146
162
->method ('setStoreId ' )
147
- ->with ('store ' );
163
+ ->with ($ productStore );
164
+
148
165
$ this ->assertEquals ($ this ->productMock , $ this ->builder ->build ($ this ->requestMock ));
149
166
}
150
167
151
168
public function testBuildWhenImpossibleLoadProduct ()
152
169
{
170
+ $ productId = 2 ;
171
+ $ productType = 'type_id ' ;
172
+ $ productStore = 'store ' ;
173
+ $ productSet = 3 ;
174
+
153
175
$ valueMap = [
154
- ['id ' , null , 15 ],
155
- ['store ' , 0 , 'some_store ' ],
156
- ['type ' , null , 'type_id ' ],
157
- ['set ' , null , 3 ],
158
- ['store ' , null , 'store ' ],
176
+ ['id ' , null , $ productId ],
177
+ ['type ' , null , $ productType ],
178
+ ['set ' , null , $ productSet ],
179
+ ['store ' , 0 , $ productStore ],
159
180
];
181
+
160
182
$ this ->requestMock ->expects ($ this ->any ())
161
183
->method ('getParam ' )
162
- ->will ($ this ->returnValueMap ($ valueMap ));
184
+ ->willReturnMap ($ valueMap );
185
+
186
+ $ this ->productRepositoryMock ->expects ($ this ->any ())
187
+ ->method ('getById ' )
188
+ ->with ($ productId , true , $ productStore )
189
+ ->willThrowException (new NoSuchEntityException ());
190
+
163
191
$ this ->productFactoryMock ->expects ($ this ->once ())
164
192
->method ('create ' )
165
- ->willReturn ($ this ->productMock );
166
- $ this ->productMock ->expects ($ this ->once ())
167
- ->method ('setStoreId ' )
168
- ->with ('some_store ' )
169
- ->willReturnSelf ();
170
- $ this ->productMock ->expects ($ this ->once ())
193
+ ->will ($ this ->returnValue ($ this ->productMock ));
194
+
195
+ $ this ->productMock ->expects ($ this ->any ())
196
+ ->method ('setData ' )
197
+ ->with ('_edit_mode ' , true );
198
+
199
+ $ this ->productMock ->expects ($ this ->any ())
171
200
->method ('setTypeId ' )
172
- ->with (\Magento \Catalog \Model \Product \Type::DEFAULT_TYPE )
173
- ->willReturnSelf ();
174
- $ this ->productMock ->expects ($ this ->once ())
175
- ->method ('load ' )
176
- ->with (15 )
177
- ->willThrowException (new \Exception ());
201
+ ->with (ProductTypes::DEFAULT_TYPE );
202
+
203
+ $ this ->productMock ->expects ($ this ->any ())
204
+ ->method ('setStoreId ' )
205
+ ->with ($ productStore );
206
+
207
+ $ this ->productMock ->expects ($ this ->any ())
208
+ ->method ('setAttributeSetId ' )
209
+ ->with ($ productSet );
210
+
178
211
$ this ->loggerMock ->expects ($ this ->once ())
179
212
->method ('critical ' );
180
- $ this ->productMock ->expects ($ this ->once ())
181
- ->method ('setAttributeSetId ' )
182
- ->with (3 )
183
- ->will ($ this ->returnSelf ());
213
+
214
+ $ this ->storeFactoryMock ->expects ($ this ->any ())
215
+ ->method ('create ' )
216
+ ->willReturn ($ this ->storeMock );
217
+
218
+ $ this ->storeMock ->expects ($ this ->any ())
219
+ ->method ('load ' )
220
+ ->with ($ productStore )
221
+ ->willReturnSelf ();
222
+
184
223
$ registryValueMap = [
185
224
['product ' , $ this ->productMock , $ this ->registryMock ],
186
225
['current_product ' , $ this ->productMock , $ this ->registryMock ],
226
+ ['current_store ' , $ this ->registryMock , $ this ->storeMock ],
187
227
];
228
+
188
229
$ this ->registryMock ->expects ($ this ->any ())
189
230
->method ('register ' )
190
- ->will ($ this ->returnValueMap ($ registryValueMap ));
231
+ ->willReturn ($ registryValueMap );
232
+
191
233
$ this ->wysiwygConfigMock ->expects ($ this ->once ())
192
234
->method ('setStoreId ' )
193
- ->with ('store ' );
235
+ ->with ($ productStore );
236
+
194
237
$ this ->assertEquals ($ this ->productMock , $ this ->builder ->build ($ this ->requestMock ));
195
238
}
196
239
197
240
public function testBuildWhenProductNotExist ()
198
241
{
242
+ $ productId = 0 ;
243
+ $ productType = 'type_id ' ;
244
+ $ productStore = 'store ' ;
245
+ $ productSet = 3 ;
246
+
199
247
$ valueMap = [
200
- ['id ' , null , null ],
201
- ['store ' , 0 , 'some_store ' ],
202
- ['type ' , null , 'type_id ' ],
203
- ['set ' , null , 3 ],
204
- ['store ' , null , 'store ' ],
248
+ ['id ' , null , $ productId ],
249
+ ['type ' , null , $ productType ],
250
+ ['set ' , null , $ productSet ],
251
+ ['store ' , 0 , $ productStore ],
205
252
];
253
+
206
254
$ this ->requestMock ->expects ($ this ->any ())
207
255
->method ('getParam ' )
208
- ->will ($ this ->returnValueMap ($ valueMap ));
256
+ ->willReturnMap ($ valueMap );
257
+
258
+ $ this ->productRepositoryMock ->expects ($ this ->any ())
259
+ ->method ('getById ' )
260
+ ->with ($ productId , true , $ productStore )
261
+ ->willThrowException (new NoSuchEntityException ());
262
+
209
263
$ this ->productFactoryMock ->expects ($ this ->once ())
210
264
->method ('create ' )
211
- ->willReturn ($ this ->productMock );
212
- $ this ->productMock ->expects ($ this ->once ())
213
- ->method ('setStoreId ' )
214
- ->with ('some_store ' )
215
- ->willReturnSelf ();
216
- $ productValueMap = [
217
- ['type_id ' , $ this ->productMock ],
218
- [\Magento \Catalog \Model \Product \Type::DEFAULT_TYPE , $ this ->productMock ],
219
- ];
265
+ ->will ($ this ->returnValue ($ this ->productMock ));
266
+
267
+ $ this ->productMock ->expects ($ this ->any ())
268
+ ->method ('setData ' )
269
+ ->with ('_edit_mode ' , true );
270
+
220
271
$ this ->productMock ->expects ($ this ->any ())
221
272
->method ('setTypeId ' )
222
- ->willReturnMap ($ productValueMap );
223
- $ this ->productMock ->expects ($ this ->never ())
224
- ->method ('load ' );
225
- $ this ->productMock ->expects ($ this ->once ())
273
+ ->with ($ productType );
274
+
275
+ $ this ->productMock ->expects ($ this ->any ())
276
+ ->method ('setStoreId ' )
277
+ ->with ($ productStore );
278
+
279
+ $ this ->productMock ->expects ($ this ->any ())
226
280
->method ('setAttributeSetId ' )
227
- ->with (3 )
228
- ->will ($ this ->returnSelf ());
281
+ ->with ($ productSet );
282
+
283
+ $ this ->storeFactoryMock ->expects ($ this ->any ())
284
+ ->method ('create ' )
285
+ ->willReturn ($ this ->storeMock );
286
+
287
+ $ this ->storeMock ->expects ($ this ->any ())
288
+ ->method ('load ' )
289
+ ->with ($ productStore )
290
+ ->willReturnSelf ();
291
+
229
292
$ registryValueMap = [
230
293
['product ' , $ this ->productMock , $ this ->registryMock ],
231
294
['current_product ' , $ this ->productMock , $ this ->registryMock ],
295
+ ['current_store ' , $ this ->registryMock , $ this ->storeMock ],
232
296
];
297
+
233
298
$ this ->registryMock ->expects ($ this ->any ())
234
299
->method ('register ' )
235
- ->will ($ this ->returnValueMap ($ registryValueMap ));
300
+ ->willReturn ($ registryValueMap );
301
+
236
302
$ this ->wysiwygConfigMock ->expects ($ this ->once ())
237
303
->method ('setStoreId ' )
238
- ->with ('store ' );
304
+ ->with ($ productStore );
305
+
239
306
$ this ->assertEquals ($ this ->productMock , $ this ->builder ->build ($ this ->requestMock ));
240
307
}
241
308
}
0 commit comments