1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Bundle\SecurityBundle\Tests\Functional\app;
13
14use Doctrine\ORM\Version;
15use Symfony\Component\Config\Loader\LoaderInterface;
16use Symfony\Component\Filesystem\Filesystem;
17use Symfony\Component\HttpKernel\Kernel;
18
19/**
20 * App Test Kernel for functional tests.
21 *
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
23 */
24class AppKernel extends Kernel
25{
26    private $varDir;
27    private $testCase;
28    private $rootConfig;
29
30    public function __construct($varDir, $testCase, $rootConfig, $environment, $debug)
31    {
32        if (!is_dir(__DIR__.'/'.$testCase)) {
33            throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
34        }
35        $this->varDir = $varDir;
36        $this->testCase = $testCase;
37
38        $fs = new Filesystem();
39        if (!$fs->isAbsolutePath($rootConfig) && !is_file($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
40            throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
41        }
42        $this->rootConfig = $rootConfig;
43
44        parent::__construct($environment, $debug);
45    }
46
47    /**
48     * {@inheritdoc}
49     */
50    public function getName()
51    {
52        if (null === $this->name) {
53            $this->name = parent::getName().substr(md5($this->rootConfig), -16);
54        }
55
56        return $this->name;
57    }
58
59    public function registerBundles()
60    {
61        if (!is_file($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
62            throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
63        }
64
65        return include $filename;
66    }
67
68    public function getRootDir()
69    {
70        return __DIR__;
71    }
72
73    public function getCacheDir()
74    {
75        return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
76    }
77
78    public function getLogDir()
79    {
80        return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/logs';
81    }
82
83    public function registerContainerConfiguration(LoaderInterface $loader)
84    {
85        $loader->load($this->rootConfig);
86
87        // to be removed once https://github.com/doctrine/DoctrineBundle/pull/684 is merged
88        if ('Acl' === $this->testCase && class_exists(Version::class)) {
89            $loader->load(__DIR__.'/Acl/doctrine.yml');
90        }
91    }
92
93    public function serialize()
94    {
95        return serialize([$this->varDir, $this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()]);
96    }
97
98    public function unserialize($str)
99    {
100        $a = unserialize($str);
101        $this->__construct($a[0], $a[1], $a[2], $a[3], $a[4]);
102    }
103
104    protected function getKernelParameters()
105    {
106        $parameters = parent::getKernelParameters();
107        $parameters['kernel.test_case'] = $this->testCase;
108
109        return $parameters;
110    }
111}
112