Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddTrackTest.php
Go to the documentation of this file.
1 <?php
8 
11 
18 class AddTrackTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $shipmentLoader;
24 
28  protected $controller;
29 
33  protected $context;
34 
38  protected $request;
39 
43  protected $response;
44 
48  protected $objectManager;
49 
53  protected $view;
54 
58  protected $resultPageMock;
59 
63  protected $pageConfigMock;
64 
68  protected $pageTitleMock;
69 
70  protected function setUp()
71  {
72  $objectManagerHelper = new ObjectManagerHelper($this);
73  $this->shipmentLoader = $this->getMockBuilder(
74  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class
75  )
76  ->disableOriginalConstructor()
77  ->setMethods(['setShipmentId', 'setOrderId', 'setShipment', 'setTracking', 'load'])
78  ->getMock();
79  $this->context = $this->createPartialMock(
80  \Magento\Backend\App\Action\Context::class,
81  [
82  'getRequest',
83  'getResponse',
84  'getRedirect',
85  'getObjectManager',
86  'getTitle',
87  'getView'
88  ]
89  );
90  $this->response = $this->createPartialMock(
91  \Magento\Framework\App\ResponseInterface::class,
92  ['setRedirect', 'sendResponse', 'setBody']
93  );
94  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
95  ->disableOriginalConstructor()->getMock();
96  $this->objectManager = $this->createPartialMock(
97  \Magento\Framework\ObjectManager\ObjectManager::class,
98  ['create', 'get']
99  );
100  $this->view = $this->createMock(\Magento\Framework\App\ViewInterface::class);
101  $this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104  $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
105  ->disableOriginalConstructor()
106  ->getMock();
107  $this->pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110  $this->context->expects($this->once())
111  ->method('getRequest')
112  ->will($this->returnValue($this->request));
113  $this->context->expects($this->once())
114  ->method('getResponse')
115  ->will($this->returnValue($this->response));
116  $this->context->expects($this->once())
117  ->method('getObjectManager')
118  ->will($this->returnValue($this->objectManager));
119  $this->context->expects($this->once())
120  ->method('getView')
121  ->will($this->returnValue($this->view));
122  $this->controller = $objectManagerHelper->getObject(
123  \Magento\Shipping\Controller\Adminhtml\Order\Shipment\AddTrack::class,
124  [
125  'context' => $this->context,
126  'shipmentLoader' => $this->shipmentLoader,
127  'request' => $this->request,
128  'response' => $this->response,
129  'view' => $this->view
130  ]
131  );
132  }
133 
137  public function testExecute()
138  {
139  $carrier = 'carrier';
140  $number = 'number';
141  $title = 'title';
142  $shipmentId = 1000012;
143  $orderId = 10003;
144  $tracking = [];
145  $shipmentData = ['items' => [], 'send_email' => ''];
146  $shipment = $this->createPartialMock(
147  \Magento\Sales\Model\Order\Shipment::class,
148  ['addTrack', '__wakeup', 'save']
149  );
150  $this->request->expects($this->any())
151  ->method('getParam')
152  ->will(
153  $this->returnValueMap(
154  [
155  ['order_id', null, $orderId], ['shipment_id', null, $shipmentId],
156  ['shipment', null, $shipmentData], ['tracking', null, $tracking],
157  ]
158  )
159  );
160  $this->request->expects($this->any())
161  ->method('getPost')
162  ->will(
163  $this->returnValueMap(
164  [
165  ['carrier', null, $carrier],
166  ['number', null, $number],
167  ['title', null, $title],
168  ]
169  )
170  );
171  $this->shipmentLoader->expects($this->any())
172  ->method('setShipmentId')
173  ->with($shipmentId);
174  $this->shipmentLoader->expects($this->any())
175  ->method('setOrderId')
176  ->with($orderId);
177  $this->shipmentLoader->expects($this->any())
178  ->method('setShipment')
179  ->with($shipmentData);
180  $this->shipmentLoader->expects($this->any())
181  ->method('setTracking')
182  ->with($tracking);
183  $this->shipmentLoader->expects($this->once())
184  ->method('load')
185  ->will($this->returnValue($shipment));
186  $track = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment\Track::class)
187  ->disableOriginalConstructor()
188  ->setMethods(['__wakeup', 'setNumber', 'setCarrierCode', 'setTitle'])
189  ->getMock();
190  $this->objectManager->expects($this->atLeastOnce())
191  ->method('create')
192  ->with(\Magento\Sales\Model\Order\Shipment\Track::class)
193  ->will($this->returnValue($track));
194  $track->expects($this->once())
195  ->method('setNumber')
196  ->with($number)
197  ->will($this->returnSelf());
198  $track->expects($this->once())
199  ->method('setCarrierCode')
200  ->with($carrier)
201  ->will($this->returnSelf());
202  $track->expects($this->once())
203  ->method('setTitle')
204  ->with($title)
205  ->will($this->returnSelf());
206  $this->view->expects($this->once())
207  ->method('loadLayout')
208  ->will($this->returnSelf());
209  $layout = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
210  $menuBlock = $this->createPartialMock(\Magento\Framework\View\Element\BlockInterface::class, ['toHtml']);
211  $html = 'html string';
212  $this->view->expects($this->once())
213  ->method('getLayout')
214  ->will($this->returnValue($layout));
215  $layout->expects($this->once())
216  ->method('getBlock')
217  ->with('shipment_tracking')
218  ->will($this->returnValue($menuBlock));
219  $menuBlock->expects($this->once())
220  ->method('toHtml')
221  ->will($this->returnValue($html));
222  $shipment->expects($this->once())
223  ->method('addTrack')
224  ->with($this->equalTo($track))
225  ->will($this->returnSelf());
226  $shipment->expects($this->any())
227  ->method('save')
228  ->will($this->returnSelf());
229  $this->view->expects($this->any())
230  ->method('getPage')
231  ->willReturn($this->resultPageMock);
232  $this->resultPageMock->expects($this->any())
233  ->method('getConfig')
234  ->willReturn($this->pageConfigMock);
235  $this->pageConfigMock->expects($this->any())
236  ->method('getTitle')
237  ->willReturn($this->pageTitleMock);
238  $this->response->expects($this->once())
239  ->method('setBody')
240  ->with($html);
241  $this->assertNull($this->controller->execute());
242  }
243 }
$title
Definition: default.phtml:14
$track
Definition: details.phtml:12
$number
Definition: details.phtml:22
foreach($order->getItems() as $orderItem) $shipment