-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoaderTest.php
417 lines (356 loc) · 14.6 KB
/
LoaderTest.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php declare(strict_types=1);
/*
* This file is part of Biurad opensource projects.
*
* @copyright 2022 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Biurad\Loader\Tests;
use Biurad\Loader\Files\Adapters;
use Biurad\Loader\Files\RecursiveUniformResourceIterator;
use Biurad\Loader\Files\UniformResourceIterator;
use Biurad\Loader\Locators;
use PHPUnit\Framework as t;
beforeAll(function (): void {
require_once __DIR__.'/Fixtures/Hello.php';
});
dataset('data_expected', [
'test_array_data' => [
[
'test' => [
'dir' => [
'root' => true,
'lib' => null,
],
'forbidden_file_extensions' => [
'php',
'php3',
'pl',
'com',
'exe',
'bat',
'cgi',
'htaccess',
],
'debugger_token' => 'debug',
],
],
],
]);
dataset('ini_actual', [
'ini_data' => <<<INI
; generated by Biurad\Loader\Files\Adapters\IniFileAdapter
[test]
dir.root = true
dir.lib = ""
forbidden_file_extensions.0 = "php"
forbidden_file_extensions.1 = "php3"
forbidden_file_extensions.2 = "pl"
forbidden_file_extensions.3 = "com"
forbidden_file_extensions.4 = "exe"
forbidden_file_extensions.5 = "bat"
forbidden_file_extensions.6 = "cgi"
forbidden_file_extensions.7 = "htaccess"
debugger_token = "debug"
INI
]);
dataset('json_actual', [
'json_data' => <<<JSON
{
"test": {
"dir": {
"root": true,
"lib": null
},
"forbidden_file_extensions": [
"php",
"php3",
"pl",
"com",
"exe",
"bat",
"cgi",
"htaccess"
],
"debugger_token": "debug"
}
}
JSON
]);
dataset('yaml_actual', [
'yaml_data' => <<<YAML
test:
dir:
root: true
lib: ~
forbidden_file_extensions:
- php
- php3
- pl
- com
- exe
- bat
- cgi
- htaccess
debugger_token: debug
YAML
]);
dataset('neon_actual', [
'neon_data' => <<<NEON
# generated by Biurad\Loader\Files\Adapters\NeonFileAdapter
test:
dir:
root: true
lib: null
forbidden_file_extensions:
- php
- php3
- pl
- com
- exe
- bat
- cgi
- htaccess
debugger_token: debug
NEON
]);
dataset('xml_actual', [
'xml_data' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<config>
<test>
<dir root="true" lib="null"/>
<forbidden_file_extensions>php</forbidden_file_extensions>
<forbidden_file_extensions>php3</forbidden_file_extensions>
<forbidden_file_extensions>pl</forbidden_file_extensions>
<forbidden_file_extensions>com</forbidden_file_extensions>
<forbidden_file_extensions>exe</forbidden_file_extensions>
<forbidden_file_extensions>bat</forbidden_file_extensions>
<forbidden_file_extensions>cgi</forbidden_file_extensions>
<forbidden_file_extensions>htaccess</forbidden_file_extensions>
<debugger_token>debug</debugger_token>
</test>
</config>
XML
]);
dataset('uniform_uri', [
'UniformResourceLocator' => new Locators\UniformResourceLocator(__DIR__.'/Fixtures'),
]);
dataset('resource_paths_add', [
['base', 'base'],
['local', 'local'],
['override', 'override'],
['all', ['override://all', 'local://all', 'base://all']],
]);
dataset('resource_streams', [
['base', ['base']],
['local', ['local']],
['override', ['override']],
['all', ['override://all', 'local://all', 'base://all']],
['fail', []],
]);
dataset('resource_paths', [
['all://base.txt', ['base/all/base.txt']],
['all://base_all.txt', ['override/all/base_all.txt', 'local/all/base_all.txt', 'base/all/base_all.txt']],
['all://base_local.txt', ['local/all/base_local.txt', 'base/all/base_local.txt']],
['all://base_override.txt', ['override/all/base_override.txt', 'base/all/base_override.txt']],
['all://local.txt', ['local/all/local.txt']],
['all://local_override.txt', ['override/all/local_override.txt', 'local/all/local_override.txt']],
['all://override.txt', ['override/all/override.txt']],
['all://asdf/../base.txt', ['base/all/base.txt']],
]);
dataset('normalize_paths', [
['', '/'],
['./', ''],
['././/./', ''],
['././/../', false],
['/', '/'],
['//', '/'],
['///', '/'],
['/././', '/'],
['foo', 'foo'],
['/foo', '/foo'],
['//foo', '/foo'],
['/foo/', '/foo/'],
['//foo//', '/foo/'],
['path/to/file.txt', 'path/to/file.txt'],
['path/to/../file.txt', 'path/file.txt'],
['path/to/../../file.txt', 'file.txt'],
['path/to/../../../file.txt', null],
['/path/to/file.txt', '/path/to/file.txt'],
['/path/to/../file.txt', '/path/file.txt'],
['/path/to/../../file.txt', '/file.txt'],
['/path/to/../../../file.txt', null],
['c:\\', 'c:/'],
['c:\\path\\to\file.txt', 'c:/path/to/file.txt'],
['c:\\path\\to\../file.txt', 'c:/path/file.txt'],
['c:\\path\\to\../../file.txt', 'c:/file.txt'],
['c:\\path\\to\../../../file.txt', null],
['stream://path/to/file.txt', 'stream://path/to/file.txt'],
['stream://path/to/../file.txt', 'stream://path/file.txt'],
['stream://path/to/../../file.txt', 'stream://file.txt'],
['stream://path/to/../../../file.txt', null],
]);
test('if namespace or classes can be aliased', function (): void {
$loader = new Locators\AliasLocator(['Zzz\\' => 'Biurad\\']);
$loader->add(Fixtures\Hello::class, 'SpaceX');
$loader->register();
$hello = new \SpaceX();
t\assertInstanceOf(Fixtures\Hello::class, $hello);
t\assertTrue(\class_exists("Zzz\Loader\Tests\Fixtures\Hello"));
});
test('if classes can be found from a path', function (): void {
$loader = (new Locators\FileLocator([__DIR__.'/../src']))->getClassLocator();
$actual1 = \array_keys(\iterator_to_array($loader->getClasses(Adapters\AbstractAdapter::class)));
$actual2 = \array_keys(\iterator_to_array($loader->getClasses()));
\sort($actual1);
\sort($actual2);
t\assertEquals([
'Biurad\Loader\Files\Adapters\AbstractAdapter',
'Biurad\Loader\Files\Adapters\CsvFileAdapter',
'Biurad\Loader\Files\Adapters\IniFileAdapter',
'Biurad\Loader\Files\Adapters\JsonFileAdapter',
'Biurad\Loader\Files\Adapters\MoFileAdapter',
'Biurad\Loader\Files\Adapters\NeonFileAdapter',
'Biurad\Loader\Files\Adapters\PhpFileAdapter',
'Biurad\Loader\Files\Adapters\XmlFileAdapter',
'Biurad\Loader\Files\Adapters\YamlFileAdapter',
], $actual1);
t\assertEquals([
'Biurad\Loader\Exceptions\FileGeneratingException',
'Biurad\Loader\Exceptions\FileLoadingException',
'Biurad\Loader\Exceptions\FileNotFoundException',
'Biurad\Loader\Exceptions\LoaderException',
'Biurad\Loader\Files\Adapters\AbstractAdapter',
'Biurad\Loader\Files\Adapters\CsvFileAdapter',
'Biurad\Loader\Files\Adapters\IniFileAdapter',
'Biurad\Loader\Files\Adapters\JsonFileAdapter',
'Biurad\Loader\Files\Adapters\MoFileAdapter',
'Biurad\Loader\Files\Adapters\NeonFileAdapter',
'Biurad\Loader\Files\Adapters\PhpFileAdapter',
'Biurad\Loader\Files\Adapters\XmlFileAdapter',
'Biurad\Loader\Files\Adapters\YamlFileAdapter',
'Biurad\Loader\Files\RecursiveUniformResourceIterator',
'Biurad\Loader\Files\UniformResourceIterator',
'Biurad\Loader\Locators\AliasLocator',
'Biurad\Loader\Locators\ClassLocator',
'Biurad\Loader\Locators\ConfigLocator',
'Biurad\Loader\Locators\FileLocator',
'Biurad\Loader\Locators\UniformResourceLocator',
], $actual2);
});
test('if ini can be loaded from string', function (array $expected, string $actual): void {
$ini = new Adapters\IniFileAdapter();
t\assertEquals($expected, $ini->fromString($actual));
})->with('data_expected', 'ini_actual');
test('if ini can be loaded from file', function (array $expected): void {
$ini = new Adapters\IniFileAdapter();
t\assertEquals($expected, $ini->fromFile(__DIR__.'/Fixtures/data/test4.ini'));
$loader = new Locators\ConfigLocator();
t\assertEquals($expected, $loader->loadFile(__DIR__.'/Fixtures/data/test4.ini'));
})->with('data_expected');
test('if ini can be dumped from array to string', function (array $expected, string $actual): void {
$ini = new Adapters\IniFileAdapter();
t\assertEquals($actual, $ini->dump($expected));
})->with('data_expected', 'ini_actual');
test('if json can be loaded from string', function (array $expected, string $actual): void {
$json = new Adapters\JsonFileAdapter();
t\assertEquals($expected, $json->fromString($actual));
})->with('data_expected', 'json_actual');
test('if json can be loaded from file', function (array $expected): void {
$json = new Adapters\JsonFileAdapter();
t\assertEquals($expected, $json->fromFile(__DIR__.'/Fixtures/data/test3.json'));
$loader = new Locators\ConfigLocator();
t\assertEquals($expected, $loader->loadFile(__DIR__.'/Fixtures/data/test3.json'));
})->with('data_expected');
test('if json can be dumped from array to string', function (array $expected, string $actual): void {
$json = new Adapters\JsonFileAdapter();
t\assertEquals($actual, $json->dump($expected));
})->with('data_expected', 'json_actual');
test('if yaml can be loaded from string', function (array $expected, string $actual): void {
$yaml = new Adapters\YamlFileAdapter();
t\assertEquals($expected, $yaml->fromString($actual));
})->with('data_expected', 'yaml_actual');
test('if yaml can be loaded from file', function (array $expected): void {
$yaml = new Adapters\YamlFileAdapter();
t\assertEquals($expected, $yaml->fromFile(__DIR__.'/Fixtures/data/test3.json'));
$loader = new Locators\ConfigLocator();
t\assertEquals($expected, $loader->loadFile(__DIR__.'/Fixtures/data/test2.yaml'));
})->with('data_expected');
test('if yaml can be dumped from array to string', function (array $expected, string $actual): void {
$yaml = new Adapters\YamlFileAdapter();
t\assertEquals(<<<YAML
test:
dir: { root: true, lib: null }
forbidden_file_extensions: [php, php3, pl, com, exe, bat, cgi, htaccess]
debugger_token: debug
YAML, $yaml->dump($expected));
})->with('data_expected', 'neon_actual');
test('if neon can be loaded from string', function (array $expected, string $actual): void {
$neon = new Adapters\NeonFileAdapter();
t\assertEquals($expected, $neon->fromString($actual));
})->with('data_expected', 'neon_actual');
test('if neon can be loaded from file', function (array $expected): void {
$neon = new Adapters\NeonFileAdapter();
t\assertEquals($expected, $neon->fromFile(__DIR__.'/Fixtures/data/test7.neon'));
$loader = new Locators\ConfigLocator();
t\assertEquals($expected, $loader->loadFile(__DIR__.'/Fixtures/data/test7.neon'));
})->with('data_expected');
test('if neon can be dumped from array to string', function (array $expected, string $actual): void {
$neon = new Adapters\NeonFileAdapter();
t\assertEquals($actual, $neon->dump($expected));
})->with('data_expected', 'neon_actual');
test('if xml can be loaded from string', function (array $expected, string $actual): void {
$xml = new Adapters\XmlFileAdapter();
t\assertEquals($expected, $xml->fromString($actual));
})->with('data_expected', 'xml_actual');
test('if xml can be loaded from file', function (array $expected): void {
$xml = new Adapters\XmlFileAdapter();
t\assertEquals($expected, $xml->fromFile(__DIR__.'/Fixtures/data/test5.xml'));
$loader = new Locators\ConfigLocator();
t\assertEquals($expected, $loader->loadFile(__DIR__.'/Fixtures/data/test5.xml'));
})->with('data_expected');
test('if xml can be dumped from array to string', function (array $expected, string $actual): void {
$xml = new Adapters\XmlFileAdapter();
t\assertEquals($actual, $xml->dump($expected));
})->with('data_expected', 'xml_actual');
test('if resource locator base path exists', function (Locators\UniformResourceLocator $u): void {
t\assertEquals(__DIR__.'/Fixtures', $u->getBase());
})->with('uniform_uri');
test('if resource locator paths can be streamed', function (Locators\UniformResourceLocator $u, string $scheme, string|array $lookup): void {
t\assertFalse($u->schemeExists($scheme));
$u->addPath($scheme, $lookup);
t\assertTrue($u->schemeExists($scheme));
})->with('uniform_uri', 'resource_paths_add');
test('if resource locator has stream paths', function (Locators\UniformResourceLocator $u): void {
t\assertFalse($u->schemeExists('foo'));
t\assertFalse($u->schemeExists('file'));
t\assertEquals(['base', 'local', 'override', 'all'], $u->getSchemes());
})->with('uniform_uri');
test('if resource locator paths exists in stream', function (Locators\UniformResourceLocator $u, string $scheme, array $expected): void {
t\assertSame($expected, $u->getPaths($scheme));
})->with('uniform_uri', 'resource_streams');
test('if resource locator stream can be iterated', function (Locators\UniformResourceLocator $u): void {
t\assertInstanceOf(UniformResourceIterator::class, $a = $u->getIterator('all://'));
t\assertInstanceOf(RecursiveUniformResourceIterator::class, $b = $u->getRecursiveIterator('all://'));
t\assertSame('file', $a->getType());
t\assertNotEmpty($a->getUrl());
t\assertFalse($b->hasChildren());
t\assertCount(7, $a);
t\assertCount(\count(\iterator_to_array($b)), \iterator_to_array($a));
})->with('uniform_uri');
test('if resource locator can find resources', function (Locators\UniformResourceLocator $u, string $uri, array $paths): void {
t\assertEquals($paths, $u->findResources($uri, false));
})->with('uniform_uri', 'resource_paths');
test('if resource locator can find individual resource', function (Locators\UniformResourceLocator $u, string $uri, array $paths): void {
$path = $paths ? \reset($paths) : false;
$fullPath = !$path ? false : __DIR__."/Fixtures/{$path}";
$this->assertEquals(\str_replace('\\', '/', $fullPath), $u->findResource($uri));
$this->assertEquals(\str_replace('\\', '/', $path), $u->findResource($uri, false));
})->with('uniform_uri', 'resource_paths');
test('if resource locator can normalize paths', function (Locators\UniformResourceLocator $u, string $uri, ?string $path): void {
t\assertEquals($path, $u->normalize($uri));
})->with('uniform_uri', 'normalize_paths');