Skip to content

Commit

Permalink
Write unit tests for PaymentRequest and PaymentMethods classes (#2464)
Browse files Browse the repository at this point in the history
* Write unit tests for PaymentMethods class

* Write unit tests for PaymentRequest class

* Write unit tests for PaymentRequest class

* Add namespace to the class
  • Loading branch information
candemiralp authored Jan 26, 2024
1 parent 2567273 commit 206ea5f
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Test/Unit/Helper/PaymentMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2024 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Payment\Test\Unit\Helper;

use Adyen\Payment\Helper\PaymentMethods;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Payment\Model\MethodInterface;
use Magento\Sales\Model\Order;

class PaymentMethodsTest extends AbstractAdyenTestCase
{
private object $paymentMethodsHelper;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->paymentMethodsHelper = $objectManager->getObject(PaymentMethods::class, []);
}

/**
* @dataProvider comparePaymentMethodProvider
*/
public function testCompareOrderAndWebhookPaymentMethods(
$orderPaymentMethod,
$notificationPaymentMethod,
$assert,
$ccType = null
) {
$methodMock = $this->createMock(MethodInterface::class);
$methodMock->method('getConfigData')
->willReturnMap([
['group', null, PaymentMethods::ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS],
['is_wallet', null, '0']
]);
$methodMock->method('getCode')->willReturn($orderPaymentMethod);

$paymentMock = $this->createMock(Order\Payment::class);
$paymentMock->method('getMethodInstance')->willReturn($methodMock);
$paymentMock->method('getMethod')->willReturn($orderPaymentMethod);
$paymentMock->method('getCcType')->willReturn($ccType);

$orderMock = $this->createMock(Order::class);
$orderMock->method('getPayment')->willReturn($paymentMock);

$notificationMock = $this->createMock(Notification::class);
$notificationMock->method('getPaymentMethod')->willReturn($notificationPaymentMethod);

$this->assertEquals(
$assert,
$this->paymentMethodsHelper->compareOrderAndWebhookPaymentMethods($orderMock, $notificationMock)
);
}

public static function comparePaymentMethodProvider(): array
{
return [
[
'orderPaymentMethod' => 'adyen_klarna',
'notificationPaymentMethod' => 'klarna',
'assert' => true
],
[
'orderPaymentMethod' => 'adyen_cc',
'notificationPaymentMethod' => 'visa',
'assert' => true,
'ccType' => 'visa'
],
[
'orderPaymentMethod' => 'adyen_klarna',
'notificationPaymentMethod' => 'boleto',
'assert' => false
]
];
}
}
125 changes: 125 additions & 0 deletions Test/Unit/Helper/PaymentRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2024 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Payment\Test\Unit\Helper;

use Adyen\Client;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Model\Api\PaymentRequest;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Adyen\Service\Checkout;
use Adyen\Service\Recurring;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Payment;

class PaymentRequestTest extends AbstractAdyenTestCase
{
private $paymentRequest;
private $configHelper;
private $adyenHelper;

protected function setUp(): void
{
$this->configHelper = $this->createMock(Config::class);
$this->configHelper
->method('getAdyenAbstractConfigData')
->willReturn('MERCHANT_ACCOUNT_PLACEHOLDER');

$this->adyenHelper = $this->createMock(Data::class);
$this->adyenHelper->method('padShopperReference')->willReturn('001');


$objectManager = new ObjectManager($this);
$this->paymentRequest = $objectManager->getObject(PaymentRequest::class, [
'configHelper' => $this->configHelper,
'adyenHelper' => $this->adyenHelper
]);
}

public function testAuthorise3d()
{
$orderMock = $this->createMock(Order::class);
$orderMock->method('getStoreId')->willReturn(1);

$paymentMock = $this->createMock(Payment::class);
$paymentMock->method('getOrder')->willReturn($orderMock);

$checkoutServiceMock = $this->createMock(Checkout::class);
$checkoutServiceMock->method('paymentsDetails')->willReturn([]);

$this->adyenHelper
->method('initializeAdyenClient')
->willReturn($this->createMock(Client::class));

$this->adyenHelper->method('createAdyenCheckoutService')->willReturn($checkoutServiceMock);

$result = $this->paymentRequest->authorise3d($paymentMock);
$this->assertIsArray($result);
}

public function testListRecurringContractByType()
{
$recurringServiceMock = $this->createMock(Recurring::class);
$recurringServiceMock->method('listRecurringDetails')->willReturn([]);

$this->adyenHelper
->method('initializeAdyenClient')
->willReturn($this->createMock(Client::class));
$this->adyenHelper->method('createAdyenRecurringService')->willReturn($recurringServiceMock);

$this->assertIsArray($this->paymentRequest->listRecurringContractByType('001', 1, 'CardOnFile'));
}

/**
* @dataProvider disableRecurringContractProvider
*/
public function testDisableRecurringContract($response, $assert)
{
if (!$assert) {
$this->expectException(LocalizedException::class);
}

$result = [
'response' => $response
];

$recurringServiceMock = $this->createMock(Recurring::class);
$recurringServiceMock->method('disable')->willReturn($result);

$this->adyenHelper
->method('initializeAdyenClient')
->willReturn($this->createMock(Client::class));
$this->adyenHelper->method('createAdyenRecurringService')->willReturn($recurringServiceMock);

$apiResponse = $this->paymentRequest->disableRecurringContract('TOKEN_PLACEHOLDER', '001', 1);

if ($assert) {
$this->assertTrue($apiResponse);
}
}

public static function disableRecurringContractProvider(): array
{
return [
[
'response' => '[detail-successfully-disabled]',
'assert' => true
],
[
'response' => '[failed]',
'assert' => false
]
];
}
}

0 comments on commit 206ea5f

Please sign in to comment.