@@ -63,88 +63,67 @@ class SaveOrderTest extends \PHPUnit_Framework_TestCase
63
63
*/
64
64
protected $ eventManagerMock ;
65
65
66
+ /**
67
+ * @var \Magento\Checkout\Model\Type\Onepage|\PHPUnit_Framework_MockObject_MockObject
68
+ */
69
+ protected $ onepageMock ;
70
+
71
+ /**
72
+ * @var \Magento\Checkout\Model\Agreements\AgreementsValidator|\PHPUnit_Framework_MockObject_MockObject
73
+ */
74
+ protected $ agreementsValidatorMock ;
75
+
76
+ /**
77
+ * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
78
+ */
79
+ protected $ quoteMock ;
80
+
66
81
/**
67
82
* Set up
68
83
*
69
84
* @return void
70
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
71
85
*/
72
86
protected function setUp ()
73
87
{
74
88
$ helper = new ObjectManager ($ this );
75
89
76
- $ contextMock = $ this ->getMock (
77
- 'Magento\Framework\App\Action\Context ' ,
78
- [],
79
- [],
80
- '' ,
81
- false
82
- );
83
-
84
- $ this ->requestMock = $ this ->getMockForAbstractClass (
85
- 'Magento\Framework\App\RequestInterface ' ,
86
- [],
87
- '' ,
88
- false ,
89
- true ,
90
- true ,
91
- ['getPost ' ]
92
- );
93
- $ this ->responseMock = $ this ->getMockForAbstractClass (
94
- 'Magento\Framework\App\ResponseInterface ' ,
95
- [],
96
- '' ,
97
- false ,
98
- true ,
99
- true ,
100
- []
101
- );
102
- $ this ->objectManagerMock = $ this ->getMockForAbstractClass (
103
- 'Magento\Framework\ObjectManagerInterface ' ,
104
- [],
105
- '' ,
106
- false ,
107
- true ,
108
- true ,
109
- []
110
- );
111
- $ this ->formKeyValidatorMock = $ this ->getMock (
112
- 'Magento\Framework\Data\Form\FormKey\Validator ' ,
113
- [],
114
- [],
115
- '' ,
116
- false
117
- );
118
- $ this ->resultRedirectFactoryMock = $ this ->getMock (
119
- 'Magento\Framework\Controller\Result\RedirectFactory ' ,
120
- [],
121
- [],
122
- '' ,
123
- false
124
- );
125
- $ this ->resultRawFactoryMock = $ this ->getMock (
126
- 'Magento\Framework\Controller\Result\RawFactory ' ,
127
- [],
128
- [],
129
- '' ,
130
- false
131
- );
132
- $ this ->resultJsonFactoryMock = $ this ->getMock (
133
- 'Magento\Framework\Controller\Result\JsonFactory ' ,
134
- [],
135
- [],
136
- '' ,
137
- false
138
- );
139
- $ this ->eventManagerMock = $ this ->getMockForAbstractClass (
140
- 'Magento\Framework\Event\ManagerInterface ' ,
141
- [],
142
- '' ,
143
- false ,
144
- true ,
145
- true ,
146
- []
147
- );
90
+ $ contextMock = $ this ->getMockBuilder ('Magento\Framework\App\Action\Context ' )
91
+ ->disableOriginalConstructor ()
92
+ ->getMock ();
93
+ $ this ->requestMock = $ this ->getMockBuilder ('Magento\Framework\App\RequestInterface ' )
94
+ ->disableOriginalConstructor ()
95
+ ->setMethods (['getPost ' ])
96
+ ->getMockForAbstractClass ();
97
+ $ this ->responseMock = $ this ->getMockBuilder ('Magento\Framework\App\ResponseInterface ' )
98
+ ->disableOriginalConstructor ()
99
+ ->getMockForAbstractClass ();
100
+ $ this ->objectManagerMock = $ this ->getMockBuilder ('Magento\Framework\ObjectManagerInterface ' )
101
+ ->disableOriginalConstructor ()
102
+ ->getMockForAbstractClass ();
103
+ $ this ->formKeyValidatorMock = $ this ->getMockBuilder ('Magento\Framework\Data\Form\FormKey\Validator ' )
104
+ ->disableOriginalConstructor ()
105
+ ->getMock ();
106
+ $ this ->resultRedirectFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\RedirectFactory ' )
107
+ ->disableOriginalConstructor ()
108
+ ->getMock ();
109
+ $ this ->resultRawFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\RawFactory ' )
110
+ ->disableOriginalConstructor ()
111
+ ->getMock ();
112
+ $ this ->resultJsonFactoryMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\JsonFactory ' )
113
+ ->disableOriginalConstructor ()
114
+ ->getMock ();
115
+ $ this ->eventManagerMock = $ this ->getMockBuilder ('Magento\Framework\Event\ManagerInterface ' )
116
+ ->disableOriginalConstructor ()
117
+ ->getMockForAbstractClass ();
118
+ $ this ->onepageMock = $ this ->getMockBuilder ('Magento\Checkout\Model\Type\Onepage ' )
119
+ ->disableOriginalConstructor ()
120
+ ->getMock ();
121
+ $ this ->agreementsValidatorMock = $ this ->getMockBuilder ('Magento\Checkout\Model\Agreements\AgreementsValidator ' )
122
+ ->disableOriginalConstructor ()
123
+ ->getMock ();
124
+ $ this ->quoteMock = $ this ->getMockBuilder ('Magento\Quote\Model\Quote ' )
125
+ ->disableOriginalConstructor ()
126
+ ->getMock ();
148
127
149
128
$ contextMock ->expects ($ this ->once ())
150
129
->method ('getRequest ' )
@@ -174,73 +153,53 @@ protected function setUp()
174
153
}
175
154
176
155
/**
177
- * Test for execute method
156
+ * Test method execution _expireAjax (call hasItems === false)
178
157
*
179
158
* @return void
180
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
181
159
*/
182
- public function testExecute ()
160
+ protected function expireAjaxFlowHasItemsFalse ()
183
161
{
184
- $ data = [
185
- 'payment-key-1 ' => 'payment-value-1 ' ,
186
- 'checks ' => [
187
- \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_CHECKOUT ,
188
- \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_FOR_COUNTRY ,
189
- \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_FOR_CURRENCY ,
190
- \Magento \Payment \Model \Method \AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX ,
191
- \Magento \Payment \Model \Method \AbstractMethod::CHECK_ZERO_TOTAL ,
192
- ]
193
- ];
162
+ $ this ->onepageMock ->expects ($ this ->atLeastOnce ())
163
+ ->method ('getQuote ' )
164
+ ->willReturn ($ this ->quoteMock );
194
165
195
- $ onepageMock = $ this ->getMock (
196
- 'Magento\Checkout\Model\Type\Onepage ' ,
197
- [],
198
- [],
199
- '' ,
200
- false
201
- );
202
- $ redirectMock = $ this ->getMock (
203
- 'Magento\Framework\Controller\Result\Redirect ' ,
204
- [],
205
- [],
206
- '' ,
207
- false
208
- );
209
- $ quoteMock = $ this ->getMock (
210
- 'Magento\Quote\Model\Quote ' ,
211
- [],
212
- [],
213
- '' ,
214
- false
215
- );
216
- $ agreementsValidatorMock = $ this ->getMock (
217
- 'Magento\Checkout\Model\Agreements\AgreementsValidator ' ,
218
- [],
219
- [],
220
- '' ,
221
- false
222
- );
223
- $ paymentMock = $ this ->getMock (
224
- 'Magento\Quote\Model\Quote\Payment ' ,
225
- [],
226
- [],
227
- '' ,
228
- false
229
- );
230
- $ checkoutMock = $ this ->getMock (
231
- 'Magento\Checkout\Model\Session ' ,
232
- ['getRedirectUrl ' ],
233
- [],
234
- '' ,
235
- false
236
- );
237
- $ resultJsonMock = $ this ->getMock (
238
- 'Magento\Framework\Controller\Result\Json ' ,
239
- [],
240
- [],
241
- '' ,
242
- false
243
- );
166
+ $ this ->quoteMock ->expects ($ this ->once ())
167
+ ->method ('hasItems ' )
168
+ ->willReturn (false );
169
+ $ this ->quoteMock ->expects ($ this ->never ())
170
+ ->method ('getHasError ' )
171
+ ->willReturn (true );
172
+ $ this ->quoteMock ->expects ($ this ->never ())
173
+ ->method ('validateMinimumAmount ' )
174
+ ->willReturn (false );
175
+
176
+ $ this ->requestMock ->expects ($ this ->never ())
177
+ ->method ('getActionName ' );
178
+ }
179
+
180
+ /**
181
+ * Test for execute method
182
+ *
183
+ * @return void
184
+ */
185
+ public function testExecuteWithSuccessOrderSave ()
186
+ {
187
+ $ testData = $ this ->getExecuteWithSuccessOrderSaveTestData ();
188
+
189
+
190
+ $ redirectMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Redirect ' )
191
+ ->disableOriginalConstructor ()
192
+ ->getMock ();
193
+ $ paymentMock = $ this ->getMockBuilder ('Magento\Quote\Model\Quote\Payment ' )
194
+ ->disableOriginalConstructor ()
195
+ ->getMock ();
196
+ $ checkoutMock = $ this ->getMockBuilder ('Magento\Checkout\Model\Session ' )
197
+ ->disableOriginalConstructor ()
198
+ ->setMethods (['getRedirectUrl ' ])
199
+ ->getMock ();
200
+ $ resultJsonMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Json ' )
201
+ ->disableOriginalConstructor ()
202
+ ->getMock ();
244
203
245
204
$ redirectMock ->expects ($ this ->never ())
246
205
->method ('setPath ' )
@@ -258,70 +217,34 @@ public function testExecute()
258
217
259
218
$ this ->objectManagerMock ->expects ($ this ->atLeastOnce ())
260
219
->method ('get ' )
261
- ->willReturnMap (
262
- [
263
- ['Magento\Checkout\Model\Type\Onepage ' , $ onepageMock ],
264
- ['Magento\Checkout\Model\Agreements\AgreementsValidator ' , $ agreementsValidatorMock ],
265
- ]
266
- );
220
+ ->willReturnMap ($ testData ['objectManager.get ' ]);
267
221
268
222
// call _expireAjax method
269
- $ onepageMock ->expects ($ this ->atLeastOnce ())
270
- ->method ('getQuote ' )
271
- ->willReturn ($ quoteMock );
272
-
273
- $ quoteMock ->expects ($ this ->once ())
274
- ->method ('hasItems ' )
275
- ->willReturn (false );
276
- $ quoteMock ->expects ($ this ->never ())
277
- ->method ('getHasError ' )
278
- ->willReturn (true );
279
- $ quoteMock ->expects ($ this ->never ())
280
- ->method ('validateMinimumAmount ' )
281
- ->willReturn (false );
282
-
283
- $ this ->requestMock ->expects ($ this ->never ())
284
- ->method ('getActionName ' );
285
- // -- end
223
+ $ this ->expireAjaxFlowHasItemsFalse ();
286
224
287
225
$ this ->requestMock ->expects ($ this ->atLeastOnce ())
288
226
->method ('getPost ' )
289
- ->willReturnMap (
290
- [
291
- [
292
- 'agreement ' ,
293
- [],
294
- [
295
- 'test-key-1 ' => 'test-value-1 '
296
- ]
297
- ],
298
- [
299
- 'payment ' ,
300
- [],
301
- $ data
302
- ],
303
- ]
304
- );
227
+ ->willReturnMap ($ testData ['request.getPost ' ]);
305
228
306
- $ agreementsValidatorMock ->expects ($ this ->once ())
229
+ $ this -> agreementsValidatorMock ->expects ($ this ->once ())
307
230
->method ('isValid ' )
308
- ->with ([ ' test-key-1 ' ])
231
+ ->with ($ testData [ ' agreementsValidator.isValid ' ])
309
232
->willReturn (true );
310
233
311
- $ quoteMock ->expects ($ this ->atLeastOnce ())
234
+ $ this -> quoteMock ->expects ($ this ->atLeastOnce ())
312
235
->method ('getPayment ' )
313
236
->willReturn ($ paymentMock );
314
237
315
238
$ paymentMock ->expects ($ this ->once ())
316
239
->method ('setQuote ' )
317
- ->with ($ quoteMock );
240
+ ->with ($ this -> quoteMock );
318
241
$ paymentMock ->expects ($ this ->once ())
319
242
->method ('importData ' )
320
- ->with ($ data );
243
+ ->with ($ testData [ ' payment.importData ' ] );
321
244
322
- $ onepageMock ->expects ($ this ->once ())
245
+ $ this -> onepageMock ->expects ($ this ->once ())
323
246
->method ('saveOrder ' );
324
- $ onepageMock ->expects ($ this ->once ())
247
+ $ this -> onepageMock ->expects ($ this ->once ())
325
248
->method ('getCheckout ' )
326
249
->willReturn ($ checkoutMock );
327
250
@@ -342,9 +265,56 @@ public function testExecute()
342
265
343
266
$ resultJsonMock ->expects ($ this ->once ())
344
267
->method ('setData ' )
345
- ->with ([ ' success ' => 1 , ' error ' => false ])
268
+ ->with ($ testData [ ' resultJson.setData ' ])
346
269
->willReturnSelf ();
347
270
348
271
$ this ->assertEquals ($ resultJsonMock , $ this ->controller ->execute ());
349
272
}
273
+
274
+ /**
275
+ * Get data for test testExecuteWithSuccessOrderSave
276
+ *
277
+ * @return array
278
+ */
279
+ protected function getExecuteWithSuccessOrderSaveTestData ()
280
+ {
281
+ $ data = [
282
+ 'payment-key-1 ' => 'payment-value-1 ' ,
283
+ 'checks ' => [
284
+ \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_CHECKOUT ,
285
+ \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_FOR_COUNTRY ,
286
+ \Magento \Payment \Model \Method \AbstractMethod::CHECK_USE_FOR_CURRENCY ,
287
+ \Magento \Payment \Model \Method \AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX ,
288
+ \Magento \Payment \Model \Method \AbstractMethod::CHECK_ZERO_TOTAL ,
289
+ ]
290
+ ];
291
+ $ testKey = 'test-key-1 ' ;
292
+
293
+ return [
294
+ 'resultJson.setData ' => [
295
+ 'success ' => 1 ,
296
+ 'error ' => false
297
+ ],
298
+ 'request.getPost ' => [
299
+ [
300
+ 'agreement ' ,
301
+ [],
302
+ [
303
+ $ testKey => 'test-value-1 '
304
+ ]
305
+ ],
306
+ [
307
+ 'payment ' ,
308
+ [],
309
+ $ data
310
+ ],
311
+ ],
312
+ 'payment.importData ' => $ data ,
313
+ 'agreementsValidator.isValid ' => [$ testKey ],
314
+ 'objectManager.get ' => [
315
+ ['Magento\Checkout\Model\Type\Onepage ' , $ this ->onepageMock ],
316
+ ['Magento\Checkout\Model\Agreements\AgreementsValidator ' , $ this ->agreementsValidatorMock ],
317
+ ]
318
+ ];
319
+ }
350
320
}
0 commit comments