Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetShippingItemsGridTest.php
Go to the documentation of this file.
1 <?php
7 
11 class GetShippingItemsGridTest extends \PHPUnit\Framework\TestCase
12 {
17 
21  protected $requestMock;
22 
26  protected $responseMock;
27 
31  protected $viewMock;
32 
36  protected $controller;
37 
38  protected function setUp()
39  {
40  $this->requestMock = $this->createPartialMock(
41  \Magento\Framework\App\Request\Http::class,
42  ['getParam', '__wakeup']
43  );
44  $this->shipmentLoaderMock = $this->createPartialMock(
45  \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader::class,
46  ['setOrderId', 'setShipmentId', 'setShipment', 'setTracking', 'load', '__wakeup']
47  );
48  $this->viewMock = $this->createPartialMock(
49  \Magento\Framework\App\View::class,
50  ['getLayout', 'renderLayout', '__wakeup']
51  );
52  $this->responseMock = $this->createPartialMock(
53  \Magento\Framework\App\Response\Http::class,
54  ['setBody', '__wakeup']
55  );
56 
57  $contextMock = $this->createPartialMock(
58  \Magento\Backend\App\Action\Context::class,
59  ['getRequest', 'getResponse', 'getView', '__wakeup']
60  );
61 
62  $contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
63  $contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
64  $contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
65 
66  $this->controller = new \Magento\Shipping\Controller\Adminhtml\Order\Shipment\GetShippingItemsGrid(
67  $contextMock,
68  $this->shipmentLoaderMock
69  );
70  }
71 
75  public function testExecute()
76  {
77  $orderId = 1;
78  $shipmentId = 1;
79  $shipment = [];
80  $tracking = [];
81  $result = 'result-html';
82 
83  $layoutMock = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['createBlock']);
84  $gridMock = $this->createPartialMock(
85  \Magento\Shipping\Block\Adminhtml\Order\Packaging\Grid::class,
86  ['setIndex', 'toHtml']
87  );
88 
89  $this->requestMock->expects($this->at(0))
90  ->method('getParam')
91  ->with('order_id')
92  ->will($this->returnValue($orderId));
93  $this->requestMock->expects($this->at(1))
94  ->method('getParam')
95  ->with('shipment_id')
96  ->will($this->returnValue($shipmentId));
97  $this->requestMock->expects($this->at(2))
98  ->method('getParam')
99  ->with('shipment')
100  ->will($this->returnValue($shipment));
101  $this->requestMock->expects($this->at(3))
102  ->method('getParam')
103  ->with('tracking')
104  ->will($this->returnValue($tracking));
105  $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId);
106  $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId);
107  $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment);
108  $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking);
109  $this->shipmentLoaderMock->expects($this->once())->method('load');
110  $layoutMock->expects($this->once())
111  ->method('createBlock')
112  ->with(\Magento\Shipping\Block\Adminhtml\Order\Packaging\Grid::class)
113  ->will($this->returnValue($gridMock));
114  $this->viewMock->expects($this->once())
115  ->method('getLayout')
116  ->will($this->returnValue($layoutMock));
117  $this->responseMock->expects($this->once())
118  ->method('setBody')
119  ->with($result)
120  ->will($this->returnSelf());
121  $this->requestMock->expects($this->at(4))
122  ->method('getParam')
123  ->with('index');
124  $gridMock->expects($this->once())
125  ->method('setIndex')
126  ->will($this->returnSelf());
127  $gridMock->expects($this->once())
128  ->method('toHtml')
129  ->will($this->returnValue($result));
130 
131  $this->assertNotEmpty('result-html', $this->controller->execute());
132  }
133 }
foreach($order->getItems() as $orderItem) $shipment