<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Tests\Unit\Core\Module\SourceHandler;

use PHPUnit\Framework\TestCase;
use PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException;
use PrestaShop\PrestaShop\Core\Module\SourceHandler\ZipSourceHandler;
use Symfony\Contracts\Translation\TranslatorInterface;
use Tests\Resources\ResourceResetter;

class ZipSourceHandlerTest extends TestCase
{
    private const UNHANDLABLE_SOURCE = 'unhandlablesource';
    private const INVALID_SOURCE = __DIR__ . '/../../../../Resources/dummyFile/invalid_module.zip';
    private const VALID_SOURCE = __DIR__ . '/../../../../Resources/dummyFile/valid_module.zip';

    /** @var ZipSourceHandler */
    private $zipSourceHandler;

    /**
     * @var ResourceResetter : responsible to reset resources used for tests
     * */
    private $resourceResetter;

    public function setUp(): void
    {
        $this->resourceResetter = new ResourceResetter();
        $this->resourceResetter->backupTestModules();

        $translator = $this->createMock(TranslatorInterface::class);
        $translator->method('trans')->willReturnArgument(0);

        $this->zipSourceHandler = new ZipSourceHandler(
            $this->resourceResetter::TEST_MODULES_DIR,
            $translator
        );
    }

    public function tearDown(): void
    {
        $this->resourceResetter->resetTestModules();
    }

    public function testCanHandle(): void
    {
        $this->assertFalse($this->zipSourceHandler->canHandle(self::UNHANDLABLE_SOURCE));
        $this->assertTrue($this->zipSourceHandler->canHandle(self::INVALID_SOURCE));
        $this->assertTrue($this->zipSourceHandler->canHandle(self::VALID_SOURCE));
    }

    public function testGetNameUnexistingSource(): void
    {
        $this->expectException(ModuleErrorException::class);
        $this->zipSourceHandler->getModuleName(self::UNHANDLABLE_SOURCE);
    }

    public function testGetNameInvalidSource(): void
    {
        $this->expectException(ModuleErrorException::class);
        $this->zipSourceHandler->getModuleName(self::INVALID_SOURCE);
    }

    public function testGetNameValidSource(): void
    {
        $this->assertSame(
            'valid_module',
            $this->zipSourceHandler->getModuleName(self::VALID_SOURCE)
        );
    }

    public function testHandleUnhandlableSource(): void
    {
        $this->expectException(ModuleErrorException::class);
        $this->zipSourceHandler->handle(self::UNHANDLABLE_SOURCE);
    }

    public function testHandleValidSource(): void
    {
        $this->zipSourceHandler->handle(self::VALID_SOURCE);
        $this->assertFileExists($this->resourceResetter::TEST_MODULES_DIR . '/valid_module/valid_module.php');
    }
}
