[TASK] Read manifest for class alias files
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Core / ClassLoadingInformationGeneratorTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Core;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Composer\Autoload\ClassLoader;
18 use TYPO3\CMS\Core\Core\ClassLoadingInformationGenerator;
19 use TYPO3\CMS\Core\Package\PackageInterface;
20 use TYPO3\CMS\Core\Tests\UnitTestCase;
21
22
23 /**
24 * Testcase for the ClassLoadingInformationGenerator class
25 */
26 class ClassLoadingInformationGeneratorTest extends UnitTestCase {
27
28 /**
29 * Data provider with different class names.
30 *
31 * @return array
32 */
33 public function isIgnoredClassNameIgnoresTestClassesDataProvider() {
34 return array(
35 'FoTest' => array('FoTest', TRUE),
36 'FoLowercasetest' => array('FoLowercasetest', FALSE),
37 'DifferentClassTes' => array('DifferentClassTes', FALSE),
38 'Test' => array('Test', TRUE),
39 'FoFixture' => array('FoFixture', TRUE),
40 'FoLowercasefixture' => array('FoLowercasefixture', FALSE),
41 'DifferentClassFixtur' => array('DifferentClassFixtur', FALSE),
42 'Fixture' => array('Fixture', TRUE),
43 'Latest' => array('Latest', FALSE),
44 'LaTest' => array('LaTest', TRUE),
45 'Tx_RedirectTest_Domain_Model_Test' => array('Tx_RedirectTest_Domain_Model_Test', FALSE),
46 );
47 }
48
49 /**
50 * @test
51 * @dataProvider isIgnoredClassNameIgnoresTestClassesDataProvider
52 *
53 * @param string $className
54 * @param bool $expectedResult
55 */
56 public function isIgnoredClassNameIgnoresTestClasses($className, $expectedResult) {
57 $generator = $this->getAccessibleMock(
58 ClassLoadingInformationGenerator::class,
59 ['dummy'],
60 [$this->getMock(ClassLoader::class), array(), __DIR__]
61 );
62
63 $this->assertEquals($expectedResult, $generator->_call('isIgnoredClassName', $className));
64 }
65
66 /**
67 * @test
68 * @expectedException \TYPO3\CMS\Core\Error\Exception
69 * @expectedExceptionCode 1444142481
70 */
71 public function buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation() {
72 $packageMock = $this->createPackageMock([
73 'extra' => [
74 'typo3/class-alias-loader' => [
75 'class-alias-maps' => [
76 'foo' => Fixtures\test_extension\Resources\PHP\Test::class,
77 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class,
78 ],
79 ],
80 ],
81 ]);
82 /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
83 $classLoaderMock = $this->getMock(ClassLoader::class);
84 $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
85 $generator->buildClassAliasMapForPackage($packageMock);
86 }
87
88 /**
89 * @test
90 * @expectedException \TYPO3\CMS\Core\Error\Exception
91 * @expectedExceptionCode 1422625075
92 */
93 public function buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile() {
94 $packageMock = $this->createPackageMock([
95 'extra' => [
96 'typo3/class-alias-loader' => [
97 'class-alias-maps' => [
98 'Migrations/Code/WrongClassAliasMap.php',
99 ],
100 ],
101 ],
102 ]);
103 /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
104 $classLoaderMock = $this->getMock(ClassLoader::class);
105 $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
106 $generator->buildClassAliasMapForPackage($packageMock);
107 }
108
109 /**
110 * @test
111 */
112 public function buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile() {
113 $expectedClassMap = [
114 'aliasToClassNameMapping' => [
115 'foo' => Fixtures\test_extension\Resources\PHP\Test::class,
116 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class,
117 ],
118 'classNameToAliasMapping' => [
119 Fixtures\test_extension\Resources\PHP\Test::class => [
120 'foo' => 'foo',
121 ],
122 Fixtures\test_extension\Resources\PHP\AnotherTestFile::class => [
123 'bar' => 'bar',
124 ]
125 ],
126 ];
127 $packageMock = $this->createPackageMock(array());
128 /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
129 $classLoaderMock = $this->getMock(ClassLoader::class);
130 $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
131 $this->assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
132 }
133
134 /**
135 * @test
136 */
137 public function buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation() {
138 $expectedClassMap = [
139 'aliasToClassNameMapping' => [
140 'foo' => Fixtures\test_extension\Resources\PHP\Test::class,
141 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class,
142 ],
143 'classNameToAliasMapping' => [
144 Fixtures\test_extension\Resources\PHP\Test::class => [
145 'foo' => 'foo',
146 ],
147 Fixtures\test_extension\Resources\PHP\AnotherTestFile::class => [
148 'bar' => 'bar',
149 ]
150 ],
151 ];
152 $packageMock = $this->createPackageMock([
153 'extra' => [
154 'typo3/class-alias-loader' => [
155 'class-alias-maps' => [
156 'Resources/PHP/ClassAliasMap.php',
157 ],
158 ],
159 ],
160 ]);
161 /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
162 $classLoaderMock = $this->getMock(ClassLoader::class);
163 $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__);
164 $this->assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock));
165 }
166
167 /**
168 * Data provider for different autoload information
169 *
170 * @return array
171 */
172 public function autoloadFilesAreBuildCorrectlyDataProvider() {
173 return [
174 'Psr-4 section' => [
175 [
176 'autoload' => [
177 'psr-4' => [
178 'TYPO3\\CMS\\TestExtension\\' => 'Classes/',
179 ],
180 ],
181 ],
182 [
183 '\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
184 ],
185 [],
186 ],
187 'Psr-4 section without trailing slash' => [
188 [
189 'autoload' => [
190 'psr-4' => [
191 'TYPO3\\CMS\\TestExtension\\' => 'Classes',
192 ],
193 ],
194 ],
195 [
196 '\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
197 ],
198 [],
199 ],
200 'Classmap section' => [
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 without trailing slash' => [
216 [
217 'autoload' => [
218 'classmap' => [
219 'Resources/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 'Classmap section pointing to a file' => [
231 [
232 'autoload' => [
233 'classmap' => [
234 'Resources/PHP/Test.php',
235 ],
236 ],
237 ],
238 [],
239 [
240 '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
241 '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
242 '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
243 ],
244 ],
245 'Classmap section pointing to two files' => [
246 [
247 'autoload' => [
248 'classmap' => [
249 'Resources/PHP/Test.php',
250 'Resources/PHP/AnotherTestFile.php',
251 ],
252 ],
253 ],
254 [],
255 [
256 '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
257 '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
258 '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
259 ],
260 ],
261 ];
262 }
263
264 /**
265 * @test
266 * @dataProvider autoloadFilesAreBuildCorrectlyDataProvider
267 *
268 * @param string $packageManifest
269 * @param array $expectedPsr4Files
270 * @param array $expectedClassMapFiles
271 */
272 public function autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles) {
273 /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
274 $classLoaderMock = $this->getMock(ClassLoader::class);
275 $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->createPackageMock($packageManifest)], __DIR__);
276 $files = $generator->buildAutoloadInformationFiles();
277
278 $this->assertArrayHasKey('psr-4File', $files);
279 $this->assertArrayHasKey('classMapFile', $files);
280 foreach ($expectedPsr4Files as $expectation) {
281 if ($expectation[0] === '!') {
282 $expectedCount = 0;
283 } else {
284 $expectedCount = 1;
285 }
286 $this->assertSame($expectedCount, substr_count($files['psr-4File'], $expectation));
287 }
288 foreach ($expectedClassMapFiles as $expectation) {
289 if ($expectation[0] === '!') {
290 $expectedCount = 0;
291 } else {
292 $expectedCount = 1;
293 }
294 $this->assertSame($expectedCount, substr_count($files['classMapFile'], $expectation));
295 }
296 }
297
298 /**
299 * @param array Array which should be returned as composer manifest
300 * @return PackageInterface
301 */
302 protected function createPackageMock($packageManifest) {
303 $packageMock = $this->getMock(PackageInterface::class);
304 $packageMock->expects($this->any())->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/');
305 $packageMock->expects($this->any())->method('getValueFromComposerManifest')->willReturn(
306 json_decode(json_encode($packageManifest))
307 );
308
309 return $packageMock;
310 }
311
312 }