‪TYPO3CMS  ‪main
ClassLoadingInformationGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Composer\Autoload\ClassLoader;
21 use PHPUnit\Framework\Attributes\DataProvider;
22 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\CMS\Core\Core\ClassLoadingInformationGenerator;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪ClassLoadingInformationGeneratorTest extends UnitTestCase
31 {
32  #[Test]
34  {
35  $this->expectException(Exception::class);
36  $this->expectExceptionCode(1444142481);
37 
38  $packageMock = $this->‪createPackageMock([
39  'extra' => [
40  'typo3/class-alias-loader' => [
41  'class-alias-maps' => [
42  'foo' => TestFile::class,
43  'bar' => AnotherTestFile::class,
44  ],
45  ],
46  ],
47  ]);
48  $classLoaderMock = $this->createMock(ClassLoader::class);
49  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
50  $generator->buildClassAliasMapForPackage($packageMock);
51  }
52 
53  #[Test]
55  {
56  $this->expectException(Exception::class);
57  $this->expectExceptionCode(1422625075);
58 
59  $packageMock = $this->‪createPackageMock([
60  'extra' => [
61  'typo3/class-alias-loader' => [
62  'class-alias-maps' => [
63  'Migrations/Code/WrongClassAliasMap.php',
64  ],
65  ],
66  ],
67  ]);
68  $classLoaderMock = $this->createMock(ClassLoader::class);
69  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
70  $generator->buildClassAliasMapForPackage($packageMock);
71  }
72 
73  #[Test]
75  {
76  $expectedClassMap = [
77  'aliasToClassNameMapping' => [
78  'foo' => TestFile::class,
79  'bar' => AnotherTestFile::class,
80  ],
81  'classNameToAliasMapping' => [
82  TestFile::class => [
83  'foo' => 'foo',
84  ],
85  AnotherTestFile::class => [
86  'bar' => 'bar',
87  ],
88  ],
89  ];
90  $packageMock = $this->‪createPackageMock([]);
91  $classLoaderMock = $this->createMock(ClassLoader::class);
92  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
93  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
94  }
95 
96  #[Test]
98  {
99  $expectedClassMap = [
100  'aliasToClassNameMapping' => [
101  'foo' => TestFile::class,
102  'bar' => AnotherTestFile::class,
103  ],
104  'classNameToAliasMapping' => [
105  TestFile::class => [
106  'foo' => 'foo',
107  ],
108  AnotherTestFile::class => [
109  'bar' => 'bar',
110  ],
111  ],
112  ];
113  $packageMock = $this->‪createPackageMock([
114  'extra' => [
115  'typo3/class-alias-loader' => [
116  'class-alias-maps' => [
117  'Resources/PHP/ClassAliasMap.php',
118  ],
119  ],
120  ],
121  ]);
122  $classLoaderMock = $this->createMock(ClassLoader::class);
123  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
124  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
125  }
126 
127  public static function ‪autoloadFilesAreBuildCorrectlyDataProvider(): array
128  {
129  return [
130  'Psr-4 section' => [
131  [
132  'autoload' => [
133  'psr-4' => [
134  'TYPO3\\CMS\\TestExtension\\' => 'Classes/',
135  ],
136  ],
137  ],
138  [
139  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
140  ],
141  [],
142  ],
143  'Psr-4 section with array' => [
144  [
145  'autoload' => [
146  'psr-4' => [
147  'TYPO3\\CMS\\TestExtension\\' => ['Classes/', 'Resources/PHP/'],
148  ],
149  ],
150  ],
151  [
152  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\',$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP\')',
153  ],
154  [],
155  ],
156  'Psr-4 section without trailing slash' => [
157  [
158  'autoload' => [
159  'psr-4' => [
160  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
161  ],
162  ],
163  ],
164  [
165  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
166  ],
167  [],
168  ],
169  'Classmap section' => [
170  [
171  'autoload' => [
172  'classmap' => [
173  'Resources/PHP/',
174  ],
175  ],
176  ],
177  [],
178  [
179  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
180  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
181  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
182  ],
183  ],
184  'Classmap section without trailing slash' => [
185  [
186  'autoload' => [
187  'classmap' => [
188  'Resources/PHP',
189  ],
190  ],
191  ],
192  [],
193  [
194  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
195  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
196  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
197  ],
198  ],
199  'Classmap section pointing to a file' => [
200  [
201  'autoload' => [
202  'classmap' => [
203  'Resources/PHP/TestFile.php',
204  ],
205  ],
206  ],
207  [],
208  [
209  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
210  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
211  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
212  ],
213  ],
214  'No autoload section' => [
215  [],
216  [],
217  [
218  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
219  '!$typo3InstallDir . \'/Fixtures/test_extension/Tests/TestClass.php\'',
220  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
221  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
222  '!$typo3InstallDir . \'/Fixtures/test_extension/class.ext_update.php\'',
223  ],
224  ],
225  'Classmap section pointing to two files' => [
226  [
227  'autoload' => [
228  'classmap' => [
229  'Resources/PHP/TestFile.php',
230  'Resources/PHP/AnotherTestFile.php',
231  ],
232  ],
233  ],
234  [],
235  [
236  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
237  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
238  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
239  ],
240  ],
241  ];
242  }
243 
244  #[DataProvider('autoloadFilesAreBuildCorrectlyDataProvider')]
245  #[Test]
246  public function ‪autoloadFilesAreBuildCorrectly(array $packageManifest, array $expectedPsr4Files, array $expectedClassMapFiles): void
247  {
248  $classLoaderMock = $this->createMock(ClassLoader::class);
249  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__);
250  $files = $generator->buildAutoloadInformationFiles();
251 
252  self::assertArrayHasKey('psr-4File', $files);
253  self::assertArrayHasKey('classMapFile', $files);
254  foreach ($expectedPsr4Files as $expectation) {
255  if ($expectation[0] === '!') {
256  $expectedCount = 0;
257  $expectation = substr($expectation, 1);
258  $message = sprintf('File "%s" is NOT expected to be in psr-4, but is.', $expectation);
259  } else {
260  $expectedCount = 1;
261  $message = sprintf('File "%s" is expected to be in psr-4, but is not.', $expectation);
262  }
263  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), $message);
264  }
265  foreach ($expectedClassMapFiles as $expectation) {
266  if ($expectation[0] === '!') {
267  $expectedCount = 0;
268  $expectation = substr($expectation, 1);
269  $message = sprintf('File "%s" is NOT expected to be in class map, but is.', $expectation);
270  } else {
271  $expectedCount = 1;
272  $message = sprintf('File "%s" is expected to be in class map, but is not.', $expectation);
273  }
274  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), $message);
275  }
276  }
277 
278  public static function ‪autoloadDevFilesAreBuildCorrectlyDataProvider(): array
279  {
280  return [
281  'Psr-4 sections' => [
282  [
283  'autoload' => [
284  'psr-4' => [
285  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
286  ],
287  ],
288  'autoload-dev' => [
289  'psr-4' => [
290  'TYPO3\\CMS\\TestExtension\\Tests\\' => 'Tests',
291  ],
292  ],
293  ],
294  [
295  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
296  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\Tests\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
297  ],
298  [],
299  ],
300  'Psr-4 sections with override' => [
301  [
302  'autoload' => [
303  'psr-4' => [
304  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
305  ],
306  ],
307  'autoload-dev' => [
308  'psr-4' => [
309  'TYPO3\\CMS\\TestExtension\\' => 'Tests',
310  ],
311  ],
312  ],
313  [
314  '!\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
315  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
316  ],
317  [],
318  ],
319  'Classmap section pointing to two files, one in dev and one not' => [
320  [
321  'autoload' => [
322  'classmap' => [
323  'Resources/PHP/TestFile.php',
324  ],
325  ],
326  'autoload-dev' => [
327  'classmap' => [
328  'Resources/PHP/AnotherTestFile.php',
329  ],
330  ],
331  ],
332  [],
333  [
334  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/TestFile.php\'',
335  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
336  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTestFile.php\'',
337  ],
338  ],
339  ];
340  }
341 
342  #[DataProvider('autoloadDevFilesAreBuildCorrectlyDataProvider')]
343  #[Test]
344  public function ‪autoloadDevFilesAreBuildCorrectly(array $packageManifest, array $expectedPsr4Files, array $expectedClassMapFiles): void
345  {
346  $classLoaderMock = $this->createMock(ClassLoader::class);
347  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__, true);
348  $files = $generator->buildAutoloadInformationFiles();
349 
350  self::assertArrayHasKey('psr-4File', $files);
351  self::assertArrayHasKey('classMapFile', $files);
352  foreach ($expectedPsr4Files as $expectation) {
353  if ($expectation[0] === '!') {
354  $expectedCount = 0;
355  } else {
356  $expectedCount = 1;
357  }
358  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), '' . $expectation);
359  }
360  foreach ($expectedClassMapFiles as $expectation) {
361  if ($expectation[0] === '!') {
362  $expectedCount = 0;
363  } else {
364  $expectedCount = 1;
365  }
366  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), '' . $expectation);
367  }
368  }
369 
370  private function ‪createPackageMock(array $packageManifest): ‪PackageInterface
371  {
372  $packageMock = $this->createMock(PackageInterface::class);
373  $packageMock->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/');
374  $packageMock->method('getValueFromComposerManifest')->willReturn(
375  json_decode(json_encode($packageManifest))
376  );
377 
378  return $packageMock;
379  }
380 }
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectly
‪autoloadDevFilesAreBuildCorrectly(array $packageManifest, array $expectedPsr4Files, array $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:344
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectlyDataProvider
‪static autoloadFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:127
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectly
‪autoloadFilesAreBuildCorrectly(array $packageManifest, array $expectedPsr4Files, array $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:246
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile
‪buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile()
Definition: ClassLoadingInformationGeneratorTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation
‪buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation()
Definition: ClassLoadingInformationGeneratorTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\createPackageMock
‪createPackageMock(array $packageManifest)
Definition: ClassLoadingInformationGeneratorTest.php:370
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile
‪buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile()
Definition: ClassLoadingInformationGeneratorTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\AnotherTestFile
Definition: AnotherTestFile.php:23
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectlyDataProvider
‪static autoloadDevFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:278
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:24
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest
Definition: ClassLoadingInformationGeneratorTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\TestFile
Definition: TestFile.php:20
‪TYPO3\CMS\Core\Tests\Unit\Core
Definition: ApplicationContextTest.php:18
‪TYPO3\CMS\Core\Error\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation
‪buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation()
Definition: ClassLoadingInformationGeneratorTest.php:97