AppKernel.php 3.3 KB

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