Skip to content

Commit 5134e04

Browse files
committed
MAGETWO-69185: Unable to create shipping label if Region not specified for shipping origin
1 parent a5f00a1 commit 5134e04

File tree

1 file changed

+85
-39
lines changed

1 file changed

+85
-39
lines changed

app/code/Magento/Shipping/Test/Unit/Model/Shipping/LabelsTest.php

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ class LabelsTest extends \PHPUnit_Framework_TestCase
3838
*/
3939
protected $region;
4040

41+
/**
42+
* @var \PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $carrierFactory;
45+
46+
/**
47+
* @var \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $user;
50+
4151
protected function setUp()
4252
{
4353
$this->request = $this->getMockBuilder(\Magento\Shipping\Model\Shipment\Request::class)
@@ -48,32 +58,32 @@ protected function setUp()
4858
->setMethods(['create'])
4959
->getMock();
5060
$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)
5362
->disableOriginalConstructor()
63+
->setMethods(['create'])
5464
->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)
5767
->disableOriginalConstructor()
58-
->setMethods(['create'])
68+
->setMethods(['getFirstname', 'getLastname', 'getEmail', 'getName'])
5969
->getMock();
60-
$carrierFactory->expects(static::any())->method('create')->willReturn($carrier);
6170

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);
6476
$regionFactory = $this->getRegionFactory();
65-
6677
$this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config::class)
6778
->disableOriginalConstructor()
6879
->setMethods(['getValue'])
6980
->getMock();
70-
71-
$bjectManagerHelper = new ObjectManagerHelper($this);
72-
$this->labels = $bjectManagerHelper->getObject(
81+
$objectManagerHelper = new ObjectManagerHelper($this);
82+
$this->labels = $objectManagerHelper->getObject(
7383
\Magento\Shipping\Model\Shipping\Labels::class,
7484
[
7585
'shipmentRequestFactory' => $requestFactory,
76-
'carrierFactory' => $carrierFactory,
86+
'carrierFactory' => $this->carrierFactory,
7787
'storeManager' => $storeManager,
7888
'scopeConfig' => $this->scopeConfig,
7989
'authSession' => $authSession,
@@ -83,14 +93,21 @@ protected function setUp()
8393
}
8494

8595
/**
86-
* @covers \Magento\Shipping\Model\Shipping\Labels
96+
* @dataProvider requestToShipmentDataProvider
8797
*/
88-
public function testRequestToShipment()
98+
public function testRequestToShipment($regionId)
8999
{
100+
$carrier = $this->getMockBuilder(\Magento\Shipping\Model\Carrier\AbstractCarrier::class)
101+
->disableOriginalConstructor()
102+
->getMock();
103+
$this->carrierFactory->expects(static::any())->method('create')->willReturn($carrier);
90104
$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
91105
->disableOriginalConstructor()
92106
->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');
94111
$shippingMethod = $this->getMockBuilder(\Magento\Framework\DataObject::class)
95112
->disableOriginalConstructor()
96113
->setMethods(['getCarrierCode'])
@@ -124,7 +141,7 @@ public function testRequestToShipment()
124141
$this->scopeConfig->expects(static::any())
125142
->method('getValue')
126143
->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],
128145
[Shipment::XML_PATH_STORE_ADDRESS1, ScopeInterface::SCOPE_STORE, $storeId, 'Beverly Heals'],
129146
['general/store_information', ScopeInterface::SCOPE_STORE, $storeId, [
130147
'name' => 'General Store', 'phone' => '(244)1500301'
@@ -134,38 +151,35 @@ public function testRequestToShipment()
134151
[Shipment::XML_PATH_STORE_COUNTRY_ID, ScopeInterface::SCOPE_STORE, $storeId, 'US'],
135152
[Shipment::XML_PATH_STORE_ADDRESS2, ScopeInterface::SCOPE_STORE, $storeId, '1st Park Avenue'],
136153
]);
137-
138154
$this->labels->requestToShipment($shipment);
139155
}
140156

141157
/**
142-
* @return \PHPUnit_Framework_MockObject_MockObject
158+
* @expectedException \Magento\Framework\Exception\LocalizedException
159+
* @dataProvider testRequestToShipmentLocalizedExceptionDataProvider
143160
*/
144-
protected function getAuthSession()
161+
public function testRequestToShipmentLocalizedException($isShipmentCarrierNotNull)
145162
{
146-
$user = $this->getMockBuilder(\Magento\User\Model\User::class)
163+
$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
147164
->disableOriginalConstructor()
148-
->setMethods(['getFirstname', 'getLastname', 'getEmail', 'getName'])
149165
->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)
164167
->disableOriginalConstructor()
165-
->setMethods(['getUser'])
166168
->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);
169183
}
170184

171185
/**
@@ -245,4 +259,36 @@ protected function getRecipientAddress()
245259
->willReturn(1);
246260
return $address;
247261
}
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+
}
248294
}

0 commit comments

Comments
 (0)