AppKernel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
  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. $dir = dirname($dir);
  25. }
  26. use Symfony\Component\HttpKernel\Util\Filesystem;
  27. use Symfony\Component\Config\Loader\LoaderInterface;
  28. use Symfony\Component\HttpKernel\Kernel;
  29. /**
  30. * App Test Kernel for functional tests.
  31. *
  32. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  33. */
  34. class AppKernel extends Kernel
  35. {
  36. private $testCase;
  37. private $rootConfig;
  38. public function __construct($testCase, $rootConfig, $environment, $debug)
  39. {
  40. if (!is_dir(__DIR__.'/'.$testCase)) {
  41. throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
  42. }
  43. $this->testCase = $testCase;
  44. $fs = new Filesystem();
  45. if (!$fs->isAbsolutePath($rootConfig) && !file_exists($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
  46. throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
  47. }
  48. $this->rootConfig = $rootConfig;
  49. parent::__construct($environment, $debug);
  50. }
  51. public function registerBundles()
  52. {
  53. if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
  54. throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
  55. }
  56. return include $filename;
  57. }
  58. public function init()
  59. {
  60. }
  61. public function getRootDir()
  62. {
  63. return __DIR__;
  64. }
  65. public function getCacheDir()
  66. {
  67. return sys_get_temp_dir().'/'.$this->testCase.'/cache/'.$this->environment;
  68. }
  69. public function getLogDir()
  70. {
  71. return sys_get_temp_dir().'/'.$this->testCase.'/logs';
  72. }
  73. public function registerContainerConfiguration(LoaderInterface $loader)
  74. {
  75. $loader->load($this->rootConfig);
  76. }
  77. public function serialize()
  78. {
  79. return serialize(array($this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
  80. }
  81. public function unserialize($str)
  82. {
  83. call_user_func_array(array($this, '__construct'), unserialize($str));
  84. }
  85. protected function getKernelParameters()
  86. {
  87. $parameters = parent::getKernelParameters();
  88. $parameters['kernel.test_case'] = $this->testCase;
  89. return $parameters;
  90. }
  91. }