Skip to content

Commit

Permalink
[ECP-9489] Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Can Demiralp committed Jan 21, 2025
1 parent ab2c6ff commit 85b5b9a
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 32 deletions.
28 changes: 20 additions & 8 deletions Model/Api/AdyenDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!'));
Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -142,20 +151,23 @@ 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);
}

private function removeDonationToken(Order $order): void
{
$order->getPayment()->unsAdditionalInformation('donationToken');
$payment = $order->getPayment();
$payment->unsAdditionalInformation('donationToken');
$this->orderRepository->save($order);
}
}
243 changes: 219 additions & 24 deletions Test/Unit/Model/Api/AdyenDonationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,250 @@
*
* 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 <[email protected]>
*/

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);
}
}

0 comments on commit 85b5b9a

Please sign in to comment.