-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.php
More file actions
366 lines (325 loc) · 11.8 KB
/
boot.php
File metadata and controls
366 lines (325 loc) · 11.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
// Bootstrap file of all tests.
// $ vendor/bin/phpunit --verbose --colors --bootstrap=./boot.php ./
// $ vendor/bin/phpunit --verbose --colors --bootstrap=./boot.php ./acl/
if (PHP_SAPI !== 'cli') {
echo 'This file must be run via CLI only!', PHP_EOL;
exit(1);
}
// @important
date_default_timezone_set('UTC');
// For non-local environments.
function is_file_loaded($file) {
return in_array($file, get_included_files());
}
// Path to "vendor/froq" folder.
$froqDir = __DIR__ . '/vendor/froq';
// Froq loader.
$froqLoader = $froqDir . '/froq/src/Autoloader.php';
if (is_file($froqLoader) && !is_file_loaded($froqLoader)) {
require $froqLoader;
$loader = froq\Autoloader::init($froqDir);
$loader->register();
}
// Froq sugars.
$froqSugars = $froqDir . '/froq-util/src/sugars.php';
if (is_file($froqSugars) && !is_file_loaded($froqSugars)) {
require $froqSugars;
}
// Composer loader.
$composerLoader = __DIR__ . '/vendor/autoload.php';
if (is_file($composerLoader) && !is_file_loaded($composerLoader)) {
require $composerLoader;
}
// @cancel: Not needed yet.
// if (is_file($composerLoader)) {
// $loader->addPsr4('froq\\acl\\', __DIR__ . '/../src/');
// $composerJson = file_get_contents(__DIR__ . '/../composer.json');
// $composerData = json_decode($composerJson, true);
// // Load deps.
// foreach ($composerData['require'] as $package => $_) {
// if (substr($package, 0, 5) === 'froq/') {
// $package = substr($package, 5);
// $packagePrefix = strtr($package, '-', '\\') . '\\';
// $packageSource = $froqDir . $package . '/src/';
// $loader->addPsr4($packagePrefix, $packageSource);
// }
// }
// // Load files.
// if (isset($composerData['autoload']['files'])) {
// foreach ($composerData['autoload']['files'] as $file) {
// require $file;
// }
// }
// }
// // Apply exclusions.
// $excludeList = new PHPUnit\Util\ExcludeList();
// // Must be re-write here (as Composer does).
// $phpunitBin = realpath(__DIR__ . '/vendor/bin/phpunit');
// $phpunitDir = realpath(dirname($phpunitBin) . '/../phpunit/phpunit');
// $excludeList->addDirectory($phpunitDir);
// // Drop Froq! dir from trace stack (as Composer does).
// foreach (glob(__DIR__ . '/vendor/*') as $item) {
// $item = realpath($item);
// if (is_dir($item)) foreach (glob($item . '/*') as $item) {
// if (is_dir($item)) $excludeList->addDirectory(realpath($item));
// }
// }
// // Drop this file from trace stack (as Composer does in bin/phpunit file).
// $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'][] = __FILE__;
// Base test case for all test classes.
abstract class TestCase extends PHPUnit\Framework\TestCase
{
function __construct()
{
parent::__construct();
if (method_exists($this, 'init')) {
$this->init();
}
}
function __destruct()
{
if (method_exists($this, 'dinit')) {
$this->dinit();
}
}
static function assertLength(int $length, string $string, string $message = ''): void
{
self::assert(
strlen($string) === $length,
'Failed asserting that length of `%s`, `%d` is identical to `%d`.',
[$string, strlen($string), $length], $message
);
}
// static function assertEqual(mixed $expected, mixed $actual, string $message = ''): void
// {
// $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'][] = __FILE__;
// try {
// self::assertEquals($expected, $actual, $message);
// } catch (\Throwable $error) {
// throw $error;
// array_delete($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'], __FILE__);
// }
// self::okay();
// }
// static function assertNotEqual(mixed $expected, mixed $actual, string $message = ''): void
// {
// $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'][] = __FILE__;
// try {
// self::assertNotEquals($expected, $actual, $message);
// } catch (\Throwable $error) {
// throw $error;
// array_delete($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'], __FILE__);
// }
// self::okay();
// }
static function assertStringContains(string $needle, string $haystack, bool $icase = false, string $message = ''): void
{
self::assert(
str_has($haystack, $needle, $icase) === true,
'Failed asserting that string `%s` contains `%s`',
[$haystack, $needle], $message
);
}
static function assertStringNotContains(string $needle, string $haystack, bool $icase = false, string $message = ''): void
{
self::assert(
str_has($haystack, $needle, $icase) === false,
'Failed asserting that string `%s` not contains `%s`',
[$haystack, $needle], $message
);
}
static function assertMatches(string $pattern, string $subject, string $message = ''): void
{
self::assert(
preg_test($pattern, $subject) === true,
'Failed asserting that subject `%s` matches pattern `%s`.',
[$subject, $pattern], $message
);
}
static function assertNotMatches(string $pattern, string $subject, string $message = ''): void
{
self::assert(
preg_test($pattern, $subject) === false,
'Failed asserting that subject `%s` not matches pattern `%s`.',
[$subject, $pattern], $message
);
}
static function assertTypeOf(string $type, mixed $input, string $message = ''): void
{
self::assert(
is_type_of($input, $type) === true,
'Failed asserting that `%t` is type of `%s`',
[$input, $type], $message
);
}
static function assertNotTypeOf(string $type, mixed $input, string $message = ''): void
{
self::assert(
is_type_of($input, $type) === false,
'Failed asserting that `%t` is not type of `%s`',
[$input, $type], $message
);
}
static function assertClassOf(string|object|array $class, string|object $subclass, string $message = ''): void
{
self::assert(
is_class_of(
$subclass = get_class_name($subclass),
...($class = is_array($class) ? $class : [$class])
) === true,
'Failed asserting that `%s` is class of `%A`.',
[$subclass, $class], $message
);
}
static function assertNotClassOf(string|object|array $class, string|object $subclass, string $message = ''): void
{
self::assert(
is_class_of(
$subclass = get_class_name($subclass),
...($class = is_array($class) ? $class : [$class])
) === false,
'Failed asserting that `%s` is class of `%A`.',
[$subclass, $class], $message
);
}
static function assertSubclassOf(string|object $class, string|object $subclass, string $message = ''): void
{
self::assert(
is_subclass_of(
$subclass = get_class_name($subclass),
$class = get_class_name($class)
) === true,
'Failed asserting that `%s` is subclass of `%s`',
[$subclass, $class], $message
);
}
static function assertNotSubclassOf(string|object $class, string|object $subclass, string $message = ''): void
{
self::assert(
is_subclass_of(
$subclass = get_class_name($subclass),
$class = get_class_name($class)
) === false,
'Failed asserting that `%s` is not subclass of `%s`',
[$subclass, $class], $message
);
}
/** @override */
static function assertFileNotExists(string $file, string $message = ''): void
{
self::assert(
file_exists($file) === false,
'Failed asserting that file `%s` not exists.',
[$file], $message
);
}
// function assertDirectoryNotExists() {}
static function assertClassExists(string $class, string $message = ''): void
{
self::assert(
class_exists($class) === true,
'Failed asserting that class `%s` exists.',
[$class], $message
);
}
static function assertClassNotExists(string $class, string $message = ''): void
{
self::assert(
class_exists($class) === false,
'Failed asserting that class `%s` not exists.',
[$class], $message
);
}
static function assertClassMethodExists(string $class, string $method, string $message = ''): void
{
self::assert(
method_exists($class, $method) === true,
'Failed asserting that class method `%s::%s` exists.',
[$class, $method], $message
);
}
static function assertClassMethodNotExists(string $class, string $method, string $message = ''): void
{
self::assert(
method_exists($class, $method) === false,
'Failed asserting that class method `%s::%s` not exists.',
[$class, $method], $message
);
}
static function assertClassConstantExists(string $class, string $constant, string $message = ''): void
{
self::assert(
constant_exists($class, $constant) === true,
'Failed asserting that class constant `%s::%s` exists.',
[$class, $constant], $message
);
}
static function assertClassConstantNotExists(string $class, string $constant, string $message = ''): void
{
self::assert(
constant_exists($class, $constant) === false,
'Failed asserting that class constant `%s::%s` not exists.',
[$class, $constant], $message
);
}
static function assertClassPropertyExists(string $class, string $property, string $message = ''): void
{
self::assert(
property_exists($class, $property) === true,
'Failed asserting that class property `%s::%s` exists.',
[$class, $property], $message
);
}
static function assertClassPropertyNotExists(string $class, string $property, string $message = ''): void
{
self::assert(
property_exists($class, $property) === false,
'Failed asserting that class property `%s::%s` not exists.',
[$class, $property], $message
);
}
static function assertFunctionExists(string $function, string $message = ''): void
{
self::assert(
function_exists($function) === true,
'Failed asserting that function `%s` exists.',
[$function], $message
);
}
static function assertFunctionNotExists(string $function, string $message = ''): void
{
self::assert(
function_exists($function) === false,
'Failed asserting that function `%s` not exists.',
[$function], $message
);
}
static function assert(bool $assertion, string $format, array $formatArgs, string $message): void
{
if (!$assertion) {
$message && $message .= PHP_EOL;
$message .= format($format, ...$formatArgs);
self::fail($message);
}
self::okay();
}
/** Faking "This test did not perform any assertions" error. */
static function okay(): void
{
self::assertTrue(true);
}
protected mixed $util = null;
/** Get an etc file constents. */
protected function etc(string $file): mixed
{
$file = sprintf('%s/.etc/%s.php', __DIR__, $file);
// $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'][] = $file;
return require $file;
}
/** Get an etc/util file constents. */
protected function util(string $name): mixed
{
return $this->etc('util/' . $name . '-util');
}
}