Skip to content

Commit fabfa98

Browse files
committed
ACP2E-3676: Exception while creating UPS shipping label
1 parent 96dec49 commit fabfa98

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

app/code/Magento/Shipping/Model/Shipping/LabelGenerator.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22
/**
3-
*
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
65
*/
76
namespace Magento\Shipping\Model\Shipping;
87

98
use Magento\Framework\App\Filesystem\DirectoryList;
109
use Magento\Framework\App\RequestInterface;
10+
use Magento\Sales\Model\Order\Shipment;
1111

1212
/**
1313
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
* phpcs:disable Magento2.Functions.DiscouragedFunction
1415
*/
1516
class LabelGenerator
1617
{
@@ -61,12 +62,14 @@ public function __construct(
6162
}
6263

6364
/**
64-
* @param \Magento\Sales\Model\Order\Shipment $shipment
65+
* Creates a shipping label
66+
*
67+
* @param Shipment $shipment
6568
* @param RequestInterface $request
6669
* @return void
6770
* @throws \Magento\Framework\Exception\LocalizedException
6871
*/
69-
public function create(\Magento\Sales\Model\Order\Shipment $shipment, RequestInterface $request)
72+
public function create(Shipment $shipment, RequestInterface $request)
7073
{
7174
$order = $shipment->getOrder();
7275
$carrier = $this->carrierFactory->create($order->getShippingMethod(true)->getCarrierCode());
@@ -76,7 +79,8 @@ public function create(\Magento\Sales\Model\Order\Shipment $shipment, RequestInt
7679
$shipment->setPackages($request->getParam('packages'));
7780
$response = $this->labelFactory->create()->requestToShipment($shipment);
7881
if ($response->hasErrors()) {
79-
throw new \Magento\Framework\Exception\LocalizedException(__($response->getErrors()));
82+
$firstError = $response->getErrors()[0];
83+
throw new \Magento\Framework\Exception\LocalizedException(__($firstError));
8084
}
8185
if (!$response->hasInfo()) {
8286
throw new \Magento\Framework\Exception\LocalizedException(__('Response info is not exist.'));
@@ -104,15 +108,17 @@ public function create(\Magento\Sales\Model\Order\Shipment $shipment, RequestInt
104108
}
105109

106110
/**
107-
* @param \Magento\Sales\Model\Order\Shipment $shipment
111+
* Adds tracking number to a shipment
112+
*
113+
* @param Shipment $shipment
108114
* @param array $trackingNumbers
109115
* @param string $carrierCode
110116
* @param string $carrierTitle
111117
*
112118
* @return void
113119
*/
114120
private function addTrackingNumbersToShipment(
115-
\Magento\Sales\Model\Order\Shipment $shipment,
121+
Shipment $shipment,
116122
$trackingNumbers,
117123
$carrierCode,
118124
$carrierTitle
@@ -168,9 +174,11 @@ public function createPdfPageFromImageString($imageString)
168174
$directory = $this->filesystem->getDirectoryWrite(
169175
DirectoryList::TMP
170176
);
171-
$directory->create();
172-
$image = @imagecreatefromstring($imageString);
173-
if (!$image) {
177+
178+
try {
179+
$directory->create();
180+
$image = imagecreatefromstring($imageString);
181+
} catch (\Exception $e) {
174182
return false;
175183
}
176184

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -284,4 +284,61 @@ public static function labelInfoDataProvider(): array
284284
[['tracking_number' => '111111', 'label_content' => 'some']]
285285
];
286286
}
287+
288+
public function testCreateResponseHasErrors()
289+
{
290+
$order = $this->getMockBuilder(Order::class)
291+
->disableOriginalConstructor()
292+
->getMock();
293+
$order->expects(static::once())
294+
->method('getShippingMethod')
295+
->with(true)
296+
->willReturn($this->getShippingMethodMock());
297+
298+
/**
299+
* @var $shipmentMock \Magento\Sales\Model\Order\Shipment|MockObject
300+
*/
301+
$shipmentMock = $this->getMockBuilder(Shipment::class)
302+
->disableOriginalConstructor()
303+
->getMock();
304+
$shipmentMock->expects(static::once())->method('getOrder')->willReturn($order);
305+
306+
$carrierMock = $this->getMockBuilder(AbstractCarrier::class)
307+
->disableOriginalConstructor()
308+
->onlyMethods(['isShippingLabelsAvailable', 'getCarrierCode'])
309+
->getMockForAbstractClass();
310+
$carrierMock->expects(static::once())
311+
->method('isShippingLabelsAvailable')
312+
->willReturn(true);
313+
$this->carrierFactory->expects(static::once())
314+
->method('create')
315+
->with(self::CARRIER_CODE)
316+
->willReturn($carrierMock);
317+
318+
$responseMock = $this->getMockBuilder(DataObject::class)
319+
->addMethods(['hasErrors', 'getErrors'])->disableOriginalConstructor()->getMock();
320+
$responseMock->expects(static::once())
321+
->method('hasErrors')
322+
->willReturn(true);
323+
$responseMock->expects(static::once())->method('getErrors')->willReturn(['Error message']);
324+
325+
$labelsMock = $this->getMockBuilder(Labels::class)
326+
->disableOriginalConstructor()
327+
->getMock();
328+
$labelsMock->expects(static::once())
329+
->method('requestToShipment')
330+
->with($shipmentMock)
331+
->willReturn($responseMock);
332+
333+
$this->labelsFactory->expects(static::once())
334+
->method('create')
335+
->willReturn($labelsMock);
336+
337+
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
338+
/**
339+
* @var $requestMock \Magento\Framework\App\RequestInterface|MockObject
340+
*/
341+
$requestMock = $this->getMockForAbstractClass(RequestInterface::class);
342+
$this->labelGenerator->create($shipmentMock, $requestMock);
343+
}
287344
}

0 commit comments

Comments
 (0)