Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PrintPackageTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class PrintPackageTest extends \PHPUnit\Framework\TestCase
16 {
21 
25  protected $requestMock;
26 
30  protected $responseMock;
31 
35  protected $fileFactoryMock;
36 
40  protected $objectManagerMock;
41 
45  protected $sessionMock;
46 
50  protected $actionFlag;
51 
55  protected $shipmentMock;
56 
60  protected $controller;
61 
62  protected function setUp()
63  {
64  $orderId = 1;
65  $shipmentId = 1;
66  $shipment = [];
67  $tracking = [];
68 
69  $this->shipmentLoaderMock = $this->createPartialMock(
70  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
71  ['setOrderId', 'setShipmentId', 'setShipment', 'setTracking', 'load']
72  );
73  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParam']);
74  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
75  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
76  $this->sessionMock = $this->createPartialMock(\Magento\Backend\Model\Session::class, ['setIsUrlNotice']);
77  $this->actionFlag = $this->createPartialMock(\Magento\Framework\App\ActionFlag::class, ['get']);
78  $this->shipmentMock = $this->createPartialMock(\Magento\Sales\Model\Order\Shipment::class, ['__wakeup']);
79  $this->fileFactoryMock = $this->createPartialMock(
80  \Magento\Framework\App\Response\Http\FileFactory::class,
81  ['create']
82  );
83 
84  $contextMock = $this->createPartialMock(
85  \Magento\Backend\App\Action\Context::class,
86  ['getRequest', 'getObjectManager', 'getResponse', 'getSession', 'getActionFlag']
87  );
88 
89  $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
90  $contextMock->expects($this->any())
91  ->method('getObjectManager')
92  ->will($this->returnValue($this->objectManagerMock));
93  $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
94  $contextMock->expects($this->any())->method('getSession')->will($this->returnValue($this->sessionMock));
95  $contextMock->expects($this->any())->method('getActionFlag')->will($this->returnValue($this->actionFlag));
96 
97  $this->requestMock->expects($this->at(0))
98  ->method('getParam')
99  ->with('order_id')
100  ->will($this->returnValue($orderId));
101  $this->requestMock->expects($this->at(1))
102  ->method('getParam')
103  ->with('shipment_id')
104  ->will($this->returnValue($shipmentId));
105  $this->requestMock->expects($this->at(2))
106  ->method('getParam')
107  ->with('shipment')
108  ->will($this->returnValue($shipment));
109  $this->requestMock->expects($this->at(3))
110  ->method('getParam')
111  ->with('tracking')
112  ->will($this->returnValue($tracking));
113  $this->shipmentLoaderMock->expects($this->once())
114  ->method('setOrderId')
115  ->with($orderId);
116  $this->shipmentLoaderMock->expects($this->once())
117  ->method('setShipmentId')
118  ->with($shipmentId);
119  $this->shipmentLoaderMock->expects($this->once())
120  ->method('setShipment')
121  ->with($shipment);
122  $this->shipmentLoaderMock->expects($this->once())
123  ->method('setTracking')
124  ->with($tracking);
125 
126  $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\PrintPackage(
127  $contextMock,
128  $this->shipmentLoaderMock,
129  $this->fileFactoryMock
130  );
131  }
132 
136  public function testExecute()
137  {
138  $date = '9999-99-99_77-77-77';
139  $content = 'PDF content';
140 
141  $packagingMock = $this->createPartialMock(\Magento\Shipping\Model\Order\Pdf\Packaging::class, ['getPdf']);
142  $pdfMock = $this->createPartialMock(\Zend_Pdf::class, ['render']);
143  $dateTimeMock = $this->createPartialMock(\Magento\Framework\Stdlib\DateTime\DateTime::class, ['date']);
144 
145  $this->shipmentLoaderMock->expects($this->once())
146  ->method('load')
147  ->will($this->returnValue($this->shipmentMock));
148  $this->objectManagerMock->expects($this->once())
149  ->method('create')
150  ->with(\Magento\Shipping\Model\Order\Pdf\Packaging::class)
151  ->will($this->returnValue($packagingMock));
152  $packagingMock->expects($this->once())
153  ->method('getPdf')
154  ->with($this->shipmentMock)
155  ->will($this->returnValue($pdfMock));
156  $this->objectManagerMock->expects($this->once())
157  ->method('get')
158  ->with(\Magento\Framework\Stdlib\DateTime\DateTime::class)
159  ->will($this->returnValue($dateTimeMock));
160  $dateTimeMock->expects($this->once())->method('date')->with('Y-m-d_H-i-s')->will($this->returnValue($date));
161  $pdfMock->expects($this->once())->method('render')->will($this->returnValue($content));
162  $this->fileFactoryMock->expects($this->once())
163  ->method('create')
164  ->with(
165  'packingslip' . $date . '.pdf',
166  $content,
168  'application/pdf'
169  )->will($this->returnValue('result-pdf-content'));
170 
171  $this->assertEquals('result-pdf-content', $this->controller->execute());
172  }
173 
177  public function testExecuteFail()
178  {
179  $this->shipmentLoaderMock->expects($this->once())
180  ->method('load')
181  ->will($this->returnValue(false));
182  $this->shipmentLoaderMock->expects($this->once())
183  ->method('load')
184  ->will($this->returnValue(false));
185  $this->actionFlag->expects($this->once())
186  ->method('get')
187  ->with('', \Magento\Backend\App\AbstractAction::FLAG_IS_URLS_CHECKED)
188  ->will($this->returnValue(true));
189  $this->sessionMock->expects($this->once())
190  ->method('setIsUrlNotice')
191  ->with(true);
192 
193  $this->assertNull($this->controller->execute());
194  }
195 }
foreach($order->getItems() as $orderItem) $shipment