33 * Copyright © 2016 Magento. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Catalog \Test \Unit \Controller \Adminhtml \Product \Initialization ;
78
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 ;
917
18+ /**
19+ * Class HelperTest
20+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+ * @SuppressWarnings(PHPMD.ExcessivePublicCount)
22+ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
23+ * @SuppressWarnings(PHPMD.TooManyFields)
24+ */
1025class HelperTest extends \PHPUnit_Framework_TestCase
1126{
27+ /**
28+ * @var ObjectManager
29+ */
30+ protected $ objectManager ;
31+
32+ /**
33+ * @var Helper
34+ */
35+ protected $ helper ;
36+
1237 /**
1338 * @var \PHPUnit_Framework_MockObject_MockObject
1439 */
1540 protected $ requestMock ;
1641
1742 /**
18- * @var \PHPUnit_Framework_MockObject_MockObject
43+ * @var StoreManagerInterface| \PHPUnit_Framework_MockObject_MockObject
1944 */
2045 protected $ storeManagerMock ;
2146
2247 /**
23- * @var \PHPUnit_Framework_MockObject_MockObject
48+ * @var StockDataFilter| \PHPUnit_Framework_MockObject_MockObject
2449 */
2550 protected $ stockFilterMock ;
2651
2752 /**
28- * @var \PHPUnit_Framework_MockObject_MockObject
53+ * @var ProductLinks| \PHPUnit_Framework_MockObject_MockObject
2954 */
3055 protected $ productLinksMock ;
3156
3257 /**
33- * @var \PHPUnit_Framework_MockObject_MockObject
58+ * @var Product| \PHPUnit_Framework_MockObject_MockObject
3459 */
3560 protected $ productMock ;
3661
@@ -55,43 +80,45 @@ class HelperTest extends \PHPUnit_Framework_TestCase
5580 protected $ websiteId = 1 ;
5681
5782 /**
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
6484 */
6585 protected $ jsHelperMock ;
6686
6787 protected function setUp ()
6888 {
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,
78101 [],
79102 [],
80103 '' ,
81104 false
82105 );
106+
107+ $ this ->stockFilterMock = $ this ->getMockBuilder (StockDataFilter::class)
108+ ->disableOriginalConstructor ()
109+ ->getMock ();
110+
83111 $ this ->productLinksMock = $ this ->getMock (
84- ' Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks ' ,
112+ \ Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks::class ,
85113 [],
86114 [],
87115 '' ,
88116 false
89117 );
90118
91119 $ this ->productMock = $ this ->getMock (
92- ' Magento\Catalog\Model\Product ' ,
120+ \ Magento \Catalog \Model \Product::class ,
93121 [
94- 'setData ' ,
95122 'addData ' ,
96123 'getId ' ,
97124 'setWebsiteIds ' ,
@@ -100,7 +127,6 @@ protected function setUp()
100127 'getAttributes ' ,
101128 'unlockAttribute ' ,
102129 'getOptionsReadOnly ' ,
103- 'setProductOptions ' ,
104130 'setCanSaveCustomOptions ' ,
105131 '__sleep ' ,
106132 '__wakeup '
@@ -109,6 +135,15 @@ protected function setUp()
109135 '' ,
110136 false
111137 );
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+ ]);
112147 }
113148
114149 /**
@@ -117,6 +152,20 @@ protected function setUp()
117152 */
118153 public function testInitialize ()
119154 {
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+
120169 $ this ->websiteMock ->expects ($ this ->once ())
121170 ->method ('getId ' )
122171 ->will ($ this ->returnValue ($ this ->websiteId ));
@@ -130,28 +179,20 @@ public function testInitialize()
130179 ->with (true )
131180 ->will ($ this ->returnValue ($ this ->storeMock ));
132181
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 ();
150188
151189 $ 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 ();
155196
156197 $ attributeNonDate ->expects ($ this ->any ())
157198 ->method ('getBackend ' )
@@ -177,6 +218,13 @@ public function testInitialize()
177218
178219 $ useDefaults = ['attributeCode1 ' , 'attributeCode2 ' ];
179220
221+ $ this ->requestMock ->expects ($ this ->any ())->method ('getPost ' )->willReturnMap (
222+ [
223+ ['product ' , [], $ productData ],
224+ ['use_default ' , null , $ useDefaults ]
225+ ]
226+ );
227+
180228 $ this ->requestMock ->expects ($ this ->at (0 ))
181229 ->method ('getPost ' )
182230 ->with ('product ' )
@@ -230,6 +278,7 @@ public function testInitialize()
230278
231279 $ productData ['category_ids ' ] = [];
232280 $ productData ['website_ids ' ] = [];
281+
233282 $ this ->productMock ->expects ($ this ->once ())
234283 ->method ('addData ' )
235284 ->with ($ productData );
@@ -242,15 +291,11 @@ public function testInitialize()
242291 ->method ('getOptionsReadOnly ' )
243292 ->will ($ this ->returnValue (false ));
244293
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-
253294 $ 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 ' ]));
254299 }
255300
256301 /**
@@ -292,15 +337,6 @@ public function mergeProductOptionsDataProvider()
292337 */
293338 public function testMergeProductOptions ($ productOptions , $ defaultOptions , $ expectedResults )
294339 {
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- );
304340 $ result = $ this ->helper ->mergeProductOptions ($ productOptions , $ defaultOptions );
305341 $ this ->assertEquals ($ expectedResults , $ result );
306342 }
0 commit comments