@@ -38,6 +38,16 @@ class LabelsTest extends \PHPUnit_Framework_TestCase
38
38
*/
39
39
protected $ region ;
40
40
41
+ /**
42
+ * @var \PHPUnit_Framework_MockObject_MockObject
43
+ */
44
+ private $ carrierFactory ;
45
+
46
+ /**
47
+ * @var \PHPUnit_Framework_MockObject_MockObject
48
+ */
49
+ private $ user ;
50
+
41
51
protected function setUp ()
42
52
{
43
53
$ this ->request = $ this ->getMockBuilder (\Magento \Shipping \Model \Shipment \Request::class)
@@ -48,32 +58,32 @@ protected function setUp()
48
58
->setMethods (['create ' ])
49
59
->getMock ();
50
60
$ requestFactory ->expects (static ::any ())->method ('create ' )->willReturn ($ this ->request );
51
-
52
- $ carrier = $ this ->getMockBuilder (\Magento \Shipping \Model \Carrier \AbstractCarrier::class)
61
+ $ this ->carrierFactory = $ this ->getMockBuilder (\Magento \Shipping \Model \CarrierFactory::class)
53
62
->disableOriginalConstructor ()
63
+ ->setMethods (['create ' ])
54
64
->getMock ();
55
-
56
- $ carrierFactory = $ this ->getMockBuilder (\Magento \Shipping \Model \CarrierFactory ::class)
65
+ $ storeManager = $ this -> getStoreManager ();
66
+ $ this -> user = $ this ->getMockBuilder (\Magento \User \Model \User ::class)
57
67
->disableOriginalConstructor ()
58
- ->setMethods (['create ' ])
68
+ ->setMethods (['getFirstname ' , ' getLastname ' , ' getEmail ' , ' getName ' ])
59
69
->getMock ();
60
- $ carrierFactory ->expects (static ::any ())->method ('create ' )->willReturn ($ carrier );
61
70
62
- $ storeManager = $ this ->getStoreManager ();
63
- $ authSession = $ this ->getAuthSession ();
71
+ $ authSession = $ this ->getMockBuilder (\Magento \Backend \Model \Auth \Session::class)
72
+ ->disableOriginalConstructor ()
73
+ ->setMethods (['getUser ' ])
74
+ ->getMock ();
75
+ $ authSession ->expects (static ::any ())->method ('getUser ' )->willReturn ($ this ->user );
64
76
$ regionFactory = $ this ->getRegionFactory ();
65
-
66
77
$ this ->scopeConfig = $ this ->getMockBuilder (\Magento \Framework \App \Config::class)
67
78
->disableOriginalConstructor ()
68
79
->setMethods (['getValue ' ])
69
80
->getMock ();
70
-
71
- $ bjectManagerHelper = new ObjectManagerHelper ($ this );
72
- $ this ->labels = $ bjectManagerHelper ->getObject (
81
+ $ objectManagerHelper = new ObjectManagerHelper ($ this );
82
+ $ this ->labels = $ objectManagerHelper ->getObject (
73
83
\Magento \Shipping \Model \Shipping \Labels::class,
74
84
[
75
85
'shipmentRequestFactory ' => $ requestFactory ,
76
- 'carrierFactory ' => $ carrierFactory ,
86
+ 'carrierFactory ' => $ this -> carrierFactory ,
77
87
'storeManager ' => $ storeManager ,
78
88
'scopeConfig ' => $ this ->scopeConfig ,
79
89
'authSession ' => $ authSession ,
@@ -83,14 +93,21 @@ protected function setUp()
83
93
}
84
94
85
95
/**
86
- * @covers \Magento\Shipping\Model\Shipping\Labels
96
+ * @dataProvider requestToShipmentDataProvider
87
97
*/
88
- public function testRequestToShipment ()
98
+ public function testRequestToShipment ($ regionId )
89
99
{
100
+ $ carrier = $ this ->getMockBuilder (\Magento \Shipping \Model \Carrier \AbstractCarrier::class)
101
+ ->disableOriginalConstructor ()
102
+ ->getMock ();
103
+ $ this ->carrierFactory ->expects (static ::any ())->method ('create ' )->willReturn ($ carrier );
90
104
$ order = $ this ->getMockBuilder (\Magento \Sales \Model \Order::class)
91
105
->disableOriginalConstructor ()
92
106
->getMock ();
93
-
107
+ $ this ->user ->expects ($ this ->exactly (2 ))->method ('getFirstname ' )->willReturn ('John ' );
108
+ $ this ->user ->expects ($ this ->exactly (2 ))->method ('getLastname ' )->willReturn ('Doe ' );
109
+ $ this ->user ->expects ($ this ->once ())->method ('getName ' )->willReturn ('John Doe ' );
110
+ $ this ->user ->expects ($ this ->once ())->method ('getEmail ' )->willReturn ('admin@admin.test.com ' );
94
111
$ shippingMethod = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
95
112
->disableOriginalConstructor ()
96
113
->setMethods (['getCarrierCode ' ])
@@ -124,7 +141,7 @@ public function testRequestToShipment()
124
141
$ this ->scopeConfig ->expects (static ::any ())
125
142
->method ('getValue ' )
126
143
->willReturnMap ([
127
- [Shipment::XML_PATH_STORE_REGION_ID , ScopeInterface::SCOPE_STORE , $ storeId , ' CA ' ],
144
+ [Shipment::XML_PATH_STORE_REGION_ID , ScopeInterface::SCOPE_STORE , $ storeId , $ regionId ],
128
145
[Shipment::XML_PATH_STORE_ADDRESS1 , ScopeInterface::SCOPE_STORE , $ storeId , 'Beverly Heals ' ],
129
146
['general/store_information ' , ScopeInterface::SCOPE_STORE , $ storeId , [
130
147
'name ' => 'General Store ' , 'phone ' => '(244)1500301 '
@@ -134,38 +151,35 @@ public function testRequestToShipment()
134
151
[Shipment::XML_PATH_STORE_COUNTRY_ID , ScopeInterface::SCOPE_STORE , $ storeId , 'US ' ],
135
152
[Shipment::XML_PATH_STORE_ADDRESS2 , ScopeInterface::SCOPE_STORE , $ storeId , '1st Park Avenue ' ],
136
153
]);
137
-
138
154
$ this ->labels ->requestToShipment ($ shipment );
139
155
}
140
156
141
157
/**
142
- * @return \PHPUnit_Framework_MockObject_MockObject
158
+ * @expectedException \Magento\Framework\Exception\LocalizedException
159
+ * @dataProvider testRequestToShipmentLocalizedExceptionDataProvider
143
160
*/
144
- protected function getAuthSession ( )
161
+ public function testRequestToShipmentLocalizedException ( $ isShipmentCarrierNotNull )
145
162
{
146
- $ user = $ this ->getMockBuilder (\Magento \User \Model \User ::class)
163
+ $ order = $ this ->getMockBuilder (\Magento \Sales \Model \Order ::class)
147
164
->disableOriginalConstructor ()
148
- ->setMethods (['getFirstname ' , 'getLastname ' , 'getEmail ' , 'getName ' ])
149
165
->getMock ();
150
- $ user ->expects (static ::exactly (2 ))
151
- ->method ('getFirstname ' )
152
- ->willReturn ('John ' );
153
- $ user ->expects (static ::exactly (2 ))
154
- ->method ('getLastname ' )
155
- ->willReturn ('Doe ' );
156
- $ user ->expects (static ::once ())
157
- ->method ('getName ' )
158
- ->willReturn ('John Doe ' );
159
- $ user ->expects (static ::once ())
160
- ->method ('getEmail ' )
161
- ->willReturn ('admin@admin.test.com ' );
162
-
163
- $ authSession = $ this ->getMockBuilder (\Magento \Backend \Model \Auth \Session::class)
166
+ $ shipment = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Shipment::class)
164
167
->disableOriginalConstructor ()
165
- ->setMethods (['getUser ' ])
166
168
->getMock ();
167
- $ authSession ->expects (static ::any ())->method ('getUser ' )->willReturn ($ user );
168
- return $ authSession ;
169
+ $ shippingMethod = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
170
+ ->disableOriginalConstructor ()
171
+ ->setMethods (['getCarrierCode ' ])
172
+ ->getMock ();
173
+ $ order ->expects ($ this ->exactly (2 ))
174
+ ->method ('getShippingMethod ' )
175
+ ->with (true )
176
+ ->willReturn ($ shippingMethod );
177
+ $ this ->carrierFactory
178
+ ->expects (static ::any ())
179
+ ->method ('create ' )
180
+ ->willReturn ($ isShipmentCarrierNotNull ? $ shippingMethod : null );
181
+ $ shipment ->expects ($ this ->once ())->method ('getOrder ' )->willReturn ($ order );
182
+ $ this ->labels ->requestToShipment ($ shipment );
169
183
}
170
184
171
185
/**
@@ -245,4 +259,36 @@ protected function getRecipientAddress()
245
259
->willReturn (1 );
246
260
return $ address ;
247
261
}
262
+
263
+ /**
264
+ * Data provider to testRequestToShipment
265
+ * @return array
266
+ */
267
+ public function requestToShipmentDataProvider ()
268
+ {
269
+ return [
270
+ [
271
+ 'CA '
272
+ ],
273
+ [
274
+ null
275
+ ]
276
+ ];
277
+ }
278
+
279
+ /**
280
+ * Data provider to testRequestToShipmentLocalizedException
281
+ * @return array
282
+ */
283
+ public function testRequestToShipmentLocalizedExceptionDataProvider ()
284
+ {
285
+ return [
286
+ [
287
+ true
288
+ ],
289
+ [
290
+ false
291
+ ]
292
+ ];
293
+ }
248
294
}
0 commit comments