Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PrintLabelTest.php
Go to the documentation of this file.
1 <?php
7 
13 class PrintLabelTest extends \PHPUnit\Framework\TestCase
14 {
19 
23  protected $shipmentMock;
24 
28  protected $fileFactoryMock;
29 
33  protected $labelGenerator;
34 
38  protected $requestMock;
39 
43  protected $responseMock;
44 
49 
53  protected $objectManagerMock;
54 
58  protected $sessionMock;
59 
63  protected $actionFlag;
64 
68  protected $helperMock;
69 
73  protected $controller;
74 
75  protected function setUp()
76  {
77  $this->shipmentLoaderMock = $this->createPartialMock(
78  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
79  ['setOrderId', 'setShipmentId', 'setShipment', 'setTracking', 'load']
80  );
81  $this->labelGenerator = $this->createPartialMock(
82  \Magento\Shipping\Model\Shipping\LabelGenerator::class,
83  ['createPdfPageFromImageString']
84  );
85  $this->fileFactoryMock = $this->createPartialMock(
86  \Magento\Framework\App\Response\Http\FileFactory::class,
87  ['create']
88  );
89  $this->shipmentMock = $this->createPartialMock(
90  \Magento\Sales\Model\Order\Shipment::class,
91  ['getIncrementId', 'getShippingLabel', '__wakeup']
92  );
93  $this->messageManagerMock = $this->createPartialMock(\Magento\Framework\Message\Manager::class, ['addError']);
94  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParam']);
95  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
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, [
101  'getRequest',
102  'getResponse',
103  'getMessageManager',
104  'getSession',
105  'getActionFlag',
106  'getObjectManager',
107  'getHelper'
108  ]);
109 
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));
121  $this->loadShipment();
122 
123  $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\PrintLabel(
124  $contextMock,
125  $this->shipmentLoaderMock,
126  $this->labelGenerator,
127  $this->fileFactoryMock
128  );
129  }
130 
136  protected function loadShipment()
137  {
138  $orderId = 1;
139  $shipmentId = 1;
140  $shipment = [];
141  $tracking = [];
142 
143  $this->requestMock->expects($this->at(0))
144  ->method('getParam')
145  ->with('order_id')
146  ->will($this->returnValue($orderId));
147  $this->requestMock->expects($this->at(1))
148  ->method('getParam')
149  ->with('shipment_id')
150  ->will($this->returnValue($shipmentId));
151  $this->requestMock->expects($this->at(2))
152  ->method('getParam')
153  ->with('shipment')
154  ->will($this->returnValue($shipment));
155  $this->requestMock->expects($this->at(3))
156  ->method('getParam')
157  ->with('tracking')
158  ->will($this->returnValue($tracking));
159  $this->shipmentLoaderMock->expects($this->once())
160  ->method('setOrderId')
161  ->with($orderId);
162  $this->shipmentLoaderMock->expects($this->once())
163  ->method('setShipmentId')
164  ->with($shipmentId);
165  $this->shipmentLoaderMock->expects($this->once())
166  ->method('setShipment')
167  ->with($shipment);
168  $this->shipmentLoaderMock->expects($this->once())
169  ->method('setTracking')
170  ->with($tracking);
171  }
172 
178  protected function fileCreate()
179  {
180  $resultContent = 'result-pdf-content';
181  $incrementId = '1000001';
182 
183  $this->shipmentMock->expects($this->once())
184  ->method('getIncrementId')
185  ->will($this->returnValue($incrementId));
186  $this->fileFactoryMock->expects($this->once())
187  ->method('create')
188  ->will($this->returnValue($resultContent));
189 
190  return $resultContent;
191  }
192 
198  protected function redirectSection()
199  {
200  $this->actionFlag->expects($this->once())
201  ->method('get')
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');
207  }
208 
212  public function testExecute()
213  {
214  $labelContent = '%PDF-label-content';
215 
216  $this->shipmentLoaderMock->expects($this->once())
217  ->method('load')
218  ->will($this->returnValue($this->shipmentMock));
219  $this->shipmentMock->expects($this->once())
220  ->method('getShippingLabel')
221  ->will($this->returnValue($labelContent));
222 
223  $this->assertEquals($this->fileCreate(), $this->controller->execute());
224  }
225 
229  public function testExecuteFromImageString()
230  {
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']);
234 
235  $this->shipmentLoaderMock->expects($this->once())
236  ->method('load')
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());
251 
252  $this->assertEquals($this->fileCreate(), $this->controller->execute());
253  }
254 
258  public function testExecuteImageStringFail()
259  {
260  $labelContent = 'Label-content';
261  $incrementId = '1000001';
262 
263  $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
264 
265  $this->shipmentLoaderMock->expects($this->once())
266  ->method('load')
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))
279  ->method('addError')
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))
283  ->method('addError')
284  ->with('An error occurred while creating shipping label.')
285  ->will($this->returnSelf());
286  $this->objectManagerMock->expects($this->once())
287  ->method('get')
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))
293  ->method('getParam')
294  ->with('shipment_id')
295  ->will($this->returnValue(1));
296  $this->redirectSection();
297 
298  $this->assertNull($this->controller->execute());
299  }
300 
304  public function testExecuteLoadShipmentFail()
305  {
306  $this->shipmentLoaderMock->expects($this->once())
307  ->method('load')
308  ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message')));
309  $this->messageManagerMock->expects($this->once())->method('addError')->will($this->returnSelf());
310  $this->redirectSection();
311 
312  $this->assertNull($this->controller->execute());
313  }
314 }
__()
Definition: __.php:13
foreach($order->getItems() as $orderItem) $shipment