CurrencyConfigTest.php 3.66 KB
Newer Older
Ketan's avatar
Ketan committed
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Directory\Test\Unit\Model;

use Magento\Config\App\Config\Type\System;
use Magento\Directory\Model\CurrencyConfig;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;

/**
 * Provide tests for CurrencyConfig model.
 */
class CurrencyConfigTest extends TestCase
{
    /**
     * @var CurrencyConfig
     */
    private $testSubject;

    /**
     * @var System|\PHPUnit_Framework_MockObject_MockObject
     */
    private $config;

    /**
     * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $storeManager;

    /**
     * @var State|\PHPUnit_Framework_MockObject_MockObject
     */
    private $appState;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        $this->config = $this->getMockBuilder(ScopeConfigInterface::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
            ->setMethods(['getStores', 'getWebsites'])
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
        $this->appState = $this->getMockBuilder(State::class)
            ->disableOriginalConstructor()
            ->getMock();
        $objectManager = new ObjectManager($this);
        $this->testSubject = $objectManager->getObject(
            CurrencyConfig::class,
            [
                'storeManager' => $this->storeManager,
                'appState' => $this->appState,
                'config' => $this->config,
            ]
        );
    }

    /**
     * Test get currency config for admin, crontab and storefront areas.
     *
     * @dataProvider getConfigCurrenciesDataProvider
     * @return void
     */
    public function testGetConfigCurrencies(string $areCode)
    {
        $path = 'test/path';
        $expected = ['ARS', 'AUD', 'BZD'];

        $this->appState->expects(self::once())
            ->method('getAreaCode')
            ->willReturn($areCode);

        /** @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject $store */
        $store = $this->getMockBuilder(StoreInterface::class)
            ->setMethods(['getCode'])
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
        $store->expects(self::once())
            ->method('getCode')
            ->willReturn('testCode');

        if (in_array($areCode, [Area::AREA_ADMINHTML, Area::AREA_CRONTAB])) {
            $this->storeManager->expects(self::once())
                ->method('getStores')
                ->willReturn([$store]);
        } else {
            $this->storeManager->expects(self::once())
                ->method('getStore')
                ->willReturn($store);
        }

        $this->config->expects(self::once())
            ->method('getValue')
            ->with(
                self::identicalTo($path)
            )->willReturn('ARS,AUD,BZD');

        $result = $this->testSubject->getConfigCurrencies($path);

        self::assertEquals($expected, $result);
    }

    /**
     * Provide test data for getConfigCurrencies test.
     *
     * @return array
     */
    public function getConfigCurrenciesDataProvider()
    {
        return [
            ['areaCode' => Area::AREA_ADMINHTML],
            ['areaCode' => Area::AREA_CRONTAB],
            ['areaCode' => Area::AREA_FRONTEND],
        ];
    }
}