Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RemoveTrackTest.php
Go to the documentation of this file.
1 <?php
7 
13 class RemoveTrackTest extends \PHPUnit\Framework\TestCase
14 {
19 
23  protected $requestMock;
24 
28  protected $objectManagerMock;
29 
33  protected $shipmentTrackMock;
34 
38  protected $shipmentMock;
39 
43  protected $viewMock;
44 
48  protected $responseMock;
49 
53  protected $resultPageMock;
54 
58  protected $pageConfigMock;
59 
63  protected $pageTitleMock;
64 
68  protected $controller;
69 
70  protected function setUp()
71  {
72  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParam']);
73  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
74  $this->shipmentTrackMock = $this->createPartialMock(
75  \Magento\Sales\Model\Order\Shipment\Track::class,
76  ['load', 'getId', 'delete', '__wakeup']
77  );
78  $this->shipmentMock = $this->createPartialMock(
79  \Magento\Sales\Model\Order\Shipment::class,
80  ['getIncrementId', '__wakeup']
81  );
82  $this->viewMock = $this->createPartialMock(
83  \Magento\Framework\App\View::class,
84  ['loadLayout', 'getLayout', 'getPage']
85  );
86  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
87  $this->shipmentLoaderMock = $this->createPartialMock(
88  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
89  ['setOrderId', 'setShipmentId', 'setShipment', 'setTracking', 'load']
90  );
91  $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94  $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97  $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
98  ->disableOriginalConstructor()
99  ->getMock();
100 
101  $contextMock = $this->createPartialMock(
102  \Magento\Backend\App\Action\Context::class,
103  ['getRequest', 'getObjectManager', 'getTitle', 'getView', 'getResponse']
104  );
105 
106  $this->objectManagerMock->expects($this->once())
107  ->method('create')
108  ->with(\Magento\Sales\Model\Order\Shipment\Track::class)
109  ->will($this->returnValue($this->shipmentTrackMock));
110 
111  $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
112  $contextMock->expects($this->any())
113  ->method('getObjectManager')
114  ->will($this->returnValue($this->objectManagerMock));
115  $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
116  $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
117 
118  $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\RemoveTrack(
119  $contextMock,
120  $this->shipmentLoaderMock
121  );
122 
123  $this->viewMock->expects($this->any())
124  ->method('getPage')
125  ->willReturn($this->resultPageMock);
126  $this->resultPageMock->expects($this->any())
127  ->method('getConfig')
128  ->willReturn($this->pageConfigMock);
129  $this->pageConfigMock->expects($this->any())
130  ->method('getTitle')
131  ->willReturn($this->pageTitleMock);
132  }
133 
139  protected function shipmentLoad()
140  {
141  $orderId = 1;
142  $shipmentId = 1;
143  $trackId = 1;
144  $shipment = [];
145  $tracking = [];
146 
147  $this->shipmentTrackMock->expects($this->once())
148  ->method('load')
149  ->with($trackId)
150  ->will($this->returnSelf());
151  $this->shipmentTrackMock->expects($this->once())
152  ->method('getId')
153  ->will($this->returnValue($trackId));
154  $this->requestMock->expects($this->at(0))
155  ->method('getParam')
156  ->with('track_id')
157  ->will($this->returnValue($trackId));
158  $this->requestMock->expects($this->at(1))
159  ->method('getParam')
160  ->with('order_id')
161  ->will($this->returnValue($orderId));
162  $this->requestMock->expects($this->at(2))
163  ->method('getParam')
164  ->with('shipment_id')
165  ->will($this->returnValue($shipmentId));
166  $this->requestMock->expects($this->at(3))
167  ->method('getParam')
168  ->with('shipment')
169  ->will($this->returnValue($shipment));
170  $this->requestMock->expects($this->at(4))
171  ->method('getParam')
172  ->with('tracking')
173  ->will($this->returnValue($tracking));
174  $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId);
175  $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId);
176  $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment);
177  $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking);
178  }
179 
186  protected function representJson(array $errors)
187  {
188  $jsonHelper = $this->createPartialMock(\Magento\Framework\Json\Helper\Data::class, ['jsonEncode']);
189  $jsonHelper->expects($this->once())
190  ->method('jsonEncode')
191  ->with($errors)
192  ->will($this->returnValue('{json}'));
193  $this->objectManagerMock->expects($this->once())
194  ->method('get')
195  ->with(\Magento\Framework\Json\Helper\Data::class)
196  ->will($this->returnValue($jsonHelper));
197  $this->responseMock->expects($this->once())
198  ->method('representJson')
199  ->with('{json}');
200  }
201 
205  public function testExecute()
206  {
207  $response = 'html-data';
208  $this->shipmentLoad();
209 
210  $this->shipmentLoaderMock->expects($this->once())
211  ->method('load')
212  ->will($this->returnValue($this->shipmentMock));
213  $this->shipmentTrackMock->expects($this->once())
214  ->method('delete')
215  ->will($this->returnSelf());
216 
217  $layoutMock = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']);
218  $trackingBlockMock = $this->createPartialMock(
219  \Magento\Shipping\Block\Adminhtml\Order\Tracking::class,
220  ['toHtml']
221  );
222 
223  $trackingBlockMock->expects($this->once())
224  ->method('toHtml')
225  ->will($this->returnValue($response));
226  $layoutMock->expects($this->once())
227  ->method('getBlock')
228  ->with('shipment_tracking')
229  ->will($this->returnValue($trackingBlockMock));
230  $this->viewMock->expects($this->once())->method('loadLayout')->will($this->returnSelf());
231  $this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($layoutMock));
232  $this->responseMock->expects($this->once())
233  ->method('setBody')
234  ->with($response);
235 
236  $this->assertNull($this->controller->execute());
237  }
238 
242  public function testExecuteTrackIdFail()
243  {
244  $trackId = null;
245  $errors = ['error' => true, 'message' => 'We can\'t load track with retrieving identifier right now.'];
246 
247  $this->shipmentTrackMock->expects($this->once())
248  ->method('load')
249  ->with($trackId)
250  ->will($this->returnSelf());
251  $this->shipmentTrackMock->expects($this->once())
252  ->method('getId')
253  ->will($this->returnValue($trackId));
254  $this->representJson($errors);
255 
256  $this->assertNull($this->controller->execute());
257  }
258 
262  public function testExecuteShipmentLoadFail()
263  {
264  $errors = [
265  'error' => true,
266  'message' => 'We can\'t initialize shipment for delete tracking number.',
267  ];
268  $this->shipmentLoad();
269 
270  $this->shipmentLoaderMock->expects($this->once())
271  ->method('load')
272  ->will($this->returnValue(null));
273  $this->representJson($errors);
274 
275  $this->assertNull($this->controller->execute());
276  }
277 
281  public function testExecuteDeleteFail()
282  {
283  $errors = ['error' => true, 'message' => 'We can\'t delete tracking number.'];
284  $this->shipmentLoad();
285 
286  $this->shipmentLoaderMock->expects($this->once())
287  ->method('load')
288  ->will($this->returnValue($this->shipmentMock));
289  $this->shipmentTrackMock->expects($this->once())
290  ->method('delete')
291  ->will($this->throwException(new \Exception()));
292  $this->representJson($errors);
293 
294  $this->assertNull($this->controller->execute());
295  }
296 }
$response
Definition: 404.php:11
$jsonHelper
Definition: reset.phtml:14
foreach($order->getItems() as $orderItem) $shipment
$errors
Definition: overview.phtml:9