28 private $carrierFactory;
33 private $labelsFactory;
43 private $trackFactory;
53 private $labelGenerator;
57 $this->carrierFactory = $this->getMockBuilder(\
Magento\
Shipping\Model\CarrierFactory::class)
58 ->disableOriginalConstructor()
61 ->disableOriginalConstructor()
62 ->setMethods([
'create'])
64 $this->scopeConfig = $this->createMock(\
Magento\Framework\
App\
Config\ScopeConfigInterface::class);
65 $this->trackFactory = $this->getMockBuilder(\
Magento\Sales\Model\
Order\
Shipment\TrackFactory::class)
66 ->disableOriginalConstructor()
67 ->setMethods([
'create'])
69 $this->filesystem = $this->getMockBuilder(\
Magento\Framework\Filesystem::class)
70 ->disableOriginalConstructor()
73 $this->labelGenerator = new \Magento\Shipping\Model\Shipping\LabelGenerator(
74 $this->carrierFactory,
87 public function testAddTrackingNumbersToShipment(array
$info)
89 $order = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)
90 ->disableOriginalConstructor()
92 $order->expects(static::once())
93 ->method(
'getShippingMethod')
95 ->willReturn($this->getShippingMethodMock());
100 $shipmentMock = $this->getMockBuilder(\
Magento\Sales\Model\
Order\Shipment::class)
101 ->disableOriginalConstructor()
103 $shipmentMock->expects(static::once())->method(
'getOrder')->willReturn(
$order);
105 $this->carrierFactory->expects(static::once())
107 ->with(self::CARRIER_CODE)
108 ->willReturn($this->getCarrierMock());
111 ->disableOriginalConstructor()
113 $labelsMock->expects(static::once())
114 ->method(
'requestToShipment')
115 ->with($shipmentMock)
116 ->willReturn($this->getResponseMock(
$info));
118 $this->labelsFactory->expects(static::once())
120 ->willReturn($labelsMock);
122 $this->filesystem->expects(static::once())
123 ->method(
'getDirectoryWrite')
124 ->willReturn($this->createMock(\
Magento\Framework\
Filesystem\Directory\WriteInterface::class));
126 $this->scopeConfig->expects(static::once())
129 'carriers/' . self::CARRIER_CODE .
'/title',
132 )->willReturn(self::CARRIER_TITLE);
134 $this->labelsFactory->expects(static::once())
136 ->willReturn($labelsMock);
139 ->setMethods([
'setNumber',
'setCarrierCode',
'setTitle'])
140 ->disableOriginalConstructor()
144 $trackingNumbers = is_array(
$info[
'tracking_number']) ?
$info[
'tracking_number'] : [
$info[
'tracking_number']];
145 foreach ($trackingNumbers as $trackingNumber) {
146 $trackMock->expects(static::at(
$i++))
147 ->method(
'setNumber')
148 ->with($trackingNumber)
150 $trackMock->expects(static::at(
$i++))
151 ->method(
'setCarrierCode')
152 ->with(self::CARRIER_CODE)
154 $trackMock->expects(static::at(
$i++))
156 ->with(self::CARRIER_TITLE)
160 $this->trackFactory->expects(static::any())
162 ->willReturn($trackMock);
167 $requestMock = $this->createMock(\
Magento\Framework\
App\RequestInterface::class);
168 $this->labelGenerator->create($shipmentMock, $requestMock);
174 private function getShippingMethodMock()
177 ->disableOriginalConstructor()
178 ->setMethods([
'getCarrierCode'])
181 ->method(
'getCarrierCode')
182 ->willReturn(self::CARRIER_CODE);
190 private function getCarrierMock()
192 $carrierMock = $this->getMockBuilder(\
Magento\Shipping\Model\Carrier\AbstractCarrier::class)
193 ->disableOriginalConstructor()
194 ->setMethods([
'isShippingLabelsAvailable',
'getCarrierCode'])
195 ->getMockForAbstractClass();
196 $carrierMock->expects(static::once())
197 ->method(
'isShippingLabelsAvailable')
199 $carrierMock->expects(static::once())
200 ->method(
'getCarrierCode')
201 ->willReturn(self::CARRIER_CODE);
210 private function getResponseMock(array
$info)
212 $responseMock = $this->getMockBuilder(\
Magento\Framework\DataObject::class)
213 ->setMethods([
'hasErrors',
'hasInfo',
'getInfo'])
214 ->disableOriginalConstructor()
216 $responseMock->expects(static::once())
217 ->method(
'hasErrors')
219 $responseMock->expects(static::once())
222 $responseMock->expects(static::once())
224 ->willReturn([
$info]);
226 return $responseMock;
235 [[
'tracking_number' => [
'111111',
'222222',
'333333'],
'label_content' =>
'some']],
236 [[
'tracking_number' =>
'111111',
'label_content' =>
'some']],
foreach( $_productCollection as $_product)() ?>" class $info