Skip to content

Commit

Permalink
added unit tests for PaymentDetails helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RokPopov committed Jan 18, 2024
1 parent 0a7ed4d commit b880b7c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
1 change: 0 additions & 1 deletion Test/Unit/Gateway/Http/Client/TransactionPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Adyen\Payment\Model\PaymentResponse;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Adyen\Service\Checkout;
use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Model\PaymentResponseFactory;
Expand Down
99 changes: 99 additions & 0 deletions Test/Unit/Helper/PaymentDetailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/

use Adyen\Payment\Helper\PaymentsDetails;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Payment;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Helper\PaymentResponseHandler;
use Adyen\Payment\Helper\Idempotency;
use Adyen\AdyenException;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\ValidatorException;
use Adyen\Service\Checkout;
use Adyen\Client;

class PaymentDetailsTest extends AbstractAdyenTestCase
{
private $checkoutSessionMock;
private $adyenHelperMock;
private $adyenLoggerMock;
private $paymentResponseHandlerMock;
private $idempotencyHelperMock;
private $paymentDetails;

protected function setUp(): void
{
$this->checkoutSessionMock = $this->createMock(Session::class);
$this->adyenHelperMock = $this->createMock(Data::class);
$this->adyenLoggerMock = $this->createMock(AdyenLogger::class);
$this->paymentResponseHandlerMock = $this->createMock(PaymentResponseHandler::class);
$this->idempotencyHelperMock = $this->createMock(Idempotency::class);

$this->paymentDetails = new PaymentsDetails(
$this->checkoutSessionMock,
$this->adyenHelperMock,
$this->adyenLoggerMock,
$this->paymentResponseHandlerMock,
$this->idempotencyHelperMock
);
}

public function testRequestHeadersAreAddedToRequest()
{
$orderMock = $this->createMock(OrderInterface::class);
$paymentMock = $this->createMock(Payment::class);
$checkoutServiceMock = $this->createMock(Checkout::class);
$adyenClientMock = $this->createMock(Client::class);
$storeId = 1;
$payload = json_encode([
'details' => 'some_details',
'paymentData' => 'some_payment_data',
'threeDSAuthenticationOnly' => true
]);
$requestOptions = [
'idempotencyKey' => 'some_idempotency_key',
'headers' => ['headerKey' => 'headerValue']
];
$paymentDetailsResult = ['resultCode' => 'Authorised', 'action' => null, 'additionalData' => null];

$orderMock->method('getPayment')->willReturn($paymentMock);
$orderMock->method('getStoreId')->willReturn($storeId);
$paymentMock->method('getOrder')->willReturn($orderMock);

$this->adyenHelperMock->method('initializeAdyenClient')->willReturn($adyenClientMock);
$this->adyenHelperMock->method('createAdyenCheckoutService')->willReturn($checkoutServiceMock);
$this->adyenHelperMock->method('buildRequestHeaders')->willReturn($requestOptions['headers']);
$this->idempotencyHelperMock->method('generateIdempotencyKey')->willReturn($requestOptions['idempotencyKey']);

$checkoutServiceMock->expects($this->once())
->method('paymentsDetails')
->with(
$this->equalTo([
'details' => 'some_details',
'paymentData' => 'some_payment_data',
'threeDSAuthenticationOnly' => true
]),
$this->equalTo($requestOptions)
)
->willReturn($paymentDetailsResult);

$this->paymentResponseHandlerMock->method('handlePaymentResponse')->willReturn(true);
$this->paymentResponseHandlerMock->method('formatPaymentResponse')->willReturn($paymentDetailsResult);

$result = $this->paymentDetails->initiatePaymentDetails($orderMock, $payload);

$this->assertJson($result);
$this->assertEquals(json_encode($paymentDetailsResult), $result);
}
}

0 comments on commit b880b7c

Please sign in to comment.