Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LabelGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
11 
19 class LabelGeneratorTest extends \PHPUnit\Framework\TestCase
20 {
21  const CARRIER_CODE = 'fedex';
22 
23  const CARRIER_TITLE = 'Fedex carrier';
24 
28  private $carrierFactory;
29 
33  private $labelsFactory;
34 
38  private $scopeConfig;
39 
43  private $trackFactory;
44 
48  private $filesystem;
49 
53  private $labelGenerator;
54 
55  protected function setUp()
56  {
57  $this->carrierFactory = $this->getMockBuilder(\Magento\Shipping\Model\CarrierFactory::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->labelsFactory = $this->getMockBuilder(\Magento\Shipping\Model\Shipping\LabelsFactory::class)
61  ->disableOriginalConstructor()
62  ->setMethods(['create'])
63  ->getMock();
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'])
68  ->getMock();
69  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $this->labelGenerator = new \Magento\Shipping\Model\Shipping\LabelGenerator(
74  $this->carrierFactory,
75  $this->labelsFactory,
76  $this->scopeConfig,
77  $this->trackFactory,
78  $this->filesystem
79  );
80  }
81 
87  public function testAddTrackingNumbersToShipment(array $info)
88  {
89  $order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $order->expects(static::once())
93  ->method('getShippingMethod')
94  ->with(true)
95  ->willReturn($this->getShippingMethodMock());
96 
100  $shipmentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103  $shipmentMock->expects(static::once())->method('getOrder')->willReturn($order);
104 
105  $this->carrierFactory->expects(static::once())
106  ->method('create')
107  ->with(self::CARRIER_CODE)
108  ->willReturn($this->getCarrierMock());
109 
110  $labelsMock = $this->getMockBuilder(\Magento\Shipping\Model\Shipping\Labels::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113  $labelsMock->expects(static::once())
114  ->method('requestToShipment')
115  ->with($shipmentMock)
116  ->willReturn($this->getResponseMock($info));
117 
118  $this->labelsFactory->expects(static::once())
119  ->method('create')
120  ->willReturn($labelsMock);
121 
122  $this->filesystem->expects(static::once())
123  ->method('getDirectoryWrite')
124  ->willReturn($this->createMock(\Magento\Framework\Filesystem\Directory\WriteInterface::class));
125 
126  $this->scopeConfig->expects(static::once())
127  ->method('getValue')
128  ->with(
129  'carriers/' . self::CARRIER_CODE . '/title',
131  null
132  )->willReturn(self::CARRIER_TITLE);
133 
134  $this->labelsFactory->expects(static::once())
135  ->method('create')
136  ->willReturn($labelsMock);
137 
138  $trackMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment\Track::class)
139  ->setMethods(['setNumber', 'setCarrierCode', 'setTitle'])
140  ->disableOriginalConstructor()
141  ->getMock();
142 
143  $i = 0;
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)
149  ->willReturnSelf();
150  $trackMock->expects(static::at($i++))
151  ->method('setCarrierCode')
152  ->with(self::CARRIER_CODE)
153  ->willReturnSelf();
154  $trackMock->expects(static::at($i++))
155  ->method('setTitle')
156  ->with(self::CARRIER_TITLE)
157  ->willReturnSelf();
158  }
159 
160  $this->trackFactory->expects(static::any())
161  ->method('create')
162  ->willReturn($trackMock);
163 
167  $requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
168  $this->labelGenerator->create($shipmentMock, $requestMock);
169  }
170 
174  private function getShippingMethodMock()
175  {
176  $shippingMethod = $this->getMockBuilder(\Magento\Framework\DataObject::class)
177  ->disableOriginalConstructor()
178  ->setMethods(['getCarrierCode'])
179  ->getMock();
180  $shippingMethod->expects(static::once())
181  ->method('getCarrierCode')
182  ->willReturn(self::CARRIER_CODE);
183 
184  return $shippingMethod;
185  }
186 
190  private function getCarrierMock()
191  {
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')
198  ->willReturn(true);
199  $carrierMock->expects(static::once())
200  ->method('getCarrierCode')
201  ->willReturn(self::CARRIER_CODE);
202 
203  return $carrierMock;
204  }
205 
210  private function getResponseMock(array $info)
211  {
212  $responseMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
213  ->setMethods(['hasErrors', 'hasInfo', 'getInfo'])
214  ->disableOriginalConstructor()
215  ->getMock();
216  $responseMock->expects(static::once())
217  ->method('hasErrors')
218  ->willReturn(false);
219  $responseMock->expects(static::once())
220  ->method('hasInfo')
221  ->willReturn(true);
222  $responseMock->expects(static::once())
223  ->method('getInfo')
224  ->willReturn([$info]);
225 
226  return $responseMock;
227  }
228 
232  public function labelInfoDataProvider()
233  {
234  return [
235  [['tracking_number' => ['111111', '222222', '333333'], 'label_content' => 'some']],
236  [['tracking_number' => '111111', 'label_content' => 'some']],
237  ];
238  }
239 }
$order
Definition: order.php:55
$shippingMethod
Definition: popup.phtml:12
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$i
Definition: gallery.phtml:31