bootstrap.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. use Cake\Cache\Cache;
  15. use Cake\Chronos\Chronos;
  16. use Cake\Core\Configure;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Datasource\FactoryLocator;
  19. use Cake\Error\Debug\TextFormatter;
  20. use Cake\Log\Engine\FileLog;
  21. use Cake\Log\Log;
  22. use Cake\ORM\Locator\TableLocator;
  23. use Cake\TestSuite\Fixture\SchemaLoader;
  24. use Cake\Utility\Security;
  25. use function Cake\Core\env;
  26. if (is_file('vendor/autoload.php')) {
  27. require_once 'vendor/autoload.php';
  28. } else {
  29. require_once dirname(__DIR__) . '/vendor/autoload.php';
  30. }
  31. if (!defined('DS')) {
  32. define('DS', DIRECTORY_SEPARATOR);
  33. }
  34. define('ROOT', dirname(__DIR__));
  35. define('APP_DIR', 'TestApp');
  36. define('TMP', sys_get_temp_dir() . DS);
  37. define('LOGS', TMP . 'logs' . DS);
  38. define('CACHE', TMP . 'cache' . DS);
  39. define('SESSIONS', TMP . 'sessions' . DS);
  40. define('CAKE_CORE_INCLUDE_PATH', ROOT);
  41. define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
  42. define('CAKE', CORE_PATH . 'src' . DS);
  43. define('CORE_TESTS', CORE_PATH . 'tests' . DS);
  44. define('CORE_TEST_CASES', CORE_TESTS . 'TestCase');
  45. define('TEST_APP', CORE_TESTS . 'test_app' . DS);
  46. // Point app constants to the test app.
  47. define('APP', TEST_APP . 'TestApp' . DS);
  48. define('WWW_ROOT', TEST_APP . 'webroot' . DS);
  49. define('CONFIG', TEST_APP . 'config' . DS);
  50. // phpcs:disable
  51. @mkdir(LOGS);
  52. @mkdir(SESSIONS);
  53. @mkdir(CACHE);
  54. @mkdir(CACHE . 'views');
  55. @mkdir(CACHE . 'models');
  56. // phpcs:enable
  57. require_once 'check.php';
  58. require_once CORE_PATH . 'config/bootstrap.php';
  59. date_default_timezone_set('UTC');
  60. mb_internal_encoding('UTF-8');
  61. Configure::write('debug', true);
  62. Configure::write('App', [
  63. 'namespace' => 'App',
  64. 'encoding' => 'UTF-8',
  65. 'base' => false,
  66. 'baseUrl' => false,
  67. 'dir' => APP_DIR,
  68. 'webroot' => 'webroot',
  69. 'wwwRoot' => WWW_ROOT,
  70. 'fullBaseUrl' => 'http://localhost',
  71. 'imageBaseUrl' => 'img/',
  72. 'jsBaseUrl' => 'js/',
  73. 'cssBaseUrl' => 'css/',
  74. 'paths' => [
  75. 'plugins' => [TEST_APP . 'Plugin' . DS],
  76. 'templates' => [TEST_APP . 'templates' . DS],
  77. 'locales' => [TEST_APP . 'resources' . DS . 'locales' . DS],
  78. ],
  79. ]);
  80. Cache::setConfig([
  81. '_cake_translations_' => [
  82. 'engine' => 'File',
  83. 'prefix' => 'cake_core_',
  84. 'serialize' => true,
  85. ],
  86. '_cake_model_' => [
  87. 'engine' => 'File',
  88. 'prefix' => 'cake_model_',
  89. 'serialize' => true,
  90. ],
  91. ]);
  92. // Ensure default test connection is defined
  93. if (!getenv('DB_URL')) {
  94. putenv('DB_URL=sqlite:///:memory:');
  95. }
  96. ConnectionManager::setConfig('test', ['url' => getenv('DB_URL')]);
  97. if (env('CAKE_TEST_AUTOQUOTE')) {
  98. ConnectionManager::get('test')->getDriver()->enableAutoQuoting(true);
  99. }
  100. Configure::write('Session', [
  101. 'defaults' => 'php',
  102. ]);
  103. Configure::write('Debugger.exportFormatter', TextFormatter::class);
  104. Log::setConfig([
  105. 'debug' => [
  106. 'engine' => FileLog::class,
  107. 'levels' => ['notice', 'info', 'debug'],
  108. 'file' => 'debug',
  109. 'path' => LOGS,
  110. ],
  111. 'error' => [
  112. 'engine' => FileLog::class,
  113. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  114. 'file' => 'error',
  115. 'path' => LOGS,
  116. ],
  117. ]);
  118. Chronos::setTestNow(Chronos::now());
  119. Security::setSalt('a-long-but-not-random-value');
  120. ini_set('intl.default_locale', 'en_US');
  121. ini_set('session.gc_divisor', '1');
  122. ini_set('assert.exception', '1');
  123. // Fixate sessionid early on, as php7.2+
  124. // does not allow the sessionid to be set after stdout
  125. // has been written to.
  126. session_id('cli');
  127. // Create test database schema
  128. if (env('FIXTURE_SCHEMA_METADATA')) {
  129. $loader = new SchemaLoader();
  130. $loader->loadInternalFile(env('FIXTURE_SCHEMA_METADATA'));
  131. }
  132. FactoryLocator::add('Table', new TableLocator());