From 206ea5f256b122ce56dc4ecc9a3b2592cf281327 Mon Sep 17 00:00:00 2001 From: Can Demiralp Date: Fri, 26 Jan 2024 12:09:41 +0100 Subject: [PATCH] Write unit tests for PaymentRequest and PaymentMethods classes (#2464) * Write unit tests for PaymentMethods class * Write unit tests for PaymentRequest class * Write unit tests for PaymentRequest class * Add namespace to the class --- Test/Unit/Helper/PaymentMethodsTest.php | 86 ++++++++++++++++ Test/Unit/Helper/PaymentRequestTest.php | 125 ++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 Test/Unit/Helper/PaymentMethodsTest.php create mode 100644 Test/Unit/Helper/PaymentRequestTest.php diff --git a/Test/Unit/Helper/PaymentMethodsTest.php b/Test/Unit/Helper/PaymentMethodsTest.php new file mode 100644 index 000000000..18f05a5c3 --- /dev/null +++ b/Test/Unit/Helper/PaymentMethodsTest.php @@ -0,0 +1,86 @@ + + */ + +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 + ] + ]; + } +} diff --git a/Test/Unit/Helper/PaymentRequestTest.php b/Test/Unit/Helper/PaymentRequestTest.php new file mode 100644 index 000000000..dbc965fdc --- /dev/null +++ b/Test/Unit/Helper/PaymentRequestTest.php @@ -0,0 +1,125 @@ + + */ + +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 + ] + ]; + } +}