3
3
* Copyright © 2016 Magento. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \Initialization ;
7
8
8
- use \Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper ;
9
+ use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper ;
10
+ use Magento \Catalog \Controller \Adminhtml \Product \Initialization \StockDataFilter ;
11
+ use Magento \Catalog \Model \Product ;
12
+ use Magento \Catalog \Model \Product \Option ;
13
+ use Magento \Framework \App \RequestInterface ;
14
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
16
+ use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
9
17
18
+ /**
19
+ * Class HelperTest
20
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
+ * @SuppressWarnings(PHPMD.ExcessivePublicCount)
22
+ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
23
+ * @SuppressWarnings(PHPMD.TooManyFields)
24
+ */
10
25
class HelperTest extends \PHPUnit_Framework_TestCase
11
26
{
27
+ /**
28
+ * @var ObjectManager
29
+ */
30
+ protected $ objectManager ;
31
+
32
+ /**
33
+ * @var Helper
34
+ */
35
+ protected $ helper ;
36
+
12
37
/**
13
38
* @var \PHPUnit_Framework_MockObject_MockObject
14
39
*/
15
40
protected $ requestMock ;
16
41
17
42
/**
18
- * @var \PHPUnit_Framework_MockObject_MockObject
43
+ * @var StoreManagerInterface| \PHPUnit_Framework_MockObject_MockObject
19
44
*/
20
45
protected $ storeManagerMock ;
21
46
22
47
/**
23
- * @var \PHPUnit_Framework_MockObject_MockObject
48
+ * @var StockDataFilter| \PHPUnit_Framework_MockObject_MockObject
24
49
*/
25
50
protected $ stockFilterMock ;
26
51
27
52
/**
28
- * @var \PHPUnit_Framework_MockObject_MockObject
53
+ * @var ProductLinks| \PHPUnit_Framework_MockObject_MockObject
29
54
*/
30
55
protected $ productLinksMock ;
31
56
32
57
/**
33
- * @var \PHPUnit_Framework_MockObject_MockObject
58
+ * @var Product| \PHPUnit_Framework_MockObject_MockObject
34
59
*/
35
60
protected $ productMock ;
36
61
@@ -55,43 +80,45 @@ class HelperTest extends \PHPUnit_Framework_TestCase
55
80
protected $ websiteId = 1 ;
56
81
57
82
/**
58
- * @var \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper
59
- */
60
- protected $ helper ;
61
-
62
- /**
63
- * @var \PHPUnit_Framework_MockObject_MockObject
83
+ * @var \Magento\Backend\Helper\Js|\PHPUnit_Framework_MockObject_MockObject
64
84
*/
65
85
protected $ jsHelperMock ;
66
86
67
87
protected function setUp ()
68
88
{
69
- $ this ->requestMock = $ this ->getMock ('Magento\Framework\App\Request\Http ' , [], [], '' , false );
70
- $ this ->jsHelperMock = $ this ->getMock ('Magento\Backend\Helper\Js ' , [], [], '' , false );
71
- $ this ->storeMock = $ this ->getMock ('Magento\Store\Model\Store ' , [], [], '' , false );
72
- $ this ->websiteMock = $ this ->getMock ('Magento\Store\Model\Website ' , [], [], '' , false );
73
- $ this ->storeManagerMock = $ this ->getMock ('Magento\Store\Model\StoreManagerInterface ' );
74
- $ this ->dateFilterMock = $ this ->getMock ('\Magento\Framework\Stdlib\DateTime\Filter\Date ' , [], [], '' , false );
75
-
76
- $ this ->stockFilterMock = $ this ->getMock (
77
- 'Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter ' ,
89
+ $ this ->objectManager = new ObjectManager ($ this );
90
+ $ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
91
+ ->setMethods (['getPost ' ])
92
+ ->getMockForAbstractClass ();
93
+
94
+ $ this ->jsHelperMock = $ this ->getMock (\Magento \Backend \Helper \Js::class, [], [], '' , false );
95
+ $ this ->storeMock = $ this ->getMock (\Magento \Store \Model \Store::class, [], [], '' , false );
96
+ $ this ->websiteMock = $ this ->getMock (\Magento \Store \Model \Website::class, [], [], '' , false );
97
+ $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)
98
+ ->getMockForAbstractClass ();
99
+ $ this ->dateFilterMock = $ this ->getMock (
100
+ \Magento \Framework \Stdlib \DateTime \Filter \Date::class,
78
101
[],
79
102
[],
80
103
'' ,
81
104
false
82
105
);
106
+
107
+ $ this ->stockFilterMock = $ this ->getMockBuilder (StockDataFilter::class)
108
+ ->disableOriginalConstructor ()
109
+ ->getMock ();
110
+
83
111
$ this ->productLinksMock = $ this ->getMock (
84
- ' Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks ' ,
112
+ \ Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks::class ,
85
113
[],
86
114
[],
87
115
'' ,
88
116
false
89
117
);
90
118
91
119
$ this ->productMock = $ this ->getMock (
92
- ' Magento\Catalog\Model\Product ' ,
120
+ \ Magento \Catalog \Model \Product::class ,
93
121
[
94
- 'setData ' ,
95
122
'addData ' ,
96
123
'getId ' ,
97
124
'setWebsiteIds ' ,
@@ -100,7 +127,6 @@ protected function setUp()
100
127
'getAttributes ' ,
101
128
'unlockAttribute ' ,
102
129
'getOptionsReadOnly ' ,
103
- 'setProductOptions ' ,
104
130
'setCanSaveCustomOptions ' ,
105
131
'__sleep ' ,
106
132
'__wakeup '
@@ -109,6 +135,15 @@ protected function setUp()
109
135
'' ,
110
136
false
111
137
);
138
+
139
+ $ this ->helper = $ this ->objectManager ->getObject (Helper::class, [
140
+ 'request ' => $ this ->requestMock ,
141
+ 'storeManager ' => $ this ->storeManagerMock ,
142
+ 'stockFilter ' => $ this ->stockFilterMock ,
143
+ 'productLinks ' => $ this ->productLinksMock ,
144
+ 'jsHelper ' => $ this ->jsHelperMock ,
145
+ 'dateFilter ' => $ this ->dateFilterMock ,
146
+ ]);
112
147
}
113
148
114
149
/**
@@ -117,6 +152,20 @@ protected function setUp()
117
152
*/
118
153
public function testInitialize ()
119
154
{
155
+ $ optionsData = [
156
+ 'option1 ' => ['is_delete ' => false , 'name ' => 'name1 ' , 'price ' => 'price1 ' , 'option_id ' => '13 ' ],
157
+ 'option2 ' => ['is_delete ' => false , 'name ' => 'name1 ' , 'price ' => 'price1 ' , 'option_id ' => '14 ' ,
158
+ 'values ' => [
159
+ 'value1 ' => ['option_type_id ' => 1 , 'is_delete ' =>'' ],
160
+ 'value2 ' => ['option_type_id ' => '-1 ' , 'is_delete ' =>'1 ' ]
161
+ ]
162
+ ],
163
+ ];
164
+ $ productData = [
165
+ 'stock_data ' => ['stock_data ' ],
166
+ 'options ' => $ optionsData ,
167
+ ];
168
+
120
169
$ this ->websiteMock ->expects ($ this ->once ())
121
170
->method ('getId ' )
122
171
->will ($ this ->returnValue ($ this ->websiteId ));
@@ -130,28 +179,20 @@ public function testInitialize()
130
179
->with (true )
131
180
->will ($ this ->returnValue ($ this ->storeMock ));
132
181
133
- $ this ->jsHelperMock = $ this ->getMock ('\Magento\Backend\Helper\Js ' , [], [], '' , false );
134
- $ this ->helper = new Helper (
135
- $ this ->requestMock ,
136
- $ this ->storeManagerMock ,
137
- $ this ->stockFilterMock ,
138
- $ this ->productLinksMock ,
139
- $ this ->jsHelperMock ,
140
- $ this ->dateFilterMock
141
- );
142
-
143
- $ productData = [
144
- 'stock_data ' => ['stock_data ' ],
145
- 'options ' => ['option1 ' , 'option2 ' ]
146
- ];
147
-
148
- $ attributeNonDate = $ this ->getMock ('Magento\Catalog\Model\ResourceModel\Eav\Attribute ' , [], [], '' , false );
149
- $ attributeDate = $ this ->getMock ('Magento\Catalog\Model\ResourceModel\Eav\Attribute ' , [], [], '' , false );
182
+ $ attributeNonDate = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class)
183
+ ->disableOriginalConstructor ()
184
+ ->getMock ();
185
+ $ attributeDate = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Eav \Attribute::class)
186
+ ->disableOriginalConstructor ()
187
+ ->getMock ();
150
188
151
189
$ attributeNonDateBackEnd =
152
- $ this ->getMock ('Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend ' , [], [], '' , false );
153
- $ attributeDateBackEnd =
154
- $ this ->getMock ('Magento\Eav\Model\Entity\Attribute\Backend\Datetime ' , [], [], '' , false );
190
+ $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \Backend \DefaultBackend::class)
191
+ ->disableOriginalConstructor ()
192
+ ->getMock ();
193
+ $ attributeDateBackEnd = $ this ->getMockBuilder (\Magento \Eav \Model \Entity \Attribute \Backend \Datetime::class)
194
+ ->disableOriginalConstructor ()
195
+ ->getMock ();
155
196
156
197
$ attributeNonDate ->expects ($ this ->any ())
157
198
->method ('getBackend ' )
@@ -177,6 +218,13 @@ public function testInitialize()
177
218
178
219
$ useDefaults = ['attributeCode1 ' , 'attributeCode2 ' ];
179
220
221
+ $ this ->requestMock ->expects ($ this ->any ())->method ('getPost ' )->willReturnMap (
222
+ [
223
+ ['product ' , [], $ productData ],
224
+ ['use_default ' , null , $ useDefaults ]
225
+ ]
226
+ );
227
+
180
228
$ this ->requestMock ->expects ($ this ->at (0 ))
181
229
->method ('getPost ' )
182
230
->with ('product ' )
@@ -230,6 +278,7 @@ public function testInitialize()
230
278
231
279
$ productData ['category_ids ' ] = [];
232
280
$ productData ['website_ids ' ] = [];
281
+
233
282
$ this ->productMock ->expects ($ this ->once ())
234
283
->method ('addData ' )
235
284
->with ($ productData );
@@ -242,15 +291,11 @@ public function testInitialize()
242
291
->method ('getOptionsReadOnly ' )
243
292
->will ($ this ->returnValue (false ));
244
293
245
- $ this ->productMock ->expects ($ this ->once ())
246
- ->method ('setProductOptions ' )
247
- ->with ($ productData ['options ' ]);
248
-
249
- $ this ->productMock ->expects ($ this ->once ())
250
- ->method ('setCanSaveCustomOptions ' )
251
- ->with (true );
252
-
253
294
$ this ->assertEquals ($ this ->productMock , $ this ->helper ->initialize ($ this ->productMock ));
295
+
296
+ $ productOptions = $ this ->productMock ->getProductOptions ();
297
+ $ this ->assertTrue (2 == count ($ productOptions ));
298
+ $ this ->assertTrue (1 == count ($ productOptions ['option2 ' ]['values ' ]));
254
299
}
255
300
256
301
/**
@@ -292,15 +337,6 @@ public function mergeProductOptionsDataProvider()
292
337
*/
293
338
public function testMergeProductOptions ($ productOptions , $ defaultOptions , $ expectedResults )
294
339
{
295
- $ this ->jsHelperMock = $ this ->getMock ('\Magento\Backend\Helper\Js ' , [], [], '' , false );
296
- $ this ->helper = new Helper (
297
- $ this ->requestMock ,
298
- $ this ->storeManagerMock ,
299
- $ this ->stockFilterMock ,
300
- $ this ->productLinksMock ,
301
- $ this ->jsHelperMock ,
302
- $ this ->dateFilterMock
303
- );
304
340
$ result = $ this ->helper ->mergeProductOptions ($ productOptions , $ defaultOptions );
305
341
$ this ->assertEquals ($ expectedResults , $ result );
306
342
}
0 commit comments