6
6
7
7
namespace CM \Payments \Test \Integration \Service ;
8
8
9
+ use CM \Payments \Api \Model \Data \PaymentInterfaceFactory ;
9
10
use CM \Payments \Api \Model \PaymentRepositoryInterface ;
10
11
use CM \Payments \Client \Api \ApiClientInterface ;
11
12
use CM \Payments \Api \Service \OrderTransactionServiceInterface ;
12
13
use CM \Payments \Model \Data \Order ;
14
+ use CM \Payments \Model \OrderRepository ;
13
15
use CM \Payments \Service \OrderTransactionService ;
14
16
use CM \Payments \Test \Integration \IntegrationTestCase ;
15
17
use CM \Payments \Test \Mock \MockApiResponse ;
@@ -55,13 +57,7 @@ protected function setUp(): void
55
57
56
58
$ magentoOrder = $ this ->loadOrderById ('100000001 ' );
57
59
58
- $ cmOrder = $ this ->objectManager ->create (Order::class);
59
- $ cmOrder ->setIncrementId ('100000001 ' )
60
- ->setOrderId ($ magentoOrder ->getEntityId ())
61
- ->setOrderKey ('test123 ' );
62
-
63
- $ cmOrderRepository = $ this ->objectManager ->create (\CM \Payments \Model \OrderRepository::class);
64
- $ cmOrderRepository ->save ($ cmOrder );
60
+ $ this ->createCmOrder ($ magentoOrder );
65
61
66
62
$ magentoOrder = $ this ->loadOrderById ('100000001 ' );
67
63
$ this ->adjustMagentoOrder ($ magentoOrder );
@@ -212,11 +208,100 @@ public function testIfCMPaymentIsCreatedInDatabase()
212
208
$ this ->orderTransactionService ->process ('100000001 ' );
213
209
214
210
$ cmPaymentRepository = $ this ->objectManager ->create (PaymentRepositoryInterface::class);
215
-
216
211
$ cmPayment = $ cmPaymentRepository ->getByOrderKey ('test123 ' );
217
212
$ this ->assertSame ('pid4911203603t ' , $ cmPayment ->getPaymentId ());
218
213
}
219
214
215
+ /**
216
+ * @magentoDataFixture Magento/Sales/_files/order.php
217
+ */
218
+ public function testShouldCancelOrderWhenCreditCardDirectPaymentIsCanceled ()
219
+ {
220
+ $ this ->clientMock
221
+ ->expects ($ this ->once ())->method ('execute ' )
222
+ ->willReturn ($ this ->mockApiResponse ->getOrderDetailCanceledPayment ());
223
+
224
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
225
+ $ magentoOrder ->getPayment ()->setMethod ('cm_payments_creditcard ' );
226
+ $ repository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
227
+ $ repository ->save ($ magentoOrder );
228
+
229
+ $ this ->createCmOrder ($ magentoOrder );
230
+
231
+ $ this ->createCMPayment ($ magentoOrder );
232
+
233
+ $ this ->orderTransactionService ->process ('100000001 ' );
234
+
235
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
236
+ $ this ->assertSame ('canceled ' , $ magentoOrder ->getStatus ());
237
+ }
238
+
239
+ /**
240
+ * @magentoDataFixture Magento/Sales/_files/order.php
241
+ */
242
+ public function testShouldNotCancelOrderWhenCreditCardDirectPaymentIsRedirectedForAuthentication ()
243
+ {
244
+ $ this ->clientMock
245
+ ->expects ($ this ->once ())->method ('execute ' )
246
+ ->willReturn ($ this ->mockApiResponse ->getOrderDetailRedirectedForAuthenticationPayment ());
247
+
248
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
249
+ $ magentoOrder ->getPayment ()->setMethod ('cm_payments_creditcard ' );
250
+ $ repository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
251
+ $ repository ->save ($ magentoOrder );
252
+
253
+ $ this ->createCmOrder ($ magentoOrder );
254
+ $ this ->createCMPayment ($ magentoOrder );
255
+
256
+ $ this ->orderTransactionService ->process ('100000001 ' );
257
+
258
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
259
+ $ this ->assertSame ('pending_payment ' , $ magentoOrder ->getStatus ());
260
+ }
261
+
262
+ /**
263
+ * @magentoDataFixture Magento/Sales/_files/order.php
264
+ */
265
+ public function testShouldNotCancelOrderWhenIdealPaymentIsCanceled ()
266
+ {
267
+ $ this ->clientMock
268
+ ->expects ($ this ->once ())->method ('execute ' )
269
+ ->willReturn ($ this ->mockApiResponse ->getOrderDetailCanceledPayment ());
270
+
271
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
272
+
273
+ $ magentoOrder ->getPayment ()->setMethod ('cm_payments_ideal ' );
274
+ $ repository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
275
+ $ repository ->save ($ magentoOrder );
276
+
277
+ $ this ->createCmOrder ($ magentoOrder );
278
+ $ this ->orderTransactionService ->process ('100000001 ' );
279
+
280
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
281
+ $ this ->assertSame ('pending_payment ' , $ magentoOrder ->getStatus ());
282
+ }
283
+
284
+ /**
285
+ * @magentoDataFixture Magento/Sales/_files/order.php
286
+ */
287
+ public function testShouldNotCancelOrderWhenCMPaymentNotExists ()
288
+ {
289
+ $ this ->clientMock
290
+ ->expects ($ this ->once ())->method ('execute ' )
291
+ ->willReturn ($ this ->mockApiResponse ->getOrderDetailCanceledPayment ());
292
+
293
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
294
+ $ magentoOrder ->getPayment ()->setMethod ('cm_payments_ideal ' );
295
+ $ repository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
296
+ $ repository ->save ($ magentoOrder );
297
+
298
+ $ this ->createCmOrder ($ magentoOrder );
299
+ $ this ->orderTransactionService ->process ('100000001 ' );
300
+
301
+ $ magentoOrder = $ this ->loadOrderById ('100000001 ' );
302
+ $ this ->assertSame ('pending_payment ' , $ magentoOrder ->getStatus ());
303
+ }
304
+
220
305
/**
221
306
* @param OrderInterface $magentoOrder
222
307
* @return OrderInterface
@@ -234,4 +319,33 @@ private function adjustMagentoOrder(OrderInterface $magentoOrder): OrderInterfac
234
319
235
320
return $ magentoOrder ;
236
321
}
322
+
323
+ /**
324
+ * @param OrderInterface $magentoOrder
325
+ */
326
+ private function createCmOrder (OrderInterface $ magentoOrder ): void
327
+ {
328
+ $ cmOrder = $ this ->objectManager ->create (Order::class);
329
+ $ cmOrder ->setIncrementId ('100000001 ' )
330
+ ->setOrderId ($ magentoOrder ->getEntityId ())
331
+ ->setOrderKey ('test123 ' );
332
+
333
+ $ cmOrderRepository = $ this ->objectManager ->create (OrderRepository::class);
334
+ $ cmOrderRepository ->save ($ cmOrder );
335
+ }
336
+
337
+ /**
338
+ * @param OrderInterface $magentoOrder
339
+ */
340
+ private function createCMPayment (OrderInterface $ magentoOrder ): void
341
+ {
342
+ $ cmPaymentRepository = $ this ->objectManager ->create (PaymentRepositoryInterface::class);
343
+ $ cmPaymentDataFactory = $ this ->objectManager ->create (PaymentInterfaceFactory::class);
344
+ $ cmPayment = $ cmPaymentDataFactory ->create ();
345
+ $ cmPayment ->setOrderId ((int )$ magentoOrder ->getEntityId ());
346
+ $ cmPayment ->setOrderKey ('test123 ' );
347
+ $ cmPayment ->setIncrementId ($ magentoOrder ->getIncrementId ());
348
+ $ cmPayment ->setPaymentId ('pid4911203603t ' );
349
+ $ cmPaymentRepository ->save ($ cmPayment );
350
+ }
237
351
}
0 commit comments