[TASK] Read manifest for class alias files 63/43863/9
authorHelmut Hummel <helmut.hummel@typo3.org>
Tue, 6 Oct 2015 14:43:38 +0000 (16:43 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Wed, 7 Oct 2015 14:44:03 +0000 (16:44 +0200)
To be more consistent with composer mode, allow
definition of alias maps in non composer mode as well.

Read the composer manifest for that
(which can be composer.json or ext_emconf.php)

Resolves: #70437
Releases: master
Change-Id: I2f888d7f95dbadc4da1785572fc793a084cf0991
Reviewed-on: http://review.typo3.org/43863
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php
typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/ClassAliasMap.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Migrations/Code/WrongClassAliasMap.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/ClassAliasMap.php [new file with mode: 0644]

index 7c4a9a7..cb8a226 100644 (file)
@@ -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;
+                               }
                        }
                }
 
index 8e52155..56f37be 100644 (file)
@@ -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,13 +57,114 @@ 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
         *
         * @return array
@@ -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 (file)
index 0000000..8b59d2c
--- /dev/null
@@ -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 (file)
index 0000000..b3d9bbc
--- /dev/null
@@ -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 (file)
index 0000000..8b59d2c
--- /dev/null
@@ -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