bootstrap.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. use Cake\Cache\Cache;
  3. use Cake\Core\Configure;
  4. use Cake\Core\Plugin;
  5. use Cake\Datasource\ConnectionManager;
  6. use Cake\Filesystem\Folder;
  7. use Cake\Log\Log;
  8. use Cake\Mailer\Mailer;
  9. use Cake\Mailer\TransportFactory;
  10. use Cake\Routing\Route\DashedRoute;
  11. use Cake\Routing\Router;
  12. use Cake\Utility\Security;
  13. use TestApp\Controller\AppController;
  14. use Tools\Plugin as ToolsPlugin;
  15. if (!defined('DS')) {
  16. define('DS', DIRECTORY_SEPARATOR);
  17. }
  18. if (!defined('WINDOWS')) {
  19. if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') {
  20. define('WINDOWS', true);
  21. } else {
  22. define('WINDOWS', false);
  23. }
  24. }
  25. define('ROOT', dirname(__DIR__));
  26. define('TMP', ROOT . DS . 'tmp' . DS);
  27. define('LOGS', TMP . 'logs' . DS);
  28. define('CACHE', TMP . 'cache' . DS);
  29. define('APP', sys_get_temp_dir());
  30. define('APP_DIR', 'src');
  31. define('CAKE_CORE_INCLUDE_PATH', ROOT . '/vendor/cakephp/cakephp');
  32. define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
  33. define('CAKE', CORE_PATH . APP_DIR . DS);
  34. define('TESTS', ROOT . DS . 'tests' . DS);
  35. define('WWW_ROOT', ROOT . DS . 'webroot' . DS);
  36. define('CONFIG', __DIR__ . DS . 'config' . DS);
  37. define('TEST_FILES', ROOT . DS . 'tests' . DS . 'test_files' . DS);
  38. ini_set('intl.default_locale', 'de_DE');
  39. require_once 'vendor/cakephp/cakephp/src/basics.php';
  40. require_once 'vendor/autoload.php';
  41. Configure::write('App', [
  42. 'namespace' => 'TestApp',
  43. 'encoding' => 'UTF-8',
  44. 'fullBaseUrl' => '//localhost',
  45. 'paths' => [
  46. 'templates' => [
  47. TESTS . 'templates' . DS,
  48. ],
  49. ],
  50. ]);
  51. Configure::write('debug', true);
  52. Configure::write('Config', [
  53. 'adminEmail' => 'test@example.com',
  54. 'adminName' => 'Mark',
  55. ]);
  56. Mailer::setConfig('default', ['transport' => 'Debug']);
  57. TransportFactory::setConfig('Debug', [
  58. 'className' => 'Debug',
  59. ]);
  60. mb_internal_encoding('UTF-8');
  61. $Tmp = new Folder(TMP);
  62. $Tmp->create(TMP . 'cache/models', 0770);
  63. $Tmp->create(TMP . 'cache/persistent', 0770);
  64. $Tmp->create(TMP . 'cache/views', 0770);
  65. $cache = [
  66. 'default' => [
  67. 'engine' => 'File',
  68. 'path' => CACHE,
  69. ],
  70. '_cake_core_' => [
  71. 'className' => 'File',
  72. 'prefix' => 'crud_myapp_cake_core_',
  73. 'path' => CACHE . 'persistent/',
  74. 'serialize' => true,
  75. 'duration' => '+10 seconds',
  76. ],
  77. '_cake_model_' => [
  78. 'className' => 'File',
  79. 'prefix' => 'crud_my_app_cake_model_',
  80. 'path' => CACHE . 'models/',
  81. 'serialize' => 'File',
  82. 'duration' => '+10 seconds',
  83. ],
  84. ];
  85. Cache::setConfig($cache);
  86. Log::setConfig('debug', [
  87. 'className' => 'Cake\Log\Engine\FileLog',
  88. 'path' => LOGS,
  89. 'file' => 'debug',
  90. 'scopes' => false,
  91. 'levels' => ['notice', 'info', 'debug'],
  92. 'url' => env('LOG_DEBUG_URL', null),
  93. ]);
  94. Log::setConfig('error', [
  95. 'className' => 'Cake\Log\Engine\FileLog',
  96. 'path' => LOGS,
  97. 'file' => 'error',
  98. 'scopes' => false,
  99. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  100. 'url' => env('LOG_ERROR_URL', null),
  101. ]);
  102. Security::setSalt('foo');
  103. // Why is this required?
  104. require ROOT . DS . 'config' . DS . 'bootstrap.php';
  105. Router::defaultRouteClass(DashedRoute::class);
  106. class_alias(AppController::class, 'App\Controller\AppController');
  107. Plugin::getCollection()->add(new ToolsPlugin());
  108. if (getenv('db_dsn')) {
  109. ConnectionManager::setConfig('test', [
  110. 'url' => getenv('db_dsn'),
  111. 'timezone' => 'UTC',
  112. 'quoteIdentifiers' => true,
  113. 'cacheMetadata' => true,
  114. ]);
  115. return;
  116. }
  117. // Ensure default test connection is defined
  118. if (!getenv('db_class')) {
  119. putenv('db_dsn=sqlite:///:memory:');
  120. //putenv('db_dsn=postgres://postgres@127.0.0.1/test');
  121. }
  122. ConnectionManager::setConfig('test', [
  123. 'url' => getenv('db_dsn') ?: null,
  124. 'driver' => getenv('db_class') ?: null,
  125. 'database' => getenv('db_database') ?: null,
  126. 'username' => getenv('db_username') ?: null,
  127. 'password' => getenv('db_password') ?: null,
  128. 'timezone' => 'UTC',
  129. 'quoteIdentifiers' => true,
  130. 'cacheMetadata' => true,
  131. ]);