/
plugin_bootstrap.php
215 lines (181 loc) · 6.33 KB
/
plugin_bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// @codingStandardsIgnoreFile
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Datasource\ConnectionManager;
use Cake\Error\ErrorTrap;
use Cake\Error\ExceptionTrap;
use Cake\Filesystem\Folder;
use Cake\Http\ServerRequest;
use Cake\Log\Log;
use Cake\Mailer\Email;
use Cake\Mailer\TransportFactory;
use Cake\Utility\Security;
use Migrations\TestSuite\Migrator;
$findRoot = function ($root) {
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);
throw new Exception("Cannot find the root of the application, unable to run tests");
};
if (!defined('PLUGIN_ROOT')) {
define('PLUGIN_ROOT', $findRoot(getcwd()));
}
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* Configure paths required to find CakePHP + general filepath
* constants
*/
require __DIR__ . DS . 'test_app' . DS . 'config' . DS . 'paths.php';
/**
* Bootstrap CakePHP.
*
* Does the various bits of setup that CakePHP needs to do.
* This includes:
*
* - Registering the CakePHP autoloader.
* - Setting the default application paths.
*/
require CORE_PATH . 'config' . DS . 'bootstrap.php';
// Use composer to load the autoloader.
require PLUGIN_ROOT . DS . 'vendor' . DS . 'autoload.php';
$envFile = null;
if (\file_exists(TESTS . '.env')) {
$envFile = TESTS . '.env';
} elseif (\file_exists(TESTS . '.env.test')) {
$envFile = TESTS . '.env.test';
} elseif (\file_exists(CONFIG . '.env')) {
$envFile = CONFIG . '.env';
} elseif (\file_exists(CONFIG . '.env.test')) {
$envFile = CONFIG . '.env.test';
}
if ($envFile) {
$dotenv = new \Symfony\Component\Dotenv\Dotenv();
$dotenv->load($envFile);
}
/*
* Read configuration file and inject configuration into various
* CakePHP classes.
*
* By default there is only one configuration file. It is often a good
* idea to create multiple configuration files, and separate the configuration
* that changes from configuration that does not. This makes deployment simpler.
*/
try {
Configure::config('default', new PhpConfig());
Configure::load('app', 'default', false);
} catch (\Exception $e) {
exit($e->getMessage() . "\n");
}
/*
* When debug = true the metadata cache should only last
* for a short time.
*/
if (Configure::read('debug')) {
Configure::write('Cache._cake_model_.duration', '+2 minutes');
Configure::write('Cache._cake_core_.duration', '+2 minutes');
}
date_default_timezone_set('UTC');
/*
* Register application error and exception handlers.
*/
(new ErrorTrap(Configure::read('Error')))->register();
(new ExceptionTrap(Configure::read('Error')))->register();
/*
* Include the CLI bootstrap overrides.
*/
if (PHP_SAPI === 'cli') {
/**
* Additional bootstrapping and configuration for CLI environments should
* be put here.
*/
// Commented out as it is set above this.
// Set the fullBaseUrl to allow URLs to be generated in shell tasks.
// This is useful when sending email from shells.
//Configure::write('App.fullBaseUrl', php_uname('n'));
// Set logs to different files so they don't have permission conflicts.
Configure::write('Log.debug.file', 'cli-debug');
Configure::write('Log.error.file', 'cli-error');
}
Configure::write('debug', true);
$tmpDirectory = new Folder(TMP);
$tmpDirectory->delete(TMP . 'cache');
$tmpDirectory->create(TMP . 'cache/models', 0777);
$tmpDirectory->create(TMP . 'cache/persistent', 0777);
$tmpDirectory->create(TMP . 'cache/views', 0777);
Cache::setConfig(Configure::consume('Cache'));
//ConnectionManager::setConfig(Configure::consume('Datasources'));
TransportFactory::setConfig(Configure::consume('EmailTransport'));
Email::setConfig(Configure::consume('Email'));
Log::setConfig(Configure::consume('Log'));
/** @var string $salt */
$salt = Configure::consume('Security.salt');
Security::setSalt($salt);
/*
* Setup detectors for mobile and tablet.
*/
ServerRequest::addDetector('mobile', function ($request) {
$detector = new \Detection\MobileDetect();
return $detector->isMobile();
});
ServerRequest::addDetector('tablet', function ($request) {
$detector = new \Detection\MobileDetect();
return $detector->isTablet();
});
// There is no time-specific type in Cake
\Cake\Database\TypeFactory::map('time', \Cake\Database\Type\TimeType::class);
\Cake\Database\TypeFactory::map('date', \Cake\Database\Type\DateType::class);
\Cake\Database\TypeFactory::map('datetime', \Cake\Database\Type\DateTimeType::class);
\Cake\Database\TypeFactory::map('timestamp', \Cake\Database\Type\DateTimeType::class);
/*
* Enable immutable time objects in the ORM.
*
* You can enable default locale format parsing by adding calls
* to `useLocaleParser()`. This enables the automatic conversion of
* locale specific date formats. For details see
* @link https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data
*/
/** @var \Cake\Database\Type\TimeType $timeType */
$timeType = \Cake\Database\TypeFactory::build('time');
$timeType->useLocaleParser();
/** @var \Cake\Database\Type\DateType $dateType */
$dateType = \Cake\Database\TypeFactory::build('date');
$dateType->useLocaleParser();
/** @var \Cake\Database\Type\DateTimeType $dateTimeType */
$dateTimeType = \Cake\Database\TypeFactory::build('datetime');
$dateTimeType->useLocaleParser();
/** @var \Cake\Database\Type\DateTimeType $timestampType */
$timestampType = \Cake\Database\TypeFactory::build('timestamp');
$timestampType->useLocaleParser();
// Ensure default test connection is defined
if (!getenv('db_class')) {
putenv('db_class=Cake\Database\Driver\Sqlite');
putenv('db_database=:memory:');
}
$dbconfig = [
'className' => \Cake\Database\Connection::class,
'driver' => getenv('db_class'),
'database' => getenv('db_database'),
'timezone' => 'UTC'
];
if (Configure::check('Tests.DbConfig')) {
$dbconfig = Configure::read('Tests.DbConfig');
}
ConnectionManager::setConfig('default', $dbconfig);
ConnectionManager::setConfig('test', $dbconfig);
// Migrations
if (Configure::check('Tests.Migrations')) {
$migrations = Configure::read('Tests.Migrations');
$migrator = new Migrator();
$migrator->runMany($migrations);
}