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

IpAddressTest.php 3.24 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 99 100 101 102 103 104 105 106 107 108 109 110
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2020 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Tests\Helper;

class IpAddressTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Adyen\Payment\Helper\IpAddress
     */
    private $ipAddressHelper;

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

    protected function setUp()
    {
        $cache = $this->getSimpleMock(\Magento\Framework\App\CacheInterface::class);
        $cache->method('load')->willReturn(
            array(
                '1.2.3.4',
                '20.20.20.20'
            )
        );
        $serializer = $this->getSimpleMock(\Magento\Framework\Serialize\SerializerInterface::class);
        $serializer->method('unserialize')->willReturnArgument(0);
        $ipAddressUtil = $this->getSimpleMock(\Adyen\Util\IpAddress::class);
        $adyenLogger = $this->getSimpleMock(\Adyen\Payment\Logger\AdyenLogger::class);

        $this->ipAddressHelper = new \Adyen\Payment\Helper\IpAddress(
            $ipAddressUtil,
            $cache,
            $serializer,
            $adyenLogger
        );
    }

    /**
     * @dataProvider ipAddressesProvider
     */
    public function testIsIpAddressValid($ipAddress, $expectedResult)
    {
        $this->assertEquals($expectedResult, $this->ipAddressHelper->isIpAddressValid([$ipAddress]));
    }

    public function testUpdateCachedIpAddresses()
    {
        $this->assertNull($this->ipAddressHelper->updateCachedIpAddresses());
    }

    public function testSaveIpAddressesToCache()
    {
        $this->assertNull($this->ipAddressHelper->saveIpAddressesToCache([]));
    }

    public function testGetIpAddressesFromCache()
    {
        $this->assertTrue(is_array($this->ipAddressHelper->getIpAddressesFromCache()));
    }

    public static function ipAddressesProvider()
    {
        return array(
            array(
                '1.2.3.4',
                true
            ),
            array(
                '20.20.20.20',
                true
            ),
            array(
                '8.8.8.8',
                false
            ),
            array(
                '192.168.100.10',
                false
            ),
            array(
                '500.168.100.10',
                false
            )
        );
    }
}