diff --git a/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php b/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php index 7c4a9a7f09fc72b2f859113b6903f5f700669f87..cb8a226cc115fa5cd4d9c158a6cc9b1a2e81337d 100644 --- a/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php +++ b/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php @@ -165,18 +165,31 @@ class ClassLoadingInformationGenerator { * @return array */ public function buildClassAliasMapForPackage(PackageInterface $package) { - $aliasToClassNameMapping = array(); - $classNameToAliasMapping = array(); - $possibleClassAliasFile = $package->getPackagePath() . 'Migrations/Code/ClassAliasMap.php'; - if (file_exists($possibleClassAliasFile)) { - $packageAliasMap = require $possibleClassAliasFile; - if (!is_array($packageAliasMap)) { - throw new \TYPO3\CMS\Core\Error\Exception('"class alias maps" must return an array', 1422625075); + $aliasToClassNameMapping = []; + $classNameToAliasMapping = []; + $possibleClassAliasFiles = []; + $manifest = $package->getValueFromComposerManifest(); + if (!empty($manifest->extra->{'typo3/class-alias-loader'}->{'class-alias-maps'})) { + $possibleClassAliasFiles = $manifest->extra->{'typo3/class-alias-loader'}->{'class-alias-maps'}; + if (!is_array($possibleClassAliasFiles)) { + throw new \TYPO3\CMS\Core\Error\Exception('"typo3/class-alias-loader"/"class-alias-maps" must return an array!', 1444142481); } - foreach ($packageAliasMap as $aliasClassName => $className) { - $lowerCasedAliasClassName = strtolower($aliasClassName); - $aliasToClassNameMapping[$lowerCasedAliasClassName] = $className; - $classNameToAliasMapping[$className][$lowerCasedAliasClassName] = $lowerCasedAliasClassName; + } else { + $possibleClassAliasFiles[] = 'Migrations/Code/ClassAliasMap.php'; + } + $packagePath = $package->getPackagePath(); + foreach ($possibleClassAliasFiles as $possibleClassAliasFile) { + $possiblePathToClassAliasFile = $packagePath . $possibleClassAliasFile; + if (file_exists($possiblePathToClassAliasFile)) { + $packageAliasMap = require $possiblePathToClassAliasFile; + if (!is_array($packageAliasMap)) { + throw new \TYPO3\CMS\Core\Error\Exception('"class alias maps" must return an array', 1422625075); + } + foreach ($packageAliasMap as $aliasClassName => $className) { + $lowerCasedAliasClassName = strtolower($aliasClassName); + $aliasToClassNameMapping[$lowerCasedAliasClassName] = $className; + $classNameToAliasMapping[$className][$lowerCasedAliasClassName] = $lowerCasedAliasClassName; + } } } diff --git a/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php b/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php index 8e52155e3e13982cdeccb3fee98e4f8a6d04fd90..56f37beab85d7031343bbd1f9c217f91441a0926 100644 --- a/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php +++ b/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php @@ -1,15 +1,18 @@ <?php namespace TYPO3\CMS\Core\Tests\Unit\Core; -/* * - * This script belongs to the TYPO3 Flow framework. * - * * - * It is free software; you can redistribute it and/or modify it under * - * the terms of the GNU Lesser General Public License, either version 3 * - * of the License, or (at your option) any later version. * - * * - * The TYPO3 project - inspiring people to share! * - * */ +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ use Composer\Autoload\ClassLoader; use TYPO3\CMS\Core\Core\ClassLoadingInformationGenerator; @@ -54,12 +57,113 @@ class ClassLoadingInformationGeneratorTest extends UnitTestCase { $generator = $this->getAccessibleMock( ClassLoadingInformationGenerator::class, ['dummy'], - [$this->getMock(ClassLoader::class), $this->createPackagesMock(array()), __DIR__] + [$this->getMock(ClassLoader::class), array(), __DIR__] ); $this->assertEquals($expectedResult, $generator->_call('isIgnoredClassName', $className)); } + /** + * @test + * @expectedException \TYPO3\CMS\Core\Error\Exception + * @expectedExceptionCode 1444142481 + */ + public function buildClassAliasMapForPackageThrowsExceptionForWrongComposerManifestInformation() { + $packageMock = $this->createPackageMock([ + 'extra' => [ + 'typo3/class-alias-loader' => [ + 'class-alias-maps' => [ + 'foo' => Fixtures\test_extension\Resources\PHP\Test::class, + 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class, + ], + ], + ], + ]); + /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */ + $classLoaderMock = $this->getMock(ClassLoader::class); + $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__); + $generator->buildClassAliasMapForPackage($packageMock); + } + + /** + * @test + * @expectedException \TYPO3\CMS\Core\Error\Exception + * @expectedExceptionCode 1422625075 + */ + public function buildClassAliasMapForPackageThrowsExceptionForWrongClassAliasMapFile() { + $packageMock = $this->createPackageMock([ + 'extra' => [ + 'typo3/class-alias-loader' => [ + 'class-alias-maps' => [ + 'Migrations/Code/WrongClassAliasMap.php', + ], + ], + ], + ]); + /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */ + $classLoaderMock = $this->getMock(ClassLoader::class); + $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__); + $generator->buildClassAliasMapForPackage($packageMock); + } + + /** + * @test + */ + public function buildClassAliasMapForPackageReturnsClassAliasMapForClassAliasMapFile() { + $expectedClassMap = [ + 'aliasToClassNameMapping' => [ + 'foo' => Fixtures\test_extension\Resources\PHP\Test::class, + 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class, + ], + 'classNameToAliasMapping' => [ + Fixtures\test_extension\Resources\PHP\Test::class => [ + 'foo' => 'foo', + ], + Fixtures\test_extension\Resources\PHP\AnotherTestFile::class => [ + 'bar' => 'bar', + ] + ], + ]; + $packageMock = $this->createPackageMock(array()); + /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */ + $classLoaderMock = $this->getMock(ClassLoader::class); + $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__); + $this->assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock)); + } + + /** + * @test + */ + public function buildClassAliasMapForPackageReturnsClassAliasMapForComposerManifestInformation() { + $expectedClassMap = [ + 'aliasToClassNameMapping' => [ + 'foo' => Fixtures\test_extension\Resources\PHP\Test::class, + 'bar' => Fixtures\test_extension\Resources\PHP\AnotherTestFile::class, + ], + 'classNameToAliasMapping' => [ + Fixtures\test_extension\Resources\PHP\Test::class => [ + 'foo' => 'foo', + ], + Fixtures\test_extension\Resources\PHP\AnotherTestFile::class => [ + 'bar' => 'bar', + ] + ], + ]; + $packageMock = $this->createPackageMock([ + 'extra' => [ + 'typo3/class-alias-loader' => [ + 'class-alias-maps' => [ + 'Resources/PHP/ClassAliasMap.php', + ], + ], + ], + ]); + /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */ + $classLoaderMock = $this->getMock(ClassLoader::class); + $generator = new ClassLoadingInformationGenerator($classLoaderMock, [], __DIR__); + $this->assertEquals($expectedClassMap, $generator->buildClassAliasMapForPackage($packageMock)); + } + /** * Data provider for different autoload information * @@ -168,7 +272,7 @@ class ClassLoadingInformationGeneratorTest extends UnitTestCase { public function autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles) { /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */ $classLoaderMock = $this->getMock(ClassLoader::class); - $generator = new ClassLoadingInformationGenerator($classLoaderMock, $this->createPackagesMock($packageManifest), __DIR__); + $generator = new ClassLoadingInformationGenerator($classLoaderMock, [$this->createPackageMock($packageManifest)], __DIR__); $files = $generator->buildAutoloadInformationFiles(); $this->assertArrayHasKey('psr-4File', $files); @@ -193,16 +297,16 @@ class ClassLoadingInformationGeneratorTest extends UnitTestCase { /** * @param array Array which should be returned as composer manifest - * @return PackageInterface[] + * @return PackageInterface */ - protected function createPackagesMock($packageManifest) { - $packageStub = $this->getMock(PackageInterface::class); - $packageStub->expects($this->any())->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/'); - $packageStub->expects($this->any())->method('getValueFromComposerManifest')->willReturn( + protected function createPackageMock($packageManifest) { + $packageMock = $this->getMock(PackageInterface::class); + $packageMock->expects($this->any())->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/test_extension/'); + $packageMock->expects($this->any())->method('getValueFromComposerManifest')->willReturn( json_decode(json_encode($packageManifest)) ); - return [$packageStub]; + return $packageMock; } } diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/ClassAliasMap.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/ClassAliasMap.php new file mode 100644 index 0000000000000000000000000000000000000000..8b59d2cd7057874fccc4cbfc62263f14f9c7921a --- /dev/null +++ b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/ClassAliasMap.php @@ -0,0 +1,5 @@ +<?php +return array( + 'foo' => \TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\Test::class, + 'bar' => \TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\AnotherTestFile::class, +); \ No newline at end of file diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/WrongClassAliasMap.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/WrongClassAliasMap.php new file mode 100644 index 0000000000000000000000000000000000000000..b3d9bbc7f3711e882119cd6b3af051245d859d04 --- /dev/null +++ b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/WrongClassAliasMap.php @@ -0,0 +1 @@ +<?php diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/ClassAliasMap.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/ClassAliasMap.php new file mode 100644 index 0000000000000000000000000000000000000000..8b59d2cd7057874fccc4cbfc62263f14f9c7921a --- /dev/null +++ b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/ClassAliasMap.php @@ -0,0 +1,5 @@ +<?php +return array( + 'foo' => \TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\Test::class, + 'bar' => \TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\AnotherTestFile::class, +); \ No newline at end of file