AppKernel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\FrameworkBundle\Tests\Functional\app;
  11. // get the autoload file
  12. $dir = __DIR__;
  13. $lastDir = null;
  14. while ($dir !== $lastDir) {
  15. $lastDir = $dir;
  16. if (file_exists($dir.'/autoload.php')) {
  17. require_once $dir.'/autoload.php';
  18. break;
  19. }
  20. if (file_exists($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) && !file_exists($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. public function registerBundles()
  58. {
  59. if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
  60. throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
  61. }
  62. return include $filename;
  63. }
  64. public function getRootDir()
  65. {
  66. return __DIR__;
  67. }
  68. public function getCacheDir()
  69. {
  70. return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
  71. }
  72. public function getLogDir()
  73. {
  74. return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/logs';
  75. }
  76. public function registerContainerConfiguration(LoaderInterface $loader)
  77. {
  78. $loader->load($this->rootConfig);
  79. }
  80. public function serialize()
  81. {
  82. return serialize(array($this->varDir, $this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
  83. }
  84. public function unserialize($str)
  85. {
  86. $a = unserialize($str);
  87. $this->__construct($a[0], $a[1], $a[2], $a[3], $a[4]);
  88. }
  89. protected function getKernelParameters()
  90. {
  91. $parameters = parent::getKernelParameters();
  92. $parameters['kernel.test_case'] = $this->testCase;
  93. return $parameters;
  94. }
  95. }