‪TYPO3CMS  11.5
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 TYPO3\CMS\Core\Core\ClassLoadingInformationGenerator;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪ClassLoadingInformationGeneratorTest extends UnitTestCase
32 {
37  {
38  $this->expectException(Exception::class);
39  $this->expectExceptionCode(1444142481);
40 
41  $packageMock = $this->‪createPackageMock([
42  'extra' => [
43  'typo3/class-alias-loader' => [
44  'class-alias-maps' => [
45  'foo' => Test::class,
46  'bar' => AnotherTestFile::class,
47  ],
48  ],
49  ],
50  ]);
51  $classLoaderMock = $this->createMock(ClassLoader::class);
52  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
53  $generator->buildClassAliasMapForPackage($packageMock);
54  }
55 
60  {
61  $this->expectException(Exception::class);
62  $this->expectExceptionCode(1422625075);
63 
64  $packageMock = $this->‪createPackageMock([
65  'extra' => [
66  'typo3/class-alias-loader' => [
67  'class-alias-maps' => [
68  'Migrations/Code/WrongClassAliasMap.php',
69  ],
70  ],
71  ],
72  ]);
73  $classLoaderMock = $this->createMock(ClassLoader::class);
74  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
75  $generator->buildClassAliasMapForPackage($packageMock);
76  }
77 
82  {
83  $expectedClassMap = [
84  'aliasToClassNameMapping' => [
85  'foo' => Test::class,
86  'bar' => AnotherTestFile::class,
87  ],
88  'classNameToAliasMapping' => [
89  Test::class => [
90  'foo' => 'foo',
91  ],
92  AnotherTestFile::class => [
93  'bar' => 'bar',
94  ],
95  ],
96  ];
97  $packageMock = $this->‪createPackageMock([]);
98  $classLoaderMock = $this->createMock(ClassLoader::class);
99  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
100  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
101  }
102 
107  {
108  $expectedClassMap = [
109  'aliasToClassNameMapping' => [
110  'foo' => Test::class,
111  'bar' => AnotherTestFile::class,
112  ],
113  'classNameToAliasMapping' => [
114  Test::class => [
115  'foo' => 'foo',
116  ],
117  AnotherTestFile::class => [
118  'bar' => 'bar',
119  ],
120  ],
121  ];
122  $packageMock = $this->‪createPackageMock([
123  'extra' => [
124  'typo3/class-alias-loader' => [
125  'class-alias-maps' => [
126  'Resources/PHP/ClassAliasMap.php',
127  ],
128  ],
129  ],
130  ]);
131  $classLoaderMock = $this->createMock(ClassLoader::class);
132  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
133  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
134  }
135 
142  {
143  return [
144  'Psr-4 section' => [
145  [
146  'autoload' => [
147  'psr-4' => [
148  'TYPO3\\CMS\\TestExtension\\' => 'Classes/',
149  ],
150  ],
151  ],
152  [
153  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
154  ],
155  [],
156  ],
157  'Psr-4 section with array' => [
158  [
159  'autoload' => [
160  'psr-4' => [
161  'TYPO3\\CMS\\TestExtension\\' => ['Classes/', 'Resources/PHP/'],
162  ],
163  ],
164  ],
165  [
166  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\',$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP\')',
167  ],
168  [],
169  ],
170  'Psr-4 section without trailing slash' => [
171  [
172  'autoload' => [
173  'psr-4' => [
174  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
175  ],
176  ],
177  ],
178  [
179  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
180  ],
181  [],
182  ],
183  'Classmap section' => [
184  [
185  'autoload' => [
186  'classmap' => [
187  'Resources/PHP/',
188  ],
189  ],
190  ],
191  [],
192  [
193  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
194  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
195  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
196  ],
197  ],
198  'Classmap section without trailing slash' => [
199  [
200  'autoload' => [
201  'classmap' => [
202  'Resources/PHP',
203  ],
204  ],
205  ],
206  [],
207  [
208  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
209  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
210  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
211  ],
212  ],
213  'Classmap section pointing to a file' => [
214  [
215  'autoload' => [
216  'classmap' => [
217  'Resources/PHP/Test.php',
218  ],
219  ],
220  ],
221  [],
222  [
223  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
224  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
225  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
226  ],
227  ],
228  'No autoload section' => [
229  [],
230  [],
231  [
232  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
233  '!$typo3InstallDir . \'/Fixtures/test_extension/Tests/TestClass.php\'',
234  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
235  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
236  '!$typo3InstallDir . \'/Fixtures/test_extension/class.ext_update.php\'',
237  ],
238  ],
239  'Classmap section pointing to two files' => [
240  [
241  'autoload' => [
242  'classmap' => [
243  'Resources/PHP/Test.php',
244  'Resources/PHP/AnotherTestFile.php',
245  ],
246  ],
247  ],
248  [],
249  [
250  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
251  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
252  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
253  ],
254  ],
255  ];
256  }
257 
266  public function ‪autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles): void
267  {
268  $classLoaderMock = $this->createMock(ClassLoader::class);
269  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__);
270  $files = $generator->buildAutoloadInformationFiles();
271 
272  self::assertArrayHasKey('psr-4File', $files);
273  self::assertArrayHasKey('classMapFile', $files);
274  foreach ($expectedPsr4Files as $expectation) {
275  if ($expectation[0] === '!') {
276  $expectedCount = 0;
277  $expectation = substr($expectation, 1);
278  $message = sprintf('File "%s" is NOT expected to be in psr-4, but is.', $expectation);
279  } else {
280  $expectedCount = 1;
281  $message = sprintf('File "%s" is expected to be in psr-4, but is not.', $expectation);
282  }
283  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), $message);
284  }
285  foreach ($expectedClassMapFiles as $expectation) {
286  if ($expectation[0] === '!') {
287  $expectedCount = 0;
288  $expectation = substr($expectation, 1);
289  $message = sprintf('File "%s" is NOT expected to be in class map, but is.', $expectation);
290  } else {
291  $expectedCount = 1;
292  $message = sprintf('File "%s" is expected to be in class map, but is not.', $expectation);
293  }
294  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), $message);
295  }
296  }
297 
304  {
305  return [
306  'Psr-4 sections' => [
307  [
308  'autoload' => [
309  'psr-4' => [
310  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
311  ],
312  ],
313  'autoload-dev' => [
314  'psr-4' => [
315  'TYPO3\\CMS\\TestExtension\\Tests\\' => 'Tests',
316  ],
317  ],
318  ],
319  [
320  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
321  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\Tests\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
322  ],
323  [],
324  ],
325  'Psr-4 sections with override' => [
326  [
327  'autoload' => [
328  'psr-4' => [
329  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
330  ],
331  ],
332  'autoload-dev' => [
333  'psr-4' => [
334  'TYPO3\\CMS\\TestExtension\\' => 'Tests',
335  ],
336  ],
337  ],
338  [
339  '!\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
340  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
341  ],
342  [],
343  ],
344  'Classmap section pointing to two files, one in dev and one not' => [
345  [
346  'autoload' => [
347  'classmap' => [
348  'Resources/PHP/Test.php',
349  ],
350  ],
351  'autoload-dev' => [
352  'classmap' => [
353  'Resources/PHP/AnotherTestFile.php',
354  ],
355  ],
356  ],
357  [],
358  [
359  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
360  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
361  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
362  ],
363  ],
364  ];
365  }
366 
375  public function ‪autoloadDevFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles): void
376  {
377  $classLoaderMock = $this->createMock(ClassLoader::class);
378  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__, true);
379  $files = $generator->buildAutoloadInformationFiles();
380 
381  self::assertArrayHasKey('psr-4File', $files);
382  self::assertArrayHasKey('classMapFile', $files);
383  foreach ($expectedPsr4Files as $expectation) {
384  if ($expectation[0] === '!') {
385  $expectedCount = 0;
386  } else {
387  $expectedCount = 1;
388  }
389  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), '' . $expectation);
390  }
391  foreach ($expectedClassMapFiles as $expectation) {
392  if ($expectation[0] === '!') {
393  $expectedCount = 0;
394  } else {
395  $expectedCount = 1;
396  }
397  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), '' . $expectation);
398  }
399  }
400 
401  protected function ‪createPackageMock(array $packageManifest): ‪PackageInterface
402  {
403  $packageMock = $this->createMock(PackageInterface::class);
404  $packageMock->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/');
405  $packageMock->method('getValueFromComposerManifest')->willReturn(
406  json_decode(json_encode($packageManifest))
407  );
408 
409  return $packageMock;
410  }
411 }
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectly
‪autoloadDevFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:375
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile
‪buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile()
Definition: ClassLoadingInformationGeneratorTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation
‪buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation()
Definition: ClassLoadingInformationGeneratorTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\createPackageMock
‪createPackageMock(array $packageManifest)
Definition: ClassLoadingInformationGeneratorTest.php:401
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile
‪buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile()
Definition: ClassLoadingInformationGeneratorTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\AnotherTestFile
Definition: AnotherTestFile.php:23
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest
Definition: ClassLoadingInformationGeneratorTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Core
Definition: ApplicationContextTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectlyDataProvider
‪array autoloadDevFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:303
‪TYPO3\CMS\Core\Error\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectly
‪autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:266
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectlyDataProvider
‪array autoloadFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:141
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\Test
Definition: Test.php:23
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation
‪buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation()
Definition: ClassLoadingInformationGeneratorTest.php:106