TestRunnerKernel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Core\Test;
  3. use Drupal\Core\DrupalKernel;
  4. use Drupal\Core\Extension\Extension;
  5. use Drupal\Core\Site\Settings;
  6. use Symfony\Component\HttpFoundation\Request;
  7. /**
  8. * Kernel for run-tests.sh.
  9. */
  10. class TestRunnerKernel extends DrupalKernel {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static function createFromRequest(Request $request, $class_loader, $environment = 'test_runner', $allow_dumping = TRUE, $app_root = NULL) {
  15. return parent::createFromRequest($request, $class_loader, $environment, $allow_dumping, $app_root);
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function __construct($environment, $class_loader, $allow_dumping = FALSE, $app_root = NULL) {
  21. // Force $allow_dumping to FALSE, because the test runner kernel should
  22. // always have to rebuild its container, and potentially avoid isolation
  23. // issues against the tests.
  24. parent::__construct($environment, $class_loader, FALSE, $app_root);
  25. // Prime the module list and corresponding Extension objects.
  26. // @todo Remove System module. Needed because
  27. // \Drupal\Core\Datetime\DateFormatter has a (needless) dependency on the
  28. // 'date_format' entity, so calls to DateFormatter::format() and
  29. // DateFormatter::formatInterval() cause a plugin not found exception.
  30. $this->moduleList = [
  31. 'system' => 0,
  32. 'simpletest' => 0,
  33. ];
  34. $this->moduleData = [
  35. 'system' => new Extension($this->root, 'module', 'core/modules/system/system.info.yml', 'system.module'),
  36. 'simpletest' => new Extension($this->root, 'module', 'core/modules/simpletest/simpletest.info.yml', 'simpletest.module'),
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function boot() {
  43. // Ensure that required Settings exist.
  44. if (!Settings::getAll()) {
  45. new Settings([
  46. 'hash_salt' => 'run-tests',
  47. 'container_yamls' => [],
  48. // If there is no settings.php, then there is no parent site. In turn,
  49. // there is no public files directory; use a custom public files path.
  50. 'file_public_path' => 'sites/default/files',
  51. ]);
  52. }
  53. // Remove Drupal's error/exception handlers; they are designed for HTML
  54. // and there is no storage nor a (watchdog) logger here.
  55. restore_error_handler();
  56. restore_exception_handler();
  57. // In addition, ensure that PHP errors are not hidden away in logs.
  58. ini_set('display_errors', TRUE);
  59. parent::boot();
  60. $this->getContainer()->get('module_handler')->loadAll();
  61. $test_discovery = new TestDiscovery(
  62. $this->getContainer()->get('app.root'),
  63. $this->getContainer()->get('class_loader')
  64. );
  65. $test_discovery->registerTestNamespaces();
  66. // Register stream wrappers.
  67. $this->getContainer()->get('stream_wrapper_manager')->register();
  68. // Create the build/artifacts directory if necessary.
  69. include_once $this->getAppRoot() . '/core/includes/file.inc';
  70. if (!is_dir('public://simpletest')) {
  71. mkdir('public://simpletest', 0777, TRUE);
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function discoverServiceProviders() {
  78. parent::discoverServiceProviders();
  79. // The test runner does not require an installed Drupal site to exist.
  80. // Therefore, its environment is identical to that of the early installer.
  81. $this->serviceProviderClasses['app']['Test'] = 'Drupal\Core\Installer\InstallerServiceProvider';
  82. }
  83. }