|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Adyen Payment module (https://www.adyen.com/) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) |
| 7 | + * See LICENSE.txt for license details. |
| 8 | + * |
| 9 | + * Author: Adyen <magento@adyen.com> |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Adyen\Payment\Test\Plugin; |
| 13 | + |
| 14 | +use Adyen\Payment\Helper\PaymentMethods; |
| 15 | +use Adyen\Payment\Logger\AdyenLogger; |
| 16 | +use Adyen\Payment\Plugin\GuestPaymentInformationResetOrderId; |
| 17 | +use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; |
| 18 | +use Magento\Checkout\Api\GuestPaymentInformationManagementInterface; |
| 19 | +use Magento\Framework\Exception\NotFoundException; |
| 20 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 21 | +use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; |
| 22 | +use Magento\Quote\Model\Quote; |
| 23 | + |
| 24 | +class GuestPaymentInformationResetOrderIdTest extends AbstractAdyenTestCase |
| 25 | +{ |
| 26 | + protected ?GuestPaymentInformationResetOrderId $guestPaymentInformationResetOrderId; |
| 27 | + protected CartRepositoryInterface $quoteRepositoryMock; |
| 28 | + protected PaymentMethods $paymentMethodsHelperMock; |
| 29 | + protected AdyenLogger $adyenLoggerMock; |
| 30 | + protected MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteIdMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class); |
| 38 | + $this->paymentMethodsHelperMock = $this->createMock(PaymentMethods::class); |
| 39 | + $this->adyenLoggerMock = $this->createMock(AdyenLogger::class); |
| 40 | + $this->maskedQuoteIdToQuoteIdMock = $this->createMock(MaskedQuoteIdToQuoteIdInterface::class); |
| 41 | + |
| 42 | + $this->guestPaymentInformationResetOrderId = new GuestPaymentInformationResetOrderId( |
| 43 | + $this->quoteRepositoryMock, |
| 44 | + $this->paymentMethodsHelperMock, |
| 45 | + $this->adyenLoggerMock, |
| 46 | + $this->maskedQuoteIdToQuoteIdMock |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + protected function tearDown(): void |
| 54 | + { |
| 55 | + $this->guestPaymentInformationResetOrderId = null; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function testBeforeSavePaymentInformationAndPlaceOrder() |
| 62 | + { |
| 63 | + $cartId = 'abc123456789abcde'; |
| 64 | + $quoteId = 1; |
| 65 | + $method = 'adyen_ideal'; |
| 66 | + |
| 67 | + $this->maskedQuoteIdToQuoteIdMock->expects($this->once()) |
| 68 | + ->method('execute') |
| 69 | + ->with($cartId) |
| 70 | + ->willReturn($quoteId); |
| 71 | + |
| 72 | + $paymentMock = $this->createMock(Quote\Payment::class); |
| 73 | + $paymentMock->expects($this->once()) |
| 74 | + ->method('getMethod') |
| 75 | + ->willReturn($method); |
| 76 | + |
| 77 | + $quoteMock = $this->createMock(Quote::class); |
| 78 | + $quoteMock->expects($this->once()) |
| 79 | + ->method('getPayment') |
| 80 | + ->willReturn($paymentMock); |
| 81 | + |
| 82 | + $this->quoteRepositoryMock->expects($this->once()) |
| 83 | + ->method('get') |
| 84 | + ->with($quoteId) |
| 85 | + ->willReturn($quoteMock); |
| 86 | + |
| 87 | + $this->paymentMethodsHelperMock->expects($this->once()) |
| 88 | + ->method('isAdyenPayment') |
| 89 | + ->with($method) |
| 90 | + ->willReturn(true); |
| 91 | + |
| 92 | + $quoteMock->expects($this->once()) |
| 93 | + ->method('setReservedOrderId') |
| 94 | + ->with(null); |
| 95 | + |
| 96 | + $mockArgument = $this->createMock(GuestPaymentInformationManagementInterface::class); |
| 97 | + |
| 98 | + $result = $this->guestPaymentInformationResetOrderId |
| 99 | + ->beforeSavePaymentInformationAndPlaceOrder($mockArgument, $cartId); |
| 100 | + |
| 101 | + $this->assertNull($result); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + public function testBeforeSavePaymentInformationAndPlaceOrderNonAdyenMethod() |
| 108 | + { |
| 109 | + $cartId = 'abc123456789abcde'; |
| 110 | + $quoteId = 1; |
| 111 | + $method = 'non_adyen'; |
| 112 | + |
| 113 | + $this->maskedQuoteIdToQuoteIdMock->expects($this->once()) |
| 114 | + ->method('execute') |
| 115 | + ->with($cartId) |
| 116 | + ->willReturn($quoteId); |
| 117 | + |
| 118 | + $paymentMock = $this->createMock(Quote\Payment::class); |
| 119 | + $paymentMock->expects($this->once()) |
| 120 | + ->method('getMethod') |
| 121 | + ->willReturn($method); |
| 122 | + |
| 123 | + $quoteMock = $this->createMock(Quote::class); |
| 124 | + $quoteMock->expects($this->once()) |
| 125 | + ->method('getPayment') |
| 126 | + ->willReturn($paymentMock); |
| 127 | + |
| 128 | + $this->quoteRepositoryMock->expects($this->once()) |
| 129 | + ->method('get') |
| 130 | + ->with($quoteId) |
| 131 | + ->willReturn($quoteMock); |
| 132 | + |
| 133 | + $this->paymentMethodsHelperMock->expects($this->once()) |
| 134 | + ->method('isAdyenPayment') |
| 135 | + ->with($method) |
| 136 | + ->willReturn(false); |
| 137 | + |
| 138 | + $quoteMock->expects($this->never())->method('setReservedOrderId'); |
| 139 | + |
| 140 | + $mockArgument = $this->createMock(GuestPaymentInformationManagementInterface::class); |
| 141 | + $this->guestPaymentInformationResetOrderId->beforeSavePaymentInformationAndPlaceOrder($mockArgument, $cartId); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function testBeforeSavePaymentInformationAndPlaceOrderException() |
| 148 | + { |
| 149 | + $cartId = 'abc123456789abcde'; |
| 150 | + |
| 151 | + $this->maskedQuoteIdToQuoteIdMock->expects($this->once()) |
| 152 | + ->method('execute') |
| 153 | + ->with($cartId) |
| 154 | + ->willThrowException(new NotFoundException(__('Not found'))); |
| 155 | + |
| 156 | + $this->adyenLoggerMock->expects($this->once())->method('error'); |
| 157 | + |
| 158 | + $mockArgument = $this->createMock(GuestPaymentInformationManagementInterface::class); |
| 159 | + $result = $this->guestPaymentInformationResetOrderId |
| 160 | + ->beforeSavePaymentInformationAndPlaceOrder($mockArgument, $cartId); |
| 161 | + |
| 162 | + $this->assertNull($result); |
| 163 | + } |
| 164 | +} |
0 commit comments