<?php

namespace App\Tests\Unit\Service\Twig;

use App\Entity\Core\File;
use App\Service\File\FileManager;
use App\Service\Twig\AssetsExtension;
use App\Service\Utils\Path;
use PHPUnit\Framework\TestCase;

class AssetsExtensionTest extends TestCase
{
    private FileManager $fileManager;

    public function setUp(): void {
        $this->fileManager = $this->getMockBuilder(FileManager::class)->disableOriginalConstructor()->getMock();
    }

    public function testGetFunctions(): void {
        $assetsExtension = $this
            ->getMockBuilder(AssetsExtension::class)
            ->setConstructorArgs([$this->fileManager])
            ->setMethodsExcept(['getFunctions'])
            ->getMock();

        $functions = $assetsExtension->getFunctions();

        $this->assertCount(2, $functions);

        $this->assertEquals('absPath', $functions[0]->getName());
        $this->assertEquals('toBase64Src', $functions[1]->getName());
    }

    public function testAbsPath(): void {
        $assetsExtension = $this
            ->getMockBuilder(AssetsExtension::class)
            ->setConstructorArgs([$this->fileManager])
            ->setMethodsExcept(['absPath'])
            ->getMock();

        $publicDir = Path::getProjectDir();

        $this->assertEquals(
            $publicDir . '/public/foo',
            $assetsExtension->absPath('foo')
        );

        $this->assertEquals(
            $publicDir . '/public/foo/bar',
            $assetsExtension->absPath('foo/bar')
        );

        $this->assertEquals(
            $publicDir . '/public',
            $assetsExtension->absPath('')
        );
    }

    public function testToBase64Src(): void {
        $assetsExtension = $this
            ->getMockBuilder(AssetsExtension::class)
            ->setConstructorArgs([$this->fileManager])
            ->setMethodsExcept(['toBase64Src'])
            ->getMock();

        $file = $this->getMockBuilder(File::class)->getMock();
        $file->method('getMimeType')->willReturn('mime');

        $this->fileManager->method('read')->with($file)->willReturn('foo');

        $this->assertEquals(
            'data:mime;base64,' . base64_encode('foo'),
            $assetsExtension->toBase64Src($file)
        );

    }
}
