5
5
*/
6
6
namespace Magento \Shipping \Test \Unit \Model \Shipping ;
7
7
8
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
- use Magento \Framework \DataObject ;
8
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
10
9
use Magento \Sales \Model \Order \Shipment ;
11
10
use Magento \Store \Model \ScopeInterface ;
12
11
@@ -39,6 +38,16 @@ class LabelsTest extends \PHPUnit_Framework_TestCase
39
38
*/
40
39
protected $ region ;
41
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
+
42
51
protected function setUp ()
43
52
{
44
53
$ this ->request = $ this ->getMockBuilder (\Magento \Shipping \Model \Shipment \Request::class)
@@ -49,32 +58,32 @@ protected function setUp()
49
58
->setMethods (['create ' ])
50
59
->getMock ();
51
60
$ requestFactory ->expects (static ::any ())->method ('create ' )->willReturn ($ this ->request );
52
-
53
- $ carrier = $ this ->getMockBuilder (\Magento \Shipping \Model \Carrier \AbstractCarrier::class)
61
+ $ this ->carrierFactory = $ this ->getMockBuilder (\Magento \Shipping \Model \CarrierFactory::class)
54
62
->disableOriginalConstructor ()
63
+ ->setMethods (['create ' ])
55
64
->getMock ();
56
-
57
- $ carrierFactory = $ this ->getMockBuilder (\Magento \Shipping \Model \CarrierFactory ::class)
65
+ $ storeManager = $ this -> getStoreManager ();
66
+ $ this -> user = $ this ->getMockBuilder (\Magento \User \Model \User ::class)
58
67
->disableOriginalConstructor ()
59
- ->setMethods (['create ' ])
68
+ ->setMethods (['getFirstname ' , ' getLastname ' , ' getEmail ' , ' getName ' ])
60
69
->getMock ();
61
- $ carrierFactory ->expects (static ::any ())->method ('create ' )->willReturn ($ carrier );
62
70
63
- $ storeManager = $ this ->getStoreManager ();
64
- $ 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 );
65
76
$ regionFactory = $ this ->getRegionFactory ();
66
-
67
77
$ this ->scopeConfig = $ this ->getMockBuilder (\Magento \Framework \App \Config::class)
68
78
->disableOriginalConstructor ()
69
79
->setMethods (['getValue ' ])
70
80
->getMock ();
71
-
72
- $ objectManagerHelper = new ObjectManager ($ this );
81
+ $ objectManagerHelper = new ObjectManagerHelper ($ this );
73
82
$ this ->labels = $ objectManagerHelper ->getObject (
74
83
\Magento \Shipping \Model \Shipping \Labels::class,
75
84
[
76
85
'shipmentRequestFactory ' => $ requestFactory ,
77
- 'carrierFactory ' => $ carrierFactory ,
86
+ 'carrierFactory ' => $ this -> carrierFactory ,
78
87
'storeManager ' => $ storeManager ,
79
88
'scopeConfig ' => $ this ->scopeConfig ,
80
89
'authSession ' => $ authSession ,
@@ -84,14 +93,21 @@ protected function setUp()
84
93
}
85
94
86
95
/**
87
- * @covers \Magento\Shipping\Model\Shipping\Labels
96
+ * @dataProvider requestToShipmentDataProvider
88
97
*/
89
- public function testRequestToShipment ()
98
+ public function testRequestToShipment ($ regionId )
90
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 );
91
104
$ order = $ this ->getMockBuilder (\Magento \Sales \Model \Order::class)
92
105
->disableOriginalConstructor ()
93
106
->getMock ();
94
-
107
+ $ this ->user ->expects ($ this ->atLeastOnce ())->method ('getFirstname ' )->willReturn ('John ' );
108
+ $ this ->user ->expects ($ this ->atLeastOnce ())->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 ' );
95
111
$ shippingMethod = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
96
112
->disableOriginalConstructor ()
97
113
->setMethods (['getCarrierCode ' ])
@@ -125,7 +141,7 @@ public function testRequestToShipment()
125
141
$ this ->scopeConfig ->expects (static ::any ())
126
142
->method ('getValue ' )
127
143
->willReturnMap ([
128
- [Shipment::XML_PATH_STORE_REGION_ID , ScopeInterface::SCOPE_STORE , $ storeId , ' CA ' ],
144
+ [Shipment::XML_PATH_STORE_REGION_ID , ScopeInterface::SCOPE_STORE , $ storeId , $ regionId ],
129
145
[Shipment::XML_PATH_STORE_ADDRESS1 , ScopeInterface::SCOPE_STORE , $ storeId , 'Beverly Heals ' ],
130
146
['general/store_information ' , ScopeInterface::SCOPE_STORE , $ storeId , [
131
147
'name ' => 'General Store ' , 'phone ' => '(244)1500301 '
@@ -135,38 +151,35 @@ public function testRequestToShipment()
135
151
[Shipment::XML_PATH_STORE_COUNTRY_ID , ScopeInterface::SCOPE_STORE , $ storeId , 'US ' ],
136
152
[Shipment::XML_PATH_STORE_ADDRESS2 , ScopeInterface::SCOPE_STORE , $ storeId , '1st Park Avenue ' ],
137
153
]);
138
-
139
154
$ this ->labels ->requestToShipment ($ shipment );
140
155
}
141
156
142
157
/**
143
- * @return \PHPUnit_Framework_MockObject_MockObject
158
+ * @expectedException \Magento\Framework\Exception\LocalizedException
159
+ * @dataProvider testRequestToShipmentLocalizedExceptionDataProvider
144
160
*/
145
- protected function getAuthSession ( )
161
+ public function testRequestToShipmentLocalizedException ( $ isShipmentCarrierNotNull )
146
162
{
147
- $ user = $ this ->getMockBuilder (\Magento \User \Model \User ::class)
163
+ $ order = $ this ->getMockBuilder (\Magento \Sales \Model \Order ::class)
148
164
->disableOriginalConstructor ()
149
- ->setMethods (['getFirstname ' , 'getLastname ' , 'getEmail ' , 'getName ' ])
150
165
->getMock ();
151
- $ user ->expects (static ::exactly (2 ))
152
- ->method ('getFirstname ' )
153
- ->willReturn ('John ' );
154
- $ user ->expects (static ::exactly (2 ))
155
- ->method ('getLastname ' )
156
- ->willReturn ('Doe ' );
157
- $ user ->expects (static ::once ())
158
- ->method ('getName ' )
159
- ->willReturn ('John Doe ' );
160
- $ user ->expects (static ::once ())
161
- ->method ('getEmail ' )
162
- ->willReturn ('admin@admin.test.com ' );
163
-
164
- $ authSession = $ this ->getMockBuilder (\Magento \Backend \Model \Auth \Session::class)
166
+ $ shipment = $ this ->getMockBuilder (\Magento \Sales \Model \Order \Shipment::class)
165
167
->disableOriginalConstructor ()
166
- ->setMethods (['getUser ' ])
167
168
->getMock ();
168
- $ authSession ->expects (static ::any ())->method ('getUser ' )->willReturn ($ user );
169
- return $ authSession ;
169
+ $ shippingMethod = $ this ->getMockBuilder (\Magento \Framework \DataObject::class)
170
+ ->disableOriginalConstructor ()
171
+ ->setMethods (['getCarrierCode ' ])
172
+ ->getMock ();
173
+ $ order ->expects ($ this ->atLeastOnce ())
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 );
170
183
}
171
184
172
185
/**
@@ -246,4 +259,36 @@ protected function getRecipientAddress()
246
259
->willReturn (1 );
247
260
return $ address ;
248
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
+ }
249
294
}
0 commit comments