77 $this->shipmentLoaderMock = $this->createPartialMock(
78 \
Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
79 [
'setOrderId',
'setShipmentId',
'setShipment',
'setTracking',
'load']
81 $this->labelGenerator = $this->createPartialMock(
82 \
Magento\Shipping\Model\Shipping\LabelGenerator::class,
83 [
'createPdfPageFromImageString']
85 $this->fileFactoryMock = $this->createPartialMock(
89 $this->shipmentMock = $this->createPartialMock(
90 \
Magento\Sales\Model\Order\Shipment::class,
91 [
'getIncrementId',
'getShippingLabel',
'__wakeup']
93 $this->messageManagerMock = $this->createPartialMock(\
Magento\Framework\Message\Manager::class, [
'addError']);
94 $this->requestMock = $this->createPartialMock(\
Magento\Framework\
App\Request\Http::class, [
'getParam']);
96 $this->sessionMock = $this->createPartialMock(\
Magento\Backend\Model\Session::class, [
'setIsUrlNotice']);
97 $this->actionFlag = $this->createPartialMock(\
Magento\Framework\
App\ActionFlag::class, [
'get']);
98 $this->objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
99 $this->helperMock = $this->createPartialMock(\
Magento\Backend\Helper\Data::class, [
'getUrl']);
100 $contextMock = $this->createPartialMock(\
Magento\Backend\
App\
Action\Context::class, [
110 $contextMock->expects($this->any())->method(
'getRequest')->will($this->returnValue($this->requestMock));
111 $contextMock->expects($this->any())->method(
'getResponse')->will($this->returnValue($this->responseMock));
112 $contextMock->expects($this->any())->method(
'getSession')->will($this->returnValue($this->sessionMock));
113 $contextMock->expects($this->any())->method(
'getActionFlag')->will($this->returnValue($this->actionFlag));
114 $contextMock->expects($this->any())->method(
'getHelper')->will($this->returnValue($this->helperMock));
115 $contextMock->expects($this->any())
116 ->method(
'getMessageManager')
117 ->will($this->returnValue($this->messageManagerMock));
118 $contextMock->expects($this->any())
119 ->method(
'getObjectManager')
120 ->will($this->returnValue($this->objectManagerMock));
123 $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\PrintLabel(
125 $this->shipmentLoaderMock,
126 $this->labelGenerator,
127 $this->fileFactoryMock
143 $this->requestMock->expects($this->at(0))
146 ->will($this->returnValue($orderId));
147 $this->requestMock->expects($this->at(1))
149 ->with(
'shipment_id')
150 ->will($this->returnValue($shipmentId));
151 $this->requestMock->expects($this->at(2))
155 $this->requestMock->expects($this->at(3))
158 ->will($this->returnValue($tracking));
159 $this->shipmentLoaderMock->expects($this->once())
160 ->method(
'setOrderId')
162 $this->shipmentLoaderMock->expects($this->once())
163 ->method(
'setShipmentId')
165 $this->shipmentLoaderMock->expects($this->once())
166 ->method(
'setShipment')
168 $this->shipmentLoaderMock->expects($this->once())
169 ->method(
'setTracking')
180 $resultContent =
'result-pdf-content';
181 $incrementId =
'1000001';
183 $this->shipmentMock->expects($this->once())
184 ->method(
'getIncrementId')
185 ->will($this->returnValue($incrementId));
186 $this->fileFactoryMock->expects($this->once())
188 ->will($this->returnValue($resultContent));
190 return $resultContent;
200 $this->actionFlag->expects($this->once())
202 ->with(
'', \
Magento\Backend\
App\AbstractAction::FLAG_IS_URLS_CHECKED)
203 ->will($this->returnValue(
true));
204 $this->sessionMock->expects($this->once())->method(
'setIsUrlNotice')->with(
true);
205 $this->helperMock->expects($this->once())->method(
'getUrl')->will($this->returnValue(
'redirect-path'));
206 $this->responseMock->expects($this->once())->method(
'setRedirect');
214 $labelContent =
'%PDF-label-content';
216 $this->shipmentLoaderMock->expects($this->once())
218 ->will($this->returnValue($this->shipmentMock));
219 $this->shipmentMock->expects($this->once())
220 ->method(
'getShippingLabel')
221 ->will($this->returnValue($labelContent));
223 $this->assertEquals($this->
fileCreate(), $this->controller->execute());
231 $labelContent =
'Label-content';
232 $pdfPageMock = $this->createPartialMock(\Zend_Pdf_Page::class, [
'render',
'getPageDictionary']);
233 $pageDictionaryMock = $this->createPartialMock(\Zend_Pdf_Element_Dictionary::class, [
'touch',
'getObject']);
235 $this->shipmentLoaderMock->expects($this->once())
237 ->will($this->returnValue($this->shipmentMock));
238 $this->shipmentMock->expects($this->once())
239 ->method(
'getShippingLabel')
240 ->will($this->returnValue($labelContent));
241 $this->labelGenerator->expects($this->once())
242 ->method(
'createPdfPageFromImageString')
243 ->with($labelContent)
244 ->will($this->returnValue($pdfPageMock));
245 $pdfPageMock->expects($this->any())
246 ->method(
'getPageDictionary')
247 ->will($this->returnValue($pageDictionaryMock));
248 $pageDictionaryMock->expects($this->any())
249 ->method(
'getObject')
250 ->will($this->returnSelf());
252 $this->assertEquals($this->
fileCreate(), $this->controller->execute());
260 $labelContent =
'Label-content';
261 $incrementId =
'1000001';
263 $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
265 $this->shipmentLoaderMock->expects($this->once())
267 ->will($this->returnValue($this->shipmentMock));
268 $this->shipmentMock->expects($this->once())
269 ->method(
'getShippingLabel')
270 ->will($this->returnValue($labelContent));
271 $this->shipmentMock->expects($this->once())
272 ->method(
'getIncrementId')
273 ->will($this->returnValue($incrementId));
274 $this->labelGenerator->expects($this->once())
275 ->method(
'createPdfPageFromImageString')
276 ->with($labelContent)
277 ->will($this->returnValue(
false));
278 $this->messageManagerMock->expects($this->at(0))
280 ->with(sprintf(
'We don\'t recognize or support the file extension in this shipment: %s.', $incrementId))
281 ->will($this->throwException(
new \Exception()));
282 $this->messageManagerMock->expects($this->at(1))
284 ->with(
'An error occurred while creating shipping label.')
285 ->will($this->returnSelf());
286 $this->objectManagerMock->expects($this->once())
288 ->with(\Psr\Log\LoggerInterface::class)
289 ->will($this->returnValue($loggerMock));
290 $loggerMock->expects($this->once())
291 ->method(
'critical');
292 $this->requestMock->expects($this->at(4))
294 ->with(
'shipment_id')
295 ->will($this->returnValue(1));
298 $this->assertNull($this->controller->execute());
306 $this->shipmentLoaderMock->expects($this->once())
308 ->willThrowException(
new \
Magento\Framework\Exception\LocalizedException(
__(
'message')));
309 $this->messageManagerMock->expects($this->once())->method(
'addError')->will($this->returnSelf());
312 $this->assertNull($this->controller->execute());
testExecuteImageStringFail()
testExecuteFromImageString()
foreach($order->getItems() as $orderItem) $shipment
testExecuteLoadShipmentFail()