<?php
/**
 * Copyright © 2016 maijindou.com All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Paypal\Test\Unit\Helper;

use Magento\Config\Model\Config;
use Magento\Config\Model\Config\ScopeDefiner;
use Magento\Directory\Helper\Data;
use Magento\Framework\App\Helper\Context;
use Magento\Paypal\Helper\Backend;
use Magento\Paypal\Model\Config\StructurePlugin;

class BackendTest extends \PHPUnit_Framework_TestCase
{
    const SCOPE = 'website';

    const SCOPE_ID = 1;

    /**
     * @var Context|\PHPUnit_Framework_MockObject_MockObject
     */
    private $context;

    /**
     * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $request;

    /**
     * @var Data|\PHPUnit_Framework_MockObject_MockObject
     */
    private $directoryHelperMock;

    /**
     * @var Config|\PHPUnit_Framework_MockObject_MockObject
     */
    private $backendConfig;

    /**
     * @var ScopeDefiner|\PHPUnit_Framework_MockObject_MockObject
     */
    private $scopeDefiner;

    /**
     * @var Backend
     */
    private $helper;

    public function setUp()
    {
        $this->context = $this->getMockBuilder('Magento\Framework\App\Helper\Context')
            ->disableOriginalConstructor()
            ->getMock();
        $this->request = $this->getMock('Magento\Framework\App\RequestInterface');
        $this->context->expects(static::once())
            ->method('getRequest')
            ->willReturn($this->request);
        $this->directoryHelperMock = $this->getMockBuilder('Magento\Directory\Helper\Data')
            ->disableOriginalConstructor()
            ->getMock();
        $this->backendConfig = $this->getMockBuilder('Magento\Config\Model\Config')
            ->disableOriginalConstructor()
            ->getMock();
        $this->scopeDefiner = $this->getMockBuilder('Magento\Config\Model\Config\ScopeDefiner')
            ->disableOriginalConstructor()
            ->getMock();

        $this->helper = new Backend(
            $this->context,
            $this->directoryHelperMock,
            $this->backendConfig,
            $this->scopeDefiner
        );
    }

    public function testGetConfigurationCountryCodeFromRequest()
    {
        $this->configurationCountryCodePrepareRequest('US');
        $this->configurationCountryCodeAssertResult('US');
    }

    /**
     * @param string|null $request
     * @dataProvider getConfigurationCountryCodeFromConfigDataProvider
     */
    public function testGetConfigurationCountryCodeFromConfig($request)
    {
        $this->configurationCountryCodePrepareRequest($request);
        $this->configurationCountryCodePrepareConfig('GB');
        $this->configurationCountryCodeAssertResult('GB');
    }

    public function getConfigurationCountryCodeFromConfigDataProvider()
    {
        return [
            [null],
            ['not country code'],
        ];
    }

    /**
     * @param string|null $request
     * @param string|null|false $config
     * @param string|null $default
     * @dataProvider getConfigurationCountryCodeFromDefaultDataProvider
     */
    public function testGetConfigurationCountryCodeFromDefault($request, $config, $default)
    {
        $this->configurationCountryCodePrepareRequest($request);
        $this->configurationCountryCodePrepareConfig($config);
        $this->directoryHelperMock->expects($this->once())
            ->method('getDefaultCountry')
            ->will($this->returnValue($default));
        $this->configurationCountryCodeAssertResult($default);
    }

    public function getConfigurationCountryCodeFromDefaultDataProvider()
    {
        return [
            [null, false, 'DE'],
            ['not country code', false, 'DE'],
            ['not country code', '', 'any final result']
        ];
    }

    /**
     * Prepare request for test
     *
     * @param string|null $request
     */
    private function configurationCountryCodePrepareRequest($request)
    {
        $this->request->expects($this->atLeastOnce())
            ->method('getParam')
            ->willReturnMap(
                [
                    [StructurePlugin::REQUEST_PARAM_COUNTRY, null, $request],
                    [self::SCOPE, null, self::SCOPE_ID]
                ]
            );
    }

    /**
     * Prepare backend config for test
     *
     * @param string|null|false $config
     */
    private function configurationCountryCodePrepareConfig($config)
    {

        $this->scopeDefiner->expects($this->once())
            ->method('getScope')
            ->willReturn(self::SCOPE);

        $this->backendConfig->expects($this->once())
            ->method('setData')
            ->with(self::SCOPE, self::SCOPE_ID);

        $this->backendConfig->expects($this->once())
            ->method('getConfigDataValue')
            ->with(\Magento\Paypal\Block\Adminhtml\System\Config\Field\Country::FIELD_CONFIG_PATH)
            ->willReturn($config);
    }

    /**
     * Assert result of getConfigurationCountryCode method
     *
     * @param string $expected
     */
    private function configurationCountryCodeAssertResult($expected)
    {
        $this->assertEquals($expected, $this->helper->getConfigurationCountryCode());
    }
}
