6
6
7
7
namespace Magento \Shipping \Test \Unit \Controller \Adminhtml \Order \Shipment ;
8
8
9
- use Magento \Backend \App \Action ;
9
+ use Magento \Backend \App \Action \Context ;
10
+ use Magento \Framework \App \Request \Http ;
11
+ use Magento \Framework \App \ResponseInterface ;
12
+ use Magento \Framework \App \ViewInterface ;
13
+ use Magento \Framework \Controller \ResultFactory ;
14
+ use Magento \Framework \Controller \ResultInterface ;
10
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
16
+ use Magento \Framework \View \Element \BlockInterface ;
17
+ use Magento \Framework \View \LayoutInterface ;
18
+ use Magento \Framework \View \Page \Config ;
19
+ use Magento \Framework \View \Page \Title ;
20
+ use Magento \Framework \View \Result \Page ;
21
+ use Magento \Sales \Api \Data \ShipmentTrackInterfaceFactory ;
22
+ use Magento \Sales \Model \Order \Shipment ;
23
+ use Magento \Sales \Model \Order \Shipment \Track ;
24
+ use Magento \Shipping \Controller \Adminhtml \Order \Shipment \AddTrack ;
25
+ use Magento \Shipping \Controller \Adminhtml \Order \ShipmentLoader ;
11
26
12
27
/**
13
- * Class AddTrackTest
28
+ * Class AddTrackTest covers AddTrack controller.
14
29
*
15
- * @package Magento\Shipping\Controller\Adminhtml\Order\Shipment
16
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
31
*/
18
32
class AddTrackTest extends \PHPUnit \Framework \TestCase
19
33
{
20
34
/**
21
- * @var \Magento\Shipping\Controller\Adminhtml\Order\ ShipmentLoader|\PHPUnit_Framework_MockObject_MockObject
35
+ * @var ShipmentLoader|\PHPUnit_Framework_MockObject_MockObject
22
36
*/
23
- protected $ shipmentLoader ;
37
+ private $ shipmentLoader ;
24
38
25
39
/**
26
- * @var \Magento\Shipping\Controller\Adminhtml\Order\Shipment\ AddTrack
40
+ * @var AddTrack
27
41
*/
28
- protected $ controller ;
42
+ private $ controller ;
29
43
30
44
/**
31
- * @var Action\ Context|\PHPUnit_Framework_MockObject_MockObject
45
+ * @var Context|\PHPUnit_Framework_MockObject_MockObject
32
46
*/
33
- protected $ context ;
47
+ private $ context ;
34
48
35
49
/**
36
- * @var \Magento\Framework\App\Request\ Http|\PHPUnit_Framework_MockObject_MockObject
50
+ * @var Http|\PHPUnit_Framework_MockObject_MockObject
37
51
*/
38
- protected $ request ;
52
+ private $ request ;
39
53
40
54
/**
41
- * @var \Magento\Framework\App\ ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
55
+ * @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
42
56
*/
43
- protected $ response ;
57
+ private $ response ;
44
58
45
59
/**
46
- * @var \Magento\Framework\ObjectManager\ObjectManager |\PHPUnit_Framework_MockObject_MockObject
60
+ * @var ViewInterface |\PHPUnit_Framework_MockObject_MockObject
47
61
*/
48
- protected $ objectManager ;
62
+ private $ view ;
49
63
50
64
/**
51
- * @var \Magento\Framework\App\ViewInterface |\PHPUnit_Framework_MockObject_MockObject
65
+ * @var Page |\PHPUnit_Framework_MockObject_MockObject
52
66
*/
53
- protected $ view ;
67
+ private $ resultPageMock ;
54
68
55
69
/**
56
- * @var \Magento\Framework\View\Result\Page |\PHPUnit_Framework_MockObject_MockObject
70
+ * @var Config |\PHPUnit_Framework_MockObject_MockObject
57
71
*/
58
- protected $ resultPageMock ;
72
+ private $ pageConfigMock ;
59
73
60
74
/**
61
- * @var \Magento\Framework\View\Page\Config |\PHPUnit_Framework_MockObject_MockObject
75
+ * @var Title |\PHPUnit_Framework_MockObject_MockObject
62
76
*/
63
- protected $ pageConfigMock ;
77
+ private $ pageTitleMock ;
64
78
65
79
/**
66
- * @var \Magento\Framework\View\Page\Title |\PHPUnit_Framework_MockObject_MockObject
80
+ * @var ShipmentTrackInterfaceFactory |\PHPUnit_Framework_MockObject_MockObject
67
81
*/
68
- protected $ pageTitleMock ;
82
+ private $ trackFactory ;
69
83
84
+ /**
85
+ * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject
86
+ */
87
+ private $ rawResult ;
88
+
89
+ /**
90
+ * @inheritdoc
91
+ */
70
92
protected function setUp ()
71
93
{
72
94
$ objectManagerHelper = new ObjectManagerHelper ($ this );
73
95
$ this ->shipmentLoader = $ this ->getMockBuilder (
74
- \ Magento \ Shipping \ Controller \ Adminhtml \ Order \ ShipmentLoader::class
96
+ ShipmentLoader::class
75
97
)
76
98
->disableOriginalConstructor ()
77
99
->setMethods (['setShipmentId ' , 'setOrderId ' , 'setShipment ' , 'setTracking ' , 'load ' ])
78
100
->getMock ();
79
101
$ this ->context = $ this ->createPartialMock (
80
- \ Magento \ Backend \ App \ Action \ Context::class,
102
+ Context::class,
81
103
[
82
104
'getRequest ' ,
83
105
'getResponse ' ,
84
106
'getRedirect ' ,
85
107
'getObjectManager ' ,
86
108
'getTitle ' ,
87
- 'getView '
109
+ 'getView ' ,
110
+ 'getResultFactory '
88
111
]
89
112
);
90
113
$ this ->response = $ this ->createPartialMock (
91
- \ Magento \ Framework \ App \ ResponseInterface::class,
114
+ ResponseInterface::class,
92
115
['setRedirect ' , 'sendResponse ' , 'setBody ' ]
93
116
);
94
- $ this ->request = $ this ->getMockBuilder (\ Magento \ Framework \ App \ Request \ Http::class)
117
+ $ this ->request = $ this ->getMockBuilder (Http::class)
95
118
->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)
119
+ $ this ->view = $ this ->createMock (ViewInterface::class);
120
+ $ this ->resultPageMock = $ this ->getMockBuilder (Page::class)
102
121
->disableOriginalConstructor ()
103
122
->getMock ();
104
- $ this ->pageConfigMock = $ this ->getMockBuilder (\ Magento \ Framework \ View \ Page \ Config::class)
123
+ $ this ->pageConfigMock = $ this ->getMockBuilder (Config::class)
105
124
->disableOriginalConstructor ()
106
125
->getMock ();
107
- $ this ->pageTitleMock = $ this ->getMockBuilder (\ Magento \ Framework \ View \ Page \ Title::class)
126
+ $ this ->pageTitleMock = $ this ->getMockBuilder (Title::class)
108
127
->disableOriginalConstructor ()
109
128
->getMock ();
129
+ $ this ->trackFactory = $ this ->getMockBuilder (ShipmentTrackInterfaceFactory::class)
130
+ ->disableOriginalConstructor ()
131
+ ->setMethods (['create ' ])
132
+ ->getMockForAbstractClass ();
133
+ $ this ->rawResult = $ this ->getMockBuilder (ResultInterface::class)
134
+ ->disableOriginalConstructor ()
135
+ ->setMethods (['setContents ' ])
136
+ ->getMockForAbstractClass ();
137
+ $ resultFactory = $ this ->getMockBuilder (ResultFactory::class)
138
+ ->disableOriginalConstructor ()
139
+ ->setMethods (['create ' ])
140
+ ->getMockForAbstractClass ();
110
141
$ this ->context ->expects ($ this ->once ())
111
142
->method ('getRequest ' )
112
143
->will ($ this ->returnValue ($ this ->request ));
113
144
$ this ->context ->expects ($ this ->once ())
114
145
->method ('getResponse ' )
115
146
->will ($ this ->returnValue ($ this ->response ));
116
- $ this ->context ->expects ($ this ->once ())
117
- ->method ('getObjectManager ' )
118
- ->will ($ this ->returnValue ($ this ->objectManager ));
119
147
$ this ->context ->expects ($ this ->once ())
120
148
->method ('getView ' )
121
149
->will ($ this ->returnValue ($ this ->view ));
150
+ $ resultFactory ->expects ($ this ->once ())
151
+ ->method ('create ' )
152
+ ->willReturn ($ this ->rawResult );
153
+ $ this ->context ->expects ($ this ->once ())
154
+ ->method ('getResultFactory ' )
155
+ ->willReturn ($ resultFactory );
122
156
$ this ->controller = $ objectManagerHelper ->getObject (
123
- \ Magento \ Shipping \ Controller \ Adminhtml \ Order \ Shipment \ AddTrack::class,
157
+ AddTrack::class,
124
158
[
125
159
'context ' => $ this ->context ,
126
160
'shipmentLoader ' => $ this ->shipmentLoader ,
127
161
'request ' => $ this ->request ,
128
162
'response ' => $ this ->response ,
129
- 'view ' => $ this ->view
163
+ 'view ' => $ this ->view ,
164
+ 'trackFactory ' => $ this ->trackFactory ,
130
165
]
131
166
);
132
167
}
@@ -144,16 +179,18 @@ public function testExecute()
144
179
$ tracking = [];
145
180
$ shipmentData = ['items ' => [], 'send_email ' => '' ];
146
181
$ shipment = $ this ->createPartialMock (
147
- \ Magento \ Sales \ Model \ Order \ Shipment::class,
182
+ Shipment::class,
148
183
['addTrack ' , '__wakeup ' , 'save ' ]
149
184
);
150
185
$ this ->request ->expects ($ this ->any ())
151
186
->method ('getParam ' )
152
187
->will (
153
188
$ this ->returnValueMap (
154
189
[
155
- ['order_id ' , null , $ orderId ], ['shipment_id ' , null , $ shipmentId ],
156
- ['shipment ' , null , $ shipmentData ], ['tracking ' , null , $ tracking ],
190
+ ['order_id ' , null , $ orderId ],
191
+ ['shipment_id ' , null , $ shipmentId ],
192
+ ['shipment ' , null , $ shipmentData ],
193
+ ['tracking ' , null , $ tracking ],
157
194
]
158
195
)
159
196
);
@@ -183,14 +220,13 @@ public function testExecute()
183
220
$ this ->shipmentLoader ->expects ($ this ->once ())
184
221
->method ('load ' )
185
222
->will ($ this ->returnValue ($ shipment ));
186
- $ track = $ this ->getMockBuilder (\ Magento \ Sales \ Model \ Order \ Shipment \ Track::class)
223
+ $ track = $ this ->getMockBuilder (Track::class)
187
224
->disableOriginalConstructor ()
188
225
->setMethods (['__wakeup ' , 'setNumber ' , 'setCarrierCode ' , 'setTitle ' ])
189
226
->getMock ();
190
- $ this ->objectManager ->expects ($ this ->atLeastOnce ())
227
+ $ this ->trackFactory ->expects ($ this ->once ())
191
228
->method ('create ' )
192
- ->with (\Magento \Sales \Model \Order \Shipment \Track::class)
193
- ->will ($ this ->returnValue ($ track ));
229
+ ->willReturn ($ track );
194
230
$ track ->expects ($ this ->once ())
195
231
->method ('setNumber ' )
196
232
->with ($ number )
@@ -206,8 +242,8 @@ public function testExecute()
206
242
$ this ->view ->expects ($ this ->once ())
207
243
->method ('loadLayout ' )
208
244
->will ($ this ->returnSelf ());
209
- $ layout = $ this ->createMock (\ Magento \ Framework \ View \ LayoutInterface::class);
210
- $ menuBlock = $ this ->createPartialMock (\ Magento \ Framework \ View \ Element \ BlockInterface::class, ['toHtml ' ]);
245
+ $ layout = $ this ->createMock (LayoutInterface::class);
246
+ $ menuBlock = $ this ->createPartialMock (BlockInterface::class, ['toHtml ' ]);
211
247
$ html = 'html string ' ;
212
248
$ this ->view ->expects ($ this ->once ())
213
249
->method ('getLayout ' )
@@ -235,9 +271,10 @@ public function testExecute()
235
271
$ this ->pageConfigMock ->expects ($ this ->any ())
236
272
->method ('getTitle ' )
237
273
->willReturn ($ this ->pageTitleMock );
238
- $ this ->response ->expects ($ this ->once ())
239
- ->method ('setBody ' )
240
- ->with ($ html );
241
- $ this ->assertNull ($ this ->controller ->execute ());
274
+ $ this ->rawResult ->expects ($ this ->once ())
275
+ ->method ('setContents ' )
276
+ ->with ($ html )
277
+ ->willReturnSelf ();
278
+ $ this ->assertInstanceOf (ResultInterface::class, $ this ->controller ->execute ());
242
279
}
243
280
}
0 commit comments