forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoloaderTest.php
More file actions
371 lines (290 loc) · 11.6 KB
/
AutoloaderTest.php
File metadata and controls
371 lines (290 loc) · 11.6 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
367
368
369
370
371
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Autoloader;
use App\Controllers\Home;
use Closure;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use Config\Autoload;
use Config\Modules;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use UnnamespacedClass;
/**
* @internal
*/
#[Group('Others')]
final class AutoloaderTest extends CIUnitTestCase
{
use ReflectionHelper;
private Autoloader $loader;
/**
* @var Closure(string): (false|string)
*/
private Closure $classLoader;
protected function setUp(): void
{
parent::setUp();
$config = new Autoload();
$modules = new Modules();
$modules->discoverInComposer = false;
$config->classmap = [
'UnnamespacedClass' => SUPPORTPATH . 'Autoloader/UnnamespacedClass.php',
'OtherClass' => APPPATH . 'Controllers/Home.php',
'Name\Spaced\Class' => APPPATH . 'Controllers/Home.php',
];
$config->psr4 = [
'App' => APPPATH,
'CodeIgniter' => SYSTEMPATH,
];
$this->loader = new Autoloader();
$this->loader->initialize($config, $modules)->register();
$this->classLoader = self::getPrivateMethodInvoker($this->loader, 'loadInNamespace');
}
protected function tearDown(): void
{
parent::tearDown();
$this->loader->unregister();
}
public function testLoadStoredClass(): void
{
$this->assertInstanceOf('UnnamespacedClass', new UnnamespacedClass());
}
public function testInitializeWithInvalidArguments(): void
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage("Config array must contain either the 'psr4' key or the 'classmap' key.");
$config = new Autoload();
$config->classmap = [];
$config->psr4 = [];
$modules = new Modules();
$modules->discoverInComposer = false;
(new Autoloader())->initialize($config, $modules);
}
public function testInitializeTwice(): void
{
$loader = new Autoloader();
$loader->initialize(new Autoload(), new Modules());
$ns = $loader->getNamespace();
$this->assertCount(1, $ns['App']);
$this->assertSame('ROOTPATH/app', clean_path($ns['App'][0]));
$loader->initialize(new Autoload(), new Modules());
$ns = $loader->getNamespace();
$this->assertCount(1, $ns['App']);
$this->assertSame('ROOTPATH/app', clean_path($ns['App'][0]));
}
public function testServiceAutoLoaderFromShareInstances(): void
{
$classLoader = self::getPrivateMethodInvoker(service('autoloader'), 'loadInNamespace');
// look for Home controller, as that should be in base repo
$actual = $classLoader(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$resolvedPath = realpath($actual);
$actual = $resolvedPath !== false ? $resolvedPath : $actual;
$this->assertSame($expected, $actual);
}
public function testServiceAutoLoader(): void
{
$autoloader = service('autoloader', false);
$autoloader->initialize(new Autoload(), new Modules());
$autoloader->register();
$classLoader = self::getPrivateMethodInvoker($autoloader, 'loadInNamespace');
// look for Home controller, as that should be in base repo
$actual = $classLoader(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$resolvedPath = realpath($actual);
$actual = $resolvedPath !== false ? $resolvedPath : $actual;
$this->assertSame($expected, $actual);
$autoloader->unregister();
}
public function testExistingFile(): void
{
$actual = ($this->classLoader)(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$this->assertSame($expected, $actual);
$actual = ($this->classLoader)('CodeIgniter\Helpers\array_helper');
$expected = SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR . 'array_helper.php';
$this->assertSame($expected, $actual);
}
public function testMatchesWithPrecedingSlash(): void
{
$actual = ($this->classLoader)(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$this->assertSame($expected, $actual);
}
public function testMissingFile(): void
{
$this->assertFalse(($this->classLoader)('\App\Missing\Classname'));
}
public function testAddNamespaceWorks(): void
{
$this->assertFalse(($this->classLoader)('My\App\Class'));
$this->loader->addNamespace('My\App', __DIR__);
$actual = ($this->classLoader)('My\App\AutoloaderTest');
$expected = __FILE__;
$this->assertSame($expected, $actual);
}
public function testAddNamespaceMultiplePathsWorks(): void
{
$this->loader->addNamespace([
'My\App' => [
APPPATH . 'Config',
__DIR__,
],
]);
$actual = ($this->classLoader)('My\App\App');
$expected = APPPATH . 'Config' . DIRECTORY_SEPARATOR . 'App.php';
$this->assertSame($expected, $actual);
$actual = ($this->classLoader)('My\App\AutoloaderTest');
$expected = __FILE__;
$this->assertSame($expected, $actual);
}
public function testAddNamespaceStringToArray(): void
{
$this->loader->addNamespace('App\Controllers', __DIR__);
$this->assertSame(
__FILE__,
($this->classLoader)('App\Controllers\AutoloaderTest'),
);
}
public function testGetNamespaceGivesArray(): void
{
$this->assertSame([
'App' => [APPPATH],
'CodeIgniter' => [SYSTEMPATH],
], $this->loader->getNamespace());
$this->assertSame([SYSTEMPATH], $this->loader->getNamespace('CodeIgniter'));
$this->assertSame([], $this->loader->getNamespace('Foo'));
}
public function testRemoveNamespace(): void
{
$this->loader->addNamespace('My\App', __DIR__);
$this->assertSame(__FILE__, ($this->classLoader)('My\App\AutoloaderTest'));
$this->loader->removeNamespace('My\App');
$this->assertFalse(($this->classLoader)('My\App\AutoloaderTest'));
}
public function testloadClassNonNamespaced(): void
{
$this->assertFalse(($this->classLoader)('Modules'));
}
public function testFindsComposerRoutes(): void
{
$config = new Autoload();
$modules = new Modules();
$modules->discoverInComposer = true;
$loader = new Autoloader();
$loader->initialize($config, $modules);
$namespaces = $loader->getNamespace();
$this->assertArrayHasKey('Laminas\\Escaper', $namespaces);
}
public function testComposerNamespaceDoesNotOverwriteConfigAutoloadPsr4(): void
{
$config = new Autoload();
$config->psr4 = [
'Psr\Log' => '/Config/Autoload/Psr/Log/',
];
$modules = new Modules();
$modules->discoverInComposer = true;
$loader = new Autoloader();
$loader->initialize($config, $modules);
$namespaces = $loader->getNamespace();
$this->assertSame('/Config/Autoload/Psr/Log/', $namespaces['Psr\Log'][0]);
$this->assertStringContainsString(VENDORPATH, $namespaces['Psr\Log'][1]);
}
public function testComposerPackagesOnly(): void
{
$config = new Autoload();
$config->psr4 = [];
$modules = new Modules();
$modules->discoverInComposer = true;
$modules->composerPackages = ['only' => ['laminas/laminas-escaper']];
$loader = new Autoloader();
$loader->initialize($config, $modules);
$namespaces = $loader->getNamespace();
$this->assertCount(1, $namespaces);
$this->assertStringContainsString(VENDORPATH, $namespaces['Laminas\Escaper'][0]);
}
public function testComposerPackagesExclude(): void
{
$config = new Autoload();
$config->psr4 = [];
$modules = new Modules();
$modules->discoverInComposer = true;
$modules->composerPackages = [
'exclude' => [
'psr/log',
'laminas/laminas-escaper',
],
];
$loader = new Autoloader();
$loader->initialize($config, $modules);
$namespaces = $loader->getNamespace();
$this->assertArrayNotHasKey('Psr\Log', $namespaces);
$this->assertArrayNotHasKey('Laminas\\Escaper', $namespaces);
}
public function testComposerPackagesOnlyAndExclude(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Cannot use "only" and "exclude" at the same time in "Config\Modules::$composerPackages".');
$config = new Autoload();
$config->psr4 = [];
$modules = new Modules();
$modules->discoverInComposer = true;
$modules->composerPackages = [
'only' => ['laminas/laminas-escaper'],
'exclude' => ['psr/log'],
];
$loader = new Autoloader();
$loader->initialize($config, $modules);
}
public function testFindsComposerRoutesWithComposerPathNotFound(): void
{
$composerPath = COMPOSER_PATH;
$config = new Autoload();
$modules = new Modules();
$modules->discoverInComposer = true;
$loader = new Autoloader();
rename(COMPOSER_PATH, COMPOSER_PATH . '.backup');
$loader->initialize($config, $modules);
rename(COMPOSER_PATH . '.backup', $composerPath);
$namespaces = $loader->getNamespace();
$this->assertArrayNotHasKey('Laminas\\Escaper', $namespaces);
}
public function testAutoloaderLoadsNonClassFiles(): void
{
$config = new Autoload();
$config->files[] = SUPPORTPATH . 'Autoloader/functions.php';
$loader = new Autoloader();
$loader->initialize($config, new Modules());
$loader->register();
$this->assertTrue(function_exists('autoload_foo'));
$this->assertSame('I am autoloaded by Autoloader through $files!', autoload_foo());
$this->assertTrue(defined('AUTOLOAD_CONSTANT')); // @phpstan-ignore method.alreadyNarrowedType
$this->assertSame('foo', AUTOLOAD_CONSTANT);
$loader->unregister();
}
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
#[WithoutErrorHandler]
public function testLoadHelpers(): void
{
$config = new Autoload();
$config->helpers[] = 'form';
$loader = new Autoloader();
$loader->initialize($config, new Modules());
$loader->loadHelpers();
$this->assertTrue(function_exists('form_open'));
$loader->unregister();
}
}