Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateLabelTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CreateLabelTest extends \PHPUnit\Framework\TestCase
12 {
17 
21  protected $shipmentMock;
22 
26  protected $requestMock;
27 
31  protected $responseMock;
32 
36  protected $objectManagerMock;
37 
42 
46  protected $labelGenerator;
47 
51  protected $controller;
52 
53  protected function setUp()
54  {
55  $this->shipmentLoaderMock = $this->createPartialMock(
56  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
57  ['setOrderId', 'setShipmentId', 'setShipment', 'setTracking', 'load', '__wakeup']
58  );
59  $this->shipmentMock = $this->createPartialMock(
60  \Magento\Sales\Model\Order\Shipment::class,
61  ['__wakeup', 'save']
62  );
63  $this->requestMock = $this->createPartialMock(
64  \Magento\Framework\App\Request\Http::class,
65  ['getParam', '__wakeup']
66  );
67  $this->responseMock = $this->createPartialMock(
68  \Magento\Framework\App\Response\Http::class,
69  ['representJson', '__wakeup']
70  );
71  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
72  $this->messageManagerMock = $this->createPartialMock(
73  \Magento\Framework\Message\Manager::class,
74  ['addSuccess', 'addError', '__wakeup']
75  );
76  $this->labelGenerator = $this->createPartialMock(
77  \Magento\Shipping\Model\Shipping\LabelGenerator::class,
78  ['create', '__wakeup']
79  );
80 
81  $contextMock = $this->createPartialMock(
82  \Magento\Backend\App\Action\Context::class,
83  ['getRequest', 'getResponse', 'getMessageManager', 'getActionFlag', 'getObjectManager', '__wakeup']
84  );
85 
86  $this->loadShipment();
87  $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
88  $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
89  $contextMock->expects($this->any())
90  ->method('getObjectManager')
91  ->will($this->returnValue($this->objectManagerMock));
92  $contextMock->expects($this->any())
93  ->method('getMessageManager')
94  ->will($this->returnValue($this->messageManagerMock));
95 
96  $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\CreateLabel(
97  $contextMock,
98  $this->shipmentLoaderMock,
99  $this->labelGenerator
100  );
101  }
102 
108  protected function loadShipment()
109  {
110  $orderId = 1;
111  $shipmentId = 1;
112  $shipment = [];
113  $tracking = [];
114 
115  $this->requestMock->expects($this->at(0))
116  ->method('getParam')
117  ->with('order_id')
118  ->will($this->returnValue($orderId));
119  $this->requestMock->expects($this->at(1))
120  ->method('getParam')
121  ->with('shipment_id')
122  ->will($this->returnValue($shipmentId));
123  $this->requestMock->expects($this->at(2))
124  ->method('getParam')
125  ->with('shipment')
126  ->will($this->returnValue($shipment));
127  $this->requestMock->expects($this->at(3))
128  ->method('getParam')
129  ->with('tracking')
130  ->will($this->returnValue($tracking));
131  $this->shipmentLoaderMock->expects($this->once())
132  ->method('setOrderId')
133  ->with($orderId);
134  $this->shipmentLoaderMock->expects($this->once())
135  ->method('setShipmentId')
136  ->with($shipmentId);
137  $this->shipmentLoaderMock->expects($this->once())
138  ->method('setShipment')
139  ->with($shipment);
140  $this->shipmentLoaderMock->expects($this->once())
141  ->method('setTracking')
142  ->with($tracking);
143  }
144 
148  public function testExecute()
149  {
150  $this->shipmentLoaderMock->expects($this->once())
151  ->method('load')
152  ->will($this->returnValue($this->shipmentMock));
153  $this->labelGenerator->expects($this->once())
154  ->method('create')
155  ->with($this->shipmentMock, $this->requestMock)
156  ->will($this->returnValue(true));
157  $this->shipmentMock->expects($this->once())->method('save')->will($this->returnSelf());
158  $this->messageManagerMock->expects($this->once())->method('addSuccess');
159  $this->responseMock->expects($this->once())->method('representJson');
160 
161  $this->assertNull($this->controller->execute());
162  }
163 
167  public function testExecuteLoadException()
168  {
169  $this->shipmentLoaderMock->expects($this->once())
170  ->method('load')
171  ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message')));
172  $this->responseMock->expects($this->once())->method('representJson');
173 
174  $this->assertNull($this->controller->execute());
175  }
176 
180  public function testExecuteSaveException()
181  {
182  $loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
183 
184  $this->shipmentLoaderMock->expects($this->once())
185  ->method('load')
186  ->will($this->returnValue($this->shipmentMock));
187  $this->labelGenerator->expects($this->once())
188  ->method('create')
189  ->with($this->shipmentMock, $this->requestMock)
190  ->will($this->returnValue(true));
191  $this->shipmentMock->expects($this->once())->method('save')->will($this->throwException(new \Exception()));
192  $loggerMock->expects($this->once())->method('critical');
193  $this->objectManagerMock->expects($this->once())
194  ->method('get')
195  ->with(\Psr\Log\LoggerInterface::class)
196  ->will($this->returnValue($loggerMock));
197  $this->responseMock->expects($this->once())->method('representJson');
198 
199  $this->assertNull($this->controller->execute());
200  }
201 
206  {
207  $this->shipmentLoaderMock->expects($this->once())
208  ->method('load')
209  ->will($this->returnValue($this->shipmentMock));
210  $this->labelGenerator->expects($this->once())
211  ->method('create')
212  ->with($this->shipmentMock, $this->requestMock)
213  ->willThrowException(
214  new \Magento\Framework\Exception\LocalizedException(__('message'))
215  );
216  $this->responseMock->expects($this->once())->method('representJson');
217 
218  $this->assertNull($this->controller->execute());
219  }
220 }
__()
Definition: __.php:13
foreach($order->getItems() as $orderItem) $shipment