We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

AdyenInitiateTerminalApiTest.php 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2019 Adyen B.V.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Model;


class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase
{

    /**
     * @var
     */
    private $adyenInitiateTerminalApi;

    private const MODULE_VERSION = '1.0.0';
    private const MODULE_NAME = 'ModuleVersion';
    private const PLATFORM_VERSION = '2.0.0';
    private const PLATFORM_NAME = 'PlatformName';
    private const CUSTOMER_ID = '1';
    private const CUSTOMER_EMAIL = 'customer@example.com';
    private const RECURRING_TYPE = 'ONECLICK,RECURRING';


    private function getSimpleMock($originalClassName)
    {
        return $this->getMockBuilder($originalClassName)
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function setUp()
    {
        $adyenHelper = $this->getSimpleMock(\Adyen\Payment\Helper\Data::class);
        $adyenLogger = $this->getSimpleMock(\Adyen\Payment\Logger\AdyenLogger::class);
        $checkoutSession = $this->getSimpleMock(\Magento\Checkout\Model\Session::class);
        $storeManager = $this->getSimpleMock(\Magento\Store\Model\StoreManagerInterface::class);
        $productMetadata = $this->getSimpleMock(\Magento\Framework\App\ProductMetadataInterface::class);

        $store = $this->getSimpleMock(\Magento\Store\Api\Data\StoreInterface::class);
        $storeManager->method('getStore')
            ->will($this->returnValue($store));


        $adyenHelper->method('getModuleVersion')
            ->will($this->returnValue(self::MODULE_VERSION));


        // Create a map of arguments to return values.
        $map = [
            ['pos_timeout', null, ''],
            ['recurring_type', null, self::RECURRING_TYPE]
        ];

        // Configure the stub.
        $adyenHelper->method('getAdyenPosCloudConfigData')
            ->will($this->returnValueMap($map));

        $adyenHelper->method('getModuleName')
            ->will($this->returnValue(self::MODULE_NAME));

        $productMetadata->method('getVersion')
            ->will($this->returnValue(self::PLATFORM_VERSION));

        $productMetadata->method('getName')
            ->will($this->returnValue(self::PLATFORM_NAME));


        $this->adyenInitiateTerminalApi = new \Adyen\Payment\Model\AdyenInitiateTerminalApi(
            $adyenHelper,
            $adyenLogger,
            $checkoutSession,
            $storeManager,
            $productMetadata
        );
    }

    /** @throws \Exception */
alessio's avatar
alessio committed
99
    public function testAddSaleToAcquirerData()
100 101 102
    {
        $quote = $this->getSimpleMock(\Magento\Quote\Model\Quote::class);
        $request = [];
alessio's avatar
alessio committed
103
        $result = $this->adyenInitiateTerminalApi->addSaleToAcquirerData($request, $quote);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

        $appInfo = [
            'applicationInfo' => [
                'merchantApplication' => [
                    'version' => self::MODULE_VERSION,
                    'name' => self::MODULE_NAME
                ],
                'externalPlatform' => [
                    'version' => self::PLATFORM_VERSION,
                    'name' => self::PLATFORM_NAME
                ]
            ]
        ];

        $saleToAcquirerData = base64_encode(json_encode($appInfo));
        $resultArrayExpected = [
            'SaleToPOIRequest' => [
                'PaymentRequest' => [
                    'SaleData' => [
                        'SaleToAcquirerData' => $saleToAcquirerData
                    ]
                ]
            ]
        ];

        $this->assertEquals($resultArrayExpected, $result);
    }

alessio's avatar
alessio committed
132
    public function testAddSaleToAcquirerDataLoggedInCustomer()
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    {
        $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote')
            ->disableOriginalConstructor()
            ->setMethods(
                [
                    'getId',
                    'getCustomerEmail',
                    'getCustomerId'
                ]
            )
            ->getMock();


        $quoteMock->expects($this->any())
            ->method('getCustomerId')
            ->willReturn(self::CUSTOMER_ID);

        $quoteMock->expects($this->atLeastOnce())
            ->method('getCustomerEmail')
            ->willReturn(self::CUSTOMER_EMAIL);

        $request = [];
alessio's avatar
alessio committed
155
        $result = $this->adyenInitiateTerminalApi->addSaleToAcquirerData($request, $quoteMock);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187


        $appInfo = [
            'shopperEmail' => self::CUSTOMER_EMAIL,
            'shopperReference' => self::CUSTOMER_ID,
            'recurringContract' => self::RECURRING_TYPE,
            'applicationInfo' => [
                'merchantApplication' => [
                    'version' => self::MODULE_VERSION,
                    'name' => self::MODULE_NAME
                ],
                'externalPlatform' => [
                    'version' => self::PLATFORM_VERSION,
                    'name' => self::PLATFORM_NAME
                ]
            ]
        ];

        $saleToAcquirerData = base64_encode(json_encode($appInfo));
        $resultArrayExpected = [
            'SaleToPOIRequest' => [
                'PaymentRequest' => [
                    'SaleData' => [
                        'SaleToAcquirerData' => $saleToAcquirerData
                    ]
                ]
            ]
        ];

        $this->assertEquals($resultArrayExpected, $result);
    }
}