<?php

/**
 * Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * https://www.d3data.de
 *
 * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
 * @author    D3 Data Development - Daniel Seifert <info@shopmodule.com>
 * @link      https://www.oxidmodule.com
 */

declare(strict_types=1);

namespace D3\Ordermanager\tests\unit\Application\Controller\Admin;

use D3\DIContainerHandler\d3DicException;
use D3\ModCfg\Application\Model\Configuration\d3_cfg_mod;
use D3\ModCfg\Application\Model\d3filesystem;
use D3\ModCfg\Application\Model\d3str;
use D3\ModCfg\Application\Model\Exception\wrongModIdException;
use D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall;
use D3\Ordermanager\Application\Model\d3ordermanager;
use D3\Ordermanager\tests\unit\d3OrdermanagerUnitTestCase;
use Doctrine\DBAL\Exception as DBALException;
use Exception;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Language;
use OxidEsales\Facts\Facts;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
use stdClass;

/**
 * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall
 */
class d3_cfg_ordermanageritem_mallTest extends d3OrdermanagerUnitTestCase
{
    /** @var d3_cfg_ordermanageritem_mall */
    protected $_oController;

    /**
     * setup basic requirements
     * @throws DBALException
     * @throws DatabaseConnectionException
     * @throws DatabaseErrorException
     * @throws Exception
     */
    public function setUp(): void
    {
        parent::setUp();

        $this->_oController = d3GetOxidDIC()->get(d3_cfg_ordermanageritem_mall::class);
    }

