Skip to content

Commit f176b94

Browse files
committed
Merge branch 'ACP2E-3676' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-02-28-2025
2 parents d4dd552 + 5c68b78 commit f176b94

File tree

2 files changed

+85
-32
lines changed

2 files changed

+85
-32
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: 66 additions & 21 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

@@ -123,29 +123,19 @@ public function testAddTrackingNumbersToShipment(array $info): void
123123
->getMock();
124124
$shipmentMock->expects(static::once())->method('getOrder')->willReturn($order);
125125

126-
$this->carrierFactory->expects(static::once())
127-
->method('create')
128-
->with(self::CARRIER_CODE)
126+
$this->carrierFactory->expects(static::once())->method('create')->with(self::CARRIER_CODE)
129127
->willReturn($this->getCarrierMock());
130128

131-
$labelsMock = $this->getMockBuilder(Labels::class)
132-
->disableOriginalConstructor()
133-
->getMock();
134-
$labelsMock->expects(static::once())
135-
->method('requestToShipment')
136-
->with($shipmentMock)
129+
$labelsMock = $this->getMockBuilder(Labels::class)->disableOriginalConstructor()->getMock();
130+
$labelsMock->expects(static::once())->method('requestToShipment')->with($shipmentMock)
137131
->willReturn($this->getResponseMock($info));
138132

139-
$this->labelsFactory->expects(static::once())
140-
->method('create')
141-
->willReturn($labelsMock);
133+
$this->labelsFactory->expects(static::once())->method('create')->willReturn($labelsMock);
142134

143-
$this->filesystem->expects(static::once())
144-
->method('getDirectoryWrite')
135+
$this->filesystem->expects(static::once())->method('getDirectoryWrite')
145136
->willReturn($this->getMockForAbstractClass(WriteInterface::class));
146137

147-
$this->scopeConfig->expects(static::once())
148-
->method('getValue')
138+
$this->scopeConfig->expects(static::once())->method('getValue')
149139
->with(
150140
'carriers/' . self::CARRIER_CODE . '/title',
151141
ScopeInterface::SCOPE_STORE,
@@ -204,9 +194,7 @@ public function testAddTrackingNumbersToShipment(array $info): void
204194
}
205195
});
206196

207-
$this->trackFactory->expects(static::any())
208-
->method('create')
209-
->willReturn($trackMock);
197+
$this->trackFactory->expects(static::any())->method('create')->willReturn($trackMock);
210198

211199
/**
212200
* @var $requestMock \Magento\Framework\App\RequestInterface|MockObject
@@ -284,4 +272,61 @@ public static function labelInfoDataProvider(): array
284272
[['tracking_number' => '111111', 'label_content' => 'some']]
285273
];
286274
}
275+
276+
public function testCreateResponseHasErrors()
277+
{
278+
$order = $this->getMockBuilder(Order::class)
279+
->disableOriginalConstructor()
280+
->getMock();
281+
$order->expects(static::once())
282+
->method('getShippingMethod')
283+
->with(true)
284+
->willReturn($this->getShippingMethodMock());
285+
286+
/**
287+
* @var $shipmentMock \Magento\Sales\Model\Order\Shipment|MockObject
288+
*/
289+
$shipmentMock = $this->getMockBuilder(Shipment::class)
290+
->disableOriginalConstructor()
291+
->getMock();
292+
$shipmentMock->expects(static::once())->method('getOrder')->willReturn($order);
293+
294+
$carrierMock = $this->getMockBuilder(AbstractCarrier::class)
295+
->disableOriginalConstructor()
296+
->onlyMethods(['isShippingLabelsAvailable', 'getCarrierCode'])
297+
->getMockForAbstractClass();
298+
$carrierMock->expects(static::once())
299+
->method('isShippingLabelsAvailable')
300+
->willReturn(true);
301+
$this->carrierFactory->expects(static::once())
302+
->method('create')
303+
->with(self::CARRIER_CODE)
304+
->willReturn($carrierMock);
305+
306+
$responseMock = $this->getMockBuilder(DataObject::class)
307+
->addMethods(['hasErrors', 'getErrors'])->disableOriginalConstructor()->getMock();
308+
$responseMock->expects(static::once())
309+
->method('hasErrors')
310+
->willReturn(true);
311+
$responseMock->expects(static::once())->method('getErrors')->willReturn(['Error message']);
312+
313+
$labelsMock = $this->getMockBuilder(Labels::class)
314+
->disableOriginalConstructor()
315+
->getMock();
316+
$labelsMock->expects(static::once())
317+
->method('requestToShipment')
318+
->with($shipmentMock)
319+
->willReturn($responseMock);
320+
321+
$this->labelsFactory->expects(static::once())
322+
->method('create')
323+
->willReturn($labelsMock);
324+
325+
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
326+
/**
327+
* @var $requestMock \Magento\Framework\App\RequestInterface|MockObject
328+
*/
329+
$requestMock = $this->getMockForAbstractClass(RequestInterface::class);
330+
$this->labelGenerator->create($shipmentMock, $requestMock);
331+
}
287332
}

0 commit comments

Comments
 (0)