‪TYPO3CMS  10.4
ClassLoadingInformationGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Composer\Autoload\ClassLoader;
19 use TYPO3\CMS\Core\Core\ClassLoadingInformationGenerator;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪ClassLoadingInformationGeneratorTest extends UnitTestCase
30 {
35  {
36  $this->expectException(Exception::class);
37  $this->expectExceptionCode(1444142481);
38 
39  $packageMock = $this->‪createPackageMock([
40  'extra' => [
41  'typo3/class-alias-loader' => [
42  'class-alias-maps' => [
43  'foo' => Test::class,
44  'bar' => AnotherTestFile::class,
45  ],
46  ],
47  ],
48  ]);
50  $classLoaderMock = $this->createMock(ClassLoader::class);
51  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
52  $generator->buildClassAliasMapForPackage($packageMock);
53  }
54 
59  {
60  $this->expectException(Exception::class);
61  $this->expectExceptionCode(1422625075);
62 
63  $packageMock = $this->‪createPackageMock([
64  'extra' => [
65  'typo3/class-alias-loader' => [
66  'class-alias-maps' => [
67  'Migrations/Code/WrongClassAliasMap.php',
68  ],
69  ],
70  ],
71  ]);
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([]);
99  $classLoaderMock = $this->createMock(ClassLoader::class);
100  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
101  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
102  }
103 
108  {
109  $expectedClassMap = [
110  'aliasToClassNameMapping' => [
111  'foo' => Test::class,
112  'bar' => AnotherTestFile::class,
113  ],
114  'classNameToAliasMapping' => [
115  Test::class => [
116  'foo' => 'foo',
117  ],
118  AnotherTestFile::class => [
119  'bar' => 'bar',
120  ]
121  ],
122  ];
123  $packageMock = $this->‪createPackageMock([
124  'extra' => [
125  'typo3/class-alias-loader' => [
126  'class-alias-maps' => [
127  'Resources/PHP/ClassAliasMap.php',
128  ],
129  ],
130  ],
131  ]);
133  $classLoaderMock = $this->createMock(ClassLoader::class);
134  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
135  self::assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
136  }
137 
144  {
145  return [
146  'Psr-4 section' => [
147  [
148  'autoload' => [
149  'psr-4' => [
150  'TYPO3\\CMS\\TestExtension\\' => 'Classes/',
151  ],
152  ],
153  ],
154  [
155  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
156  ],
157  [],
158  ],
159  'Psr-4 section with array' => [
160  [
161  'autoload' => [
162  'psr-4' => [
163  'TYPO3\\CMS\\TestExtension\\' => ['Classes/', 'Resources/PHP/'],
164  ],
165  ],
166  ],
167  [
168  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\',$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP\')',
169  ],
170  [],
171  ],
172  'Psr-4 section without trailing slash' => [
173  [
174  'autoload' => [
175  'psr-4' => [
176  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
177  ],
178  ],
179  ],
180  [
181  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
182  ],
183  [],
184  ],
185  'Classmap section' => [
186  [
187  'autoload' => [
188  'classmap' => [
189  'Resources/PHP/',
190  ],
191  ],
192  ],
193  [],
194  [
195  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
196  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
197  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
198  ],
199  ],
200  'Classmap section without trailing slash' => [
201  [
202  'autoload' => [
203  'classmap' => [
204  'Resources/PHP',
205  ],
206  ],
207  ],
208  [],
209  [
210  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
211  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
212  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
213  ],
214  ],
215  'Classmap section pointing to a file' => [
216  [
217  'autoload' => [
218  'classmap' => [
219  'Resources/PHP/Test.php',
220  ],
221  ],
222  ],
223  [],
224  [
225  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
226  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
227  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
228  ],
229  ],
230  'No autoload section' => [
231  [],
232  [],
233  [
234  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
235  '!$typo3InstallDir . \'/Fixtures/test_extension/Tests/TestClass.php\'',
236  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
237  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
238  '!$typo3InstallDir . \'/Fixtures/test_extension/class.ext_update.php\'',
239  ],
240  ],
241  'Classmap section pointing to two files' => [
242  [
243  'autoload' => [
244  'classmap' => [
245  'Resources/PHP/Test.php',
246  'Resources/PHP/AnotherTestFile.php',
247  ],
248  ],
249  ],
250  [],
251  [
252  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
253  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
254  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
255  ],
256  ],
257  ];
258  }
259 
268  public function ‪autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
269  {
271  $classLoaderMock = $this->createMock(ClassLoader::class);
272  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__);
273  $files = $generator->buildAutoloadInformationFiles();
274 
275  self::assertArrayHasKey('psr-4File', $files);
276  self::assertArrayHasKey('classMapFile', $files);
277  foreach ($expectedPsr4Files as $expectation) {
278  if ($expectation[0] === '!') {
279  $expectedCount = 0;
280  $expectation = substr($expectation, 1);
281  $message = sprintf('File "%s" is NOT expected to be in psr-4, but is.', $expectation);
282  } else {
283  $expectedCount = 1;
284  $message = sprintf('File "%s" is expected to be in psr-4, but is not.', $expectation);
285  }
286  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), $message);
287  }
288  foreach ($expectedClassMapFiles as $expectation) {
289  if ($expectation[0] === '!') {
290  $expectedCount = 0;
291  $expectation = substr($expectation, 1);
292  $message = sprintf('File "%s" is NOT expected to be in class map, but is.', $expectation);
293  } else {
294  $expectedCount = 1;
295  $message = sprintf('File "%s" is expected to be in class map, but is not.', $expectation);
296  }
297  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), $message);
298  }
299  }
300 
307  {
308  return [
309  'Psr-4 sections' => [
310  [
311  'autoload' => [
312  'psr-4' => [
313  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
314  ],
315  ],
316  'autoload-dev' => [
317  'psr-4' => [
318  'TYPO3\\CMS\\TestExtension\\Tests\\' => 'Tests',
319  ],
320  ],
321  ],
322  [
323  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
324  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\Tests\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
325  ],
326  [],
327  ],
328  'Psr-4 sections with override' => [
329  [
330  'autoload' => [
331  'psr-4' => [
332  'TYPO3\\CMS\\TestExtension\\' => 'Classes',
333  ],
334  ],
335  'autoload-dev' => [
336  'psr-4' => [
337  'TYPO3\\CMS\\TestExtension\\' => 'Tests',
338  ],
339  ],
340  ],
341  [
342  '!\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
343  '\'‪TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Tests\')',
344  ],
345  [],
346  ],
347  'Classmap section pointing to two files, one in dev and one not' => [
348  [
349  'autoload' => [
350  'classmap' => [
351  'Resources/PHP/Test.php',
352  ],
353  ],
354  'autoload-dev' => [
355  'classmap' => [
356  'Resources/PHP/AnotherTestFile.php',
357  ],
358  ],
359  ],
360  [],
361  [
362  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
363  '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
364  '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
365  ],
366  ],
367  ];
368  }
369 
378  public function ‪autoloadDevFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
379  {
381  $classLoaderMock = $this->createMock(ClassLoader::class);
382  $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->‪createPackageMock($packageManifest)], __DIR__, true);
383  $files = $generator->buildAutoloadInformationFiles();
384 
385  self::assertArrayHasKey('psr-4File', $files);
386  self::assertArrayHasKey('classMapFile', $files);
387  foreach ($expectedPsr4Files as $expectation) {
388  if ($expectation[0] === '!') {
389  $expectedCount = 0;
390  } else {
391  $expectedCount = 1;
392  }
393  self::assertSame($expectedCount, substr_count($files['psr-4File'], $expectation), '' . $expectation);
394  }
395  foreach ($expectedClassMapFiles as $expectation) {
396  if ($expectation[0] === '!') {
397  $expectedCount = 0;
398  } else {
399  $expectedCount = 1;
400  }
401  self::assertSame($expectedCount, substr_count($files['classMapFile'], $expectation), '' . $expectation);
402  }
403  }
404 
409  protected function ‪createPackageMock($packageManifest)
410  {
411  $packageMock = $this->createMock(PackageInterface::class);
412  $packageMock->expects(self::any())->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/');
413  $packageMock->expects(self::any())->method('getValueFromComposerManifest')->willReturn(
414  json_decode(json_encode($packageManifest))
415  );
416 
417  return $packageMock;
418  }
419 }
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectly
‪autoloadDevFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:378
‪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:34
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile
‪buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile()
Definition: ClassLoadingInformationGeneratorTest.php:58
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\AnotherTestFile
Definition: AnotherTestFile.php:22
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\createPackageMock
‪PackageInterface createPackageMock($packageManifest)
Definition: ClassLoadingInformationGeneratorTest.php:409
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest
Definition: ClassLoadingInformationGeneratorTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Core
Definition: ApplicationContextTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadDevFilesAreBuildCorrectlyDataProvider
‪array autoloadDevFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:306
‪TYPO3\CMS\Core\Error\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectly
‪autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles)
Definition: ClassLoadingInformationGeneratorTest.php:268
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\autoloadFilesAreBuildCorrectlyDataProvider
‪array autoloadFilesAreBuildCorrectlyDataProvider()
Definition: ClassLoadingInformationGeneratorTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\Test
Definition: Test.php:22
‪TYPO3\CMS\Core\Tests\Unit\Core\ClassLoadingInformationGeneratorTest\buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation
‪buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation()
Definition: ClassLoadingInformationGeneratorTest.php:107