bootstrap.php 4.1 KB

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