From 85b5b9a3c059e1cd07bb63c6b97b0825bd7cff3f Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Tue, 21 Jan 2025 11:14:34 +0100 Subject: [PATCH] [ECP-9489] Update unit tests --- Model/Api/AdyenDonations.php | 28 ++- Test/Unit/Model/Api/AdyenDonationsTest.php | 243 +++++++++++++++++++-- 2 files changed, 239 insertions(+), 32 deletions(-) diff --git a/Model/Api/AdyenDonations.php b/Model/Api/AdyenDonations.php index 8c21aa914..99e0cdb9e 100644 --- a/Model/Api/AdyenDonations.php +++ b/Model/Api/AdyenDonations.php @@ -12,6 +12,7 @@ namespace Adyen\Payment\Model\Api; +use Adyen\AdyenException; use Adyen\Payment\Api\AdyenDonationsInterface; use Adyen\Payment\Helper\ChargedCurrency; use Adyen\Payment\Helper\Config; @@ -70,12 +71,20 @@ public function donate(int $orderId, string $payload): void $this->makeDonation($payload, $order); } + /** + * @param string $payload + * @param OrderInterface $order + * @return void + * @throws AdyenException + * @throws LocalizedException + */ public function makeDonation(string $payload, OrderInterface $order): void { $payload = $this->jsonSerializer->unserialize($payload); - $paymentMethodInstance = $order->getPayment()->getMethodInstance(); - $donationToken = $order->getPayment()->getAdditionalInformation('donationToken'); + $payment = $order->getPayment(); + $paymentMethodInstance = $payment->getMethodInstance(); + $donationToken = $payment->getAdditionalInformation('donationToken'); if (!$donationToken) { throw new LocalizedException(__('Donation failed!')); @@ -99,10 +108,10 @@ function ($amount) use ($formatter, $currencyCode) { } $payload['donationToken'] = $donationToken; - $payload['donationOriginalPspReference'] = $order->getPayment()->getAdditionalInformation('pspReference'); + $payload['donationOriginalPspReference'] = $payment->getAdditionalInformation('pspReference'); // Override payment method object with payment method code - if ($order->getPayment()->getMethod() === AdyenCcConfigProvider::CODE) { + if ($payment->getMethod() === AdyenCcConfigProvider::CODE) { $payload['paymentMethod'] = 'scheme'; } elseif ($this->paymentMethodsHelper->isAlternativePaymentMethod($paymentMethodInstance)) { $payload['paymentMethod'] = $this->paymentMethodsHelper->getAlternativePaymentMethodTxVariant( @@ -128,7 +137,7 @@ function ($amount) use ($formatter, $currencyCode) { $this->removeDonationToken($order); } catch (LocalizedException $e) { - $this->donationTryCount = $order->getPayment()->getAdditionalInformation('donationTryCount'); + $this->donationTryCount = $payment->getAdditionalInformation('donationTryCount'); if ($this->donationTryCount >= 5) { // Remove donation token after 5 try and throw a exception. @@ -142,12 +151,14 @@ function ($amount) use ($formatter, $currencyCode) { private function incrementTryCount(Order $order): void { + $payment = $order->getPayment(); + if (!$this->donationTryCount) { - $order->getPayment()->setAdditionalInformation('donationTryCount', 1); + $payment->setAdditionalInformation('donationTryCount', 1); } else { $this->donationTryCount += 1; - $order->getPayment()->setAdditionalInformation('donationTryCount', $this->donationTryCount); + $payment->setAdditionalInformation('donationTryCount', $this->donationTryCount); } $this->orderRepository->save($order); @@ -155,7 +166,8 @@ private function incrementTryCount(Order $order): void private function removeDonationToken(Order $order): void { - $order->getPayment()->unsAdditionalInformation('donationToken'); + $payment = $order->getPayment(); + $payment->unsAdditionalInformation('donationToken'); $this->orderRepository->save($order); } } diff --git a/Test/Unit/Model/Api/AdyenDonationsTest.php b/Test/Unit/Model/Api/AdyenDonationsTest.php index a019fe3a4..b87d37b09 100644 --- a/Test/Unit/Model/Api/AdyenDonationsTest.php +++ b/Test/Unit/Model/Api/AdyenDonationsTest.php @@ -3,7 +3,7 @@ * * Adyen Payment module (https://www.adyen.com/) * - * Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/) + * Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) * See LICENSE.txt for license details. * * Author: Adyen @@ -11,47 +11,242 @@ namespace Adyen\Payment\Test\Unit\Model\Api; +use Adyen\AdyenException; use Adyen\Payment\Helper\ChargedCurrency; use Adyen\Payment\Helper\Config; use Adyen\Payment\Helper\Data; use Adyen\Payment\Helper\PaymentMethods; +use Adyen\Payment\Model\AdyenAmountCurrency; use Adyen\Payment\Model\Api\AdyenDonations; use Adyen\Payment\Model\Sales\OrderRepository; use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Framework\Serialize\Serializer\Json; use Magento\Payment\Gateway\Command\CommandPoolInterface; -use Magento\Sales\Api\Data\OrderInterface; +use Magento\Payment\Gateway\CommandInterface; +use Magento\Payment\Model\MethodInterface; +use Magento\Sales\Model\Order; +use PHPUnit\Framework\MockObject\MockObject; +use Magento\Framework\Serialize\Serializer\Json; class AdyenDonationsTest extends AbstractAdyenTestCase { + private ?AdyenDonations $adyenDonations; + private CommandPoolInterface|MockObject $commandPoolMock; + private Json|MockObject $jsonMock; + private Data|MockObject $dataMock; + private Config|MockObject $configMock; + private ChargedCurrency|MockObject $chargedCurrencyMock; + private PaymentMethods|MockObject $paymentMethodsMock; + private OrderRepository|MockObject $orderRepositoryMock; + + protected function setUp(): void + { + $this->commandPoolMock = $this->createMock(CommandPoolInterface::class); + $this->jsonMock = $this->createPartialMock(Json::class, []); + $this->dataMock = $this->createPartialMock(Data::class, []); + $this->chargedCurrencyMock = $this->createMock(ChargedCurrency::class); + $this->configMock = $this->createMock(Config::class); + $this->paymentMethodsMock = $this->createPartialMock(PaymentMethods::class, []); + $this->orderRepositoryMock = $this->createMock(OrderRepository::class); + + $this->adyenDonations = new AdyenDonations( + $this->commandPoolMock, + $this->jsonMock, + $this->dataMock, + $this->chargedCurrencyMock, + $this->configMock, + $this->paymentMethodsMock, + $this->orderRepositoryMock + ); + } + + protected function tearDown(): void + { + $this->adyenDonations = null; + } + + private static function paymentMethodDataProvider(): array + { + return [ + [ + 'paymentMethod' => 'adyen_cc', + 'paymentMethodGroup' => 'adyen', + 'customerId' => 1, + 'executeSuccess' => true, + 'donationTryCount' => 0 + ], + [ + 'paymentMethod' => 'adyen_ideal', + 'paymentMethodGroup' => 'adyen-alternative-payment-method', + 'customerId' => null, + 'executeSuccess' => true, + 'donationTryCount' => 0 + ], + [ + 'paymentMethod' => 'adyen_cc', + 'paymentMethodGroup' => 'adyen', + 'customerId' => null, + 'executeSuccess' => false, + 'donationTryCount' => 0 + ], + [ + 'paymentMethod' => 'adyen_cc', + 'paymentMethodGroup' => 'adyen', + 'customerId' => null, + 'executeSuccess' => false, + 'donationTryCount' => 5 + ], + ]; + } + /** - * @throws NoSuchEntityException - * @throws LocalizedException + * @dataProvider paymentMethodDataProvider + * @param $paymentMethod + * @param $paymentMethodGroup + * @param $customerId + * @param $executeSuccess + * @param $donationTryCount + * @return void * @throws InputException + * @throws LocalizedException + * @throws NoSuchEntityException */ - public function testDonate() + public function testDonate($paymentMethod, $paymentMethodGroup, $customerId, $executeSuccess, $donationTryCount) { - $orderRepositoryMock = $this->createPartialMock(OrderRepository::class, ['get']); - $orderRepositoryMock->expects(self::atLeastOnce()) + $orderId = 1; + $payload = '{"amount":{"value":1000,"currency":"EUR"}}'; + $donationTokenMock = 'mock_token_abcde'; + $orderCurrency = 'EUR'; + $storeId = 1; + $donationAmounts = '1,5,10'; + $pspReference = 'xyz_12345'; + $orderIncrementId = '00000000001'; + + $paymentMethodInstanceMock = $this->createMock(MethodInterface::class); + $paymentMethodInstanceMock->method('getConfigData') + ->with('group') + ->willReturn($paymentMethodGroup); + + $paymentMock = $this->createMock(Order\Payment::class); + $paymentMock->expects($this->once()) + ->method('getMethodInstance') + ->willReturn($paymentMethodInstanceMock); + $paymentMock->expects($this->once()) + ->method('getMethod') + ->willReturn($paymentMethod); + $paymentMock->method('getAdditionalInformation') + ->willReturnMap([ + ['donationToken', $donationTokenMock], + ['pspReference', $pspReference], + ['donationTryCount', $donationTryCount] + ]); + + $orderMock = $this->createMock(Order::class); + $orderMock->method('getPayment')->willReturn($paymentMock); + $orderMock->method('getStoreId')->willReturn($storeId); + $orderMock->method('getCustomerId')->willReturn($customerId); + $orderMock->method('getIncrementId')->willReturn($orderIncrementId); + + $this->orderRepositoryMock->expects($this->once()) + ->method('get') + ->with($orderId) + ->willReturn($orderMock); + $this->orderRepositoryMock->method('save')->with($orderMock); + + $orderAmountCurrencyMock = $this->createMock(AdyenAmountCurrency::class); + $orderAmountCurrencyMock->method('getCurrencyCode')->willReturn($orderCurrency); + + $this->chargedCurrencyMock->expects($this->once()) + ->method('getOrderAmountCurrency') + ->with($orderMock, false) + ->willReturn($orderAmountCurrencyMock); + + $this->configMock->expects($this->once()) + ->method('getAdyenGivingDonationAmounts') + ->with($storeId) + ->willReturn($donationAmounts); + + $donationCommand = $this->createMock(CommandInterface::class); + + if ($executeSuccess) { + $paymentMock->expects($this->once()) + ->method('unsAdditionalInformation') + ->with('donationToken'); + + $donationCommand->expects($this->once())->method('execute'); + } else { + $this->expectException(LocalizedException::class); + + $donationCommand->method('execute')->willThrowException( + new LocalizedException(__('exception')) + ); + } + + $this->commandPoolMock->expects($this->once()) ->method('get') - ->willReturn($this->createMock(OrderInterface::class)); - - $adyenDonationsMock = $this->getMockBuilder(AdyenDonations::class) - ->setMethods(['makeDonation']) - ->setConstructorArgs([ - $this->createMock(CommandPoolInterface::class), - $this->createMock(Json::class), - $this->createMock(Data::class), - $this->createMock(ChargedCurrency::class), - $this->createMock(Config::class), - $this->createMock(PaymentMethods::class), - $orderRepositoryMock - ]) - ->getMock(); - - $adyenDonationsMock->donate(1, ''); + ->with('capture') + ->willReturn($donationCommand); + + $this->adyenDonations->donate($orderId, $payload); + } + + /** + * @return void + * @throws LocalizedException + * @throws AdyenException + */ + public function testNullDonationToken() + { + $this->expectException(LocalizedException::class); + + $payload = '{"amount":{"value":1000,"currency":"EUR"}}'; + $donationTokenMock = null; + + $paymentMock = $this->createMock(Order\Payment::class); + $paymentMock->method('getAdditionalInformation') + ->willReturnMap([ + ['donationToken', $donationTokenMock] + ]); + + $orderMock = $this->createMock(Order::class); + $orderMock->expects($this->once())->method('getPayment')->willReturn($paymentMock); + + // Assert LocalizedException + $this->adyenDonations->makeDonation($payload, $orderMock); + } + + /** + * @return void + * @throws AdyenException + * @throws LocalizedException + */ + public function testCurrencyMismatch() + { + $this->expectException(LocalizedException::class); + + $payload = '{"amount":{"value":1000,"currency":"EUR"}}'; + $donationTokenMock = 'mock_token_abcde'; + $orderCurrency = 'TRY'; + + $paymentMock = $this->createMock(Order\Payment::class); + $paymentMock->method('getAdditionalInformation') + ->willReturnMap([ + ['donationToken', $donationTokenMock] + ]); + $orderMock = $this->createMock(Order::class); + $orderMock->expects($this->once())->method('getPayment')->willReturn($paymentMock); + + $orderAmountCurrencyMock = $this->createMock(AdyenAmountCurrency::class); + $orderAmountCurrencyMock->method('getCurrencyCode')->willReturn($orderCurrency); + + $this->chargedCurrencyMock->expects($this->once()) + ->method('getOrderAmountCurrency') + ->with($orderMock, false) + ->willReturn($orderAmountCurrencyMock); + + // Assert LocalizedException + $this->adyenDonations->makeDonation($payload, $orderMock); } }