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 5.99 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
<?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
{
    private $adyenInitiateTerminalApi;

31 32 33 34 35 36 37
    const MODULE_VERSION = '1.0.0';
    const MODULE_NAME = 'ModuleVersion';
    const PLATFORM_VERSION = '2.0.0';
    const PLATFORM_NAME = 'PlatformName';
    const CUSTOMER_ID = '1';
    const CUSTOMER_EMAIL = 'customer@example.com';
    const RECURRING_TYPE = 'ONECLICK,RECURRING';
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

    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
90
    public function testAddSaleToAcquirerData()
91 92 93
    {
        $quote = $this->getSimpleMock(\Magento\Quote\Model\Quote::class);
        $request = [];
alessio's avatar
alessio committed
94
        $result = $this->adyenInitiateTerminalApi->addSaleToAcquirerData($request, $quote);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

        $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
123
    public function testAddSaleToAcquirerDataLoggedInCustomer()
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    {
        $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
145
        $result = $this->adyenInitiateTerminalApi->addSaleToAcquirerData($request, $quoteMock);
146
        
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        $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);
    }
176
}