    public function tearDown(): void
    {
        parent::tearDown();

        unset($this->_oController);
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::__construct
     * @test
     * @return void
     * @throws d3DicException
     */
    public function constructorPass()
    {
        $this->assertSame(
            'd3_ordermanager',
            d3GetOxidDIC()->getParameter('d3.ordermanager.modcfgid')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::__construct
     * @test
     * @return void
     * @throws ReflectionException
     * @throws d3DicException
     */
    public function constructorException()
    {
        /** @var d3_cfg_ordermanageritem_mall|MockObject $controller */
        $controller = $this->getMockBuilder(d3_cfg_ordermanageritem_mall::class)
            ->disableOriginalConstructor()
            ->getMock();

        d3GetOxidDIC()->setParameter('d3.ordermanager.modcfgid', 'differentModCfgid');

        $this->expectException(wrongModIdException::class);

        $this->callMethod(
            $controller,
            '__construct'
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getProfile
     * @test
     * @throws ReflectionException
     */
    public function profileObjectHasRightInstance()
    {
        $this->assertInstanceOf(
            d3ordermanager::class,
            $this->callMethod($this->_oController, 'getProfile')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::render
     * @test
     * @throws ReflectionException
     */
    public function renderWithOxidPass()
    {
        $_POST['oxid'] = 'foobar';

        /** @var d3ordermanager|MockObject $oProfileMock */
        $oProfileMock = $this->getMockBuilder(d3ordermanager::class)
            ->onlyMethods(['loadInLang'])
            ->getMock();
        $oProfileMock->expects($this->once())->method('loadInLang')->willReturn(true);

        /** @var d3_cfg_ordermanageritem_mall|MockObject $oControllerMock */
        $oControllerMock = $this->getMockBuilder(d3_cfg_ordermanageritem_mall::class)
            ->onlyMethods([
                'getProfile',
                '_d3LoadInOtherLang',
                '_isSetOxid',
            ])
            ->getMock();
        $oControllerMock->method('getProfile')->willReturn($oProfileMock);
        $oControllerMock->method('_d3LoadInOtherLang')->willReturn($oProfileMock);
        $oControllerMock->method('_isSetOxid')->willReturn(true);

        $this->_oController = $oControllerMock;

        $this->assertSame(
            strtoupper((oxNew(Facts::class, __DIR__ ?? 'path'))->getEdition()) === 'EE' ?
                'admin_mall' :
                '@d3ordermanager/admin/d3_cfg_ordermanageritem_mall',
            $this->callMethod($this->_oController, 'render')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::render
     * @test
     * @throws ReflectionException
     */
    public function renderWithoutOxidPass()
    {
        $_POST['oxid'] = null;

        /** @var d3ordermanager|MockObject $oProfileMock */
        $oProfileMock = $this->getMockBuilder(d3ordermanager::class)
            ->onlyMethods(['loadInLang'])
            ->getMock();
        $oProfileMock->expects($this->never())->method('loadInLang')->willReturn(true);

        /** @var d3_cfg_ordermanageritem_mall|MockObject $oControllerMock */
        $oControllerMock = $this->getMockBuilder(d3_cfg_ordermanageritem_mall::class)
            ->onlyMethods([
                'getProfile',
                '_d3LoadInOtherLang',
                '_isSetOxid',
            ])
            ->getMock();
        $oControllerMock->method('getProfile')->willReturn($oProfileMock);
        $oControllerMock->method('_d3LoadInOtherLang')->willReturn($oProfileMock);
        $oControllerMock->method('_isSetOxid')->willReturn(false);

        $this->_oController = $oControllerMock;

        $this->assertSame(
            strtoupper((oxNew(Facts::class, __DIR__ ?? 'path'))->getEdition()) === 'EE' ?
                'admin_mall' :
                '@d3ordermanager/admin/d3_cfg_ordermanageritem_mall',
            $this->callMethod($this->_oController, 'render')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getUserMessages
     * @test
     * @throws ReflectionException
     */
    public function canGetUserMessages()
    {
        $this->assertIsArray(
            $this->callMethod($this->_oController, 'getUserMessages')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getLang
     * @test
     * @throws ReflectionException
     */
    public function languageObjectIsRightInstance()
    {
        $this->assertInstanceOf(
            Language::class,
            $this->callMethod($this->_oController, 'getLang')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getHelpURL
     * @test
     * @throws ReflectionException
     */
    public function canGetHelpUrlWithExtension()
    {
        /** @var d3filesystem|MockObject $oFileSystemMock */
        $oFileSystemMock = $this->getMockBuilder(d3filesystem::class)
            ->onlyMethods([
                'unprefixedslashit',
                'splitFilename',
            ])
            ->getMock();
        $oFileSystemMock->method('unprefixedslashit')->willReturnCallback([$this, 'firstArgumentReturnCallback']);
        $oFileSystemMock->method('splitFilename')->willReturn(
            ['name' => 'filename', 'ext' => 'html']
        );
        d3GetOxidDIC()->set(d3filesystem::class, $oFileSystemMock);

        /** @var Language|MockObject $oLangMock */
        $oLangMock = $this->getMockBuilder(Language::class)
            ->onlyMethods(['TranslateString'])
            ->getMock();
        $oLangMock->method('TranslateString')->willReturn('modulepath');

        /** @var stdClass|MockObject $oModCfgMock */
        $oModCfgMock = $this->getMockBuilder(stdClass::class)
            ->addMethods(['getHelpURL'])
            ->getMock();
        $oModCfgMock->method('getHelpURL')->willReturn('https://faq.d3data.de/module/');

        /** @var d3_cfg_ordermanageritem_mall|MockObject $oControllerMock */
        $oControllerMock = $this->getMockBuilder(d3_cfg_ordermanageritem_mall::class)
            ->onlyMethods([
                'd3GetSet',
                'getLang',
            ])
            ->getMock();
        $oControllerMock->method('d3GetSet')->willReturn($oModCfgMock);
        $oControllerMock->method('getLang')->willReturn($oLangMock);

        $this->setValue($oControllerMock, '_sHelpLinkMLAdd', 'foobar');

        $this->_oController = $oControllerMock;

        $this->assertEquals(
            'https://faq.d3data.de/module/modulepath',
            $this->callMethod($this->_oController, 'getHelpURL')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getHelpURL
     * @test
     * @throws ReflectionException
     */
    public function canGetHelpUrlWithoutExtension()
    {
        /** @var d3filesystem|MockObject $oFileSystemMock */
        $oFileSystemMock = $this->getMockBuilder(d3filesystem::class)
            ->onlyMethods(['splitFilename'])
            ->getMock();
        $oFileSystemMock->method('splitFilename')->willReturn(
            ['name' => 'filename', 'ext' => '']
        );

        d3GetOxidDIC()->set(d3filesystem::class, $oFileSystemMock);

        /** @var d3str|MockObject $oD3StrMock */
        $oD3StrMock = $this->getMockBuilder(d3str::class)
            ->onlyMethods([
                'unprefixedslashit',
                'trailingslashit',
            ])
            ->getMock();
        $oD3StrMock->method('unprefixedslashit')->willReturnCallback([$this, 'firstArgumentReturnCallback']);
        $oD3StrMock->expects($this->once())->method('trailingslashit')->willReturnCallback([$this, 'firstArgumentReturnCallback']);

        d3GetOxidDIC()->set(d3str::class, $oD3StrMock);

        /** @var Language|MockObject $oLangMock */
        $oLangMock = $this->getMockBuilder(Language::class)
            ->onlyMethods(['TranslateString'])
            ->getMock();
        $oLangMock->method('TranslateString')->willReturn('modulepath');

        /** @var stdClass|MockObject $oModCfgMock */
        $oModCfgMock = $this->getMockBuilder(stdClass::class)
            ->addMethods(['getHelpURL'])
            ->getMock();
        $oModCfgMock->method('getHelpURL')->willReturn('https://faq.d3data.de/module/');

        /** @var d3_cfg_ordermanageritem_mall|MockObject $oControllerMock */
        $oControllerMock = $this->getMockBuilder(d3_cfg_ordermanageritem_mall::class)
            ->onlyMethods([
                'd3GetSet',
                'getLang',
            ])
            ->getMock();
        $oControllerMock->method('d3GetSet')->willReturn($oModCfgMock);
        $oControllerMock->method('getLang')->willReturn($oLangMock);

        $this->setValue($oControllerMock, '_sHelpLinkMLAdd', 'foobar');

        $this->_oController = $oControllerMock;

        $this->assertEquals(
            'https://faq.d3data.de/module/modulepath',
            $this->callMethod($this->_oController, 'getHelpURL')
        );
    }

    /**
     * @return mixed
     */
    public function firstArgumentReturnCallback()
    {
        $args = func_get_args();
        return $args[0];
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::d3GetSet
     * @test
     * @throws ReflectionException
     */
    public function modcfgObjectIsRightInstance()
    {
        $this->assertInstanceOf(
            d3_cfg_mod::class,
            $this->callMethod($this->_oController, 'd3GetSet')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::getNaviItems
     * @test
     * @throws ReflectionException
     */
    public function naviItemshasRightType()
    {
        $this->assertIsArray(
            $this->callMethod($this->_oController, 'getNaviItems')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::d3GetMenuItemTitle
     * @test
     * @throws ReflectionException
     */
    public function menuItemTitleHasRightType()
    {
        $this->assertIsString(
            $this->callMethod($this->_oController, 'd3GetMenuItemTitle')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::d3GetMenuSubItemTitle
     * @test
     * @throws ReflectionException
     */
    public function menuSubItemTitleHasRightType()
    {
        $this->assertIsString(
            $this->callMethod($this->_oController, 'd3GetMenuSubItemTitle')
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::_isSetOxid
     * @test
     * @throws ReflectionException
     */
    public function checkSetOxidPass()
    {
        $this->assertTrue(
            $this->callMethod($this->_oController, '_isSetOxid', ['foobar'])
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::_isSetOxid
     * @test
     * @throws ReflectionException
     */
    public function checkUnsetOxidPass()
    {
        $this->assertFalse(
            $this->callMethod($this->_oController, '_isSetOxid', [-1])
        );
        $this->assertFalse(
            $this->callMethod($this->_oController, '_isSetOxid', [null])
        );
    }

    /**
     * @covers \D3\Ordermanager\Application\Controller\Admin\d3_cfg_ordermanageritem_mall::_d3LoadInOtherLang
     * @test
     * @throws ReflectionException
     */
    public function canLoadInOtherLang()
    {
        /** @var d3ordermanager|MockObject $oProfileMock */
        $oProfileMock = $this->getMockBuilder(d3ordermanager::class)
            ->onlyMethods([
                'getAvailableInLangs',
                'loadInLang',
            ])
            ->getMock();
        $oProfileMock->method('getAvailableInLangs')->willReturn(['en' => 'english']);
        $oProfileMock->expects($this->once())->method('loadInLang')->willReturn(true);

        $this->setValue($this->_oController, '_iEditLang', 'de');

        $this->assertSame(
            $oProfileMock,
            $this->callMethod($this->_oController, '_d3LoadInOtherLang', [$oProfileMock, 'foobar'])
        );
    }

    /**
     * @param $sLicenseKey
     * @param d3ordermanager $oManager
     * @return null
     */
    protected function _setModuleLicenseKey($sLicenseKey, $oManager = null)
    {
        return null;
    }
}
