<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bundle\TwigBundle\Tests\Functional;

use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;

class EmptyAppTest extends TestCase
{
    public function testBootEmptyApp()
    {
        $kernel = new EmptyAppKernel('test', true);
        $kernel->boot();

        $this->assertTrue($kernel->getContainer()->hasParameter('twig.default_path'));
        $this->assertNotEmpty($kernel->getContainer()->getParameter('twig.default_path'));
    }

    protected function setUp(): void
    {
        $this->deleteTempDir();
    }

    protected function tearDown(): void
    {
        $this->deleteTempDir();
    }

    private function deleteTempDir()
    {
        if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel')) {
            return;
        }

        $fs = new Filesystem();
        $fs->remove($dir);
    }
}

class EmptyAppKernel extends Kernel
{
    public function registerBundles(): iterable
    {
        return [new TwigBundle()];
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(static function (ContainerBuilder $container) {
            $container->register('error_renderer.html', HtmlErrorRenderer::class);
            $container->setAlias('error_renderer', 'error_renderer.html');
            $container->setParameter('debug.file_link_format', null);
        });
    }

    public function getCacheDir(): string
    {
        return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/cache/'.$this->environment;
    }

    public function getLogDir(): string
    {
        return sys_get_temp_dir().'/'.Kernel::VERSION.'/EmptyAppKernel/logs';
    }
}
