[TASK] Adjust tests for ClassLoadingInformationGenerator 86/43686/5
authorNicole Cordes <typo3@cordes.co>
Thu, 1 Oct 2015 08:08:58 +0000 (10:08 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Thu, 1 Oct 2015 19:33:53 +0000 (21:33 +0200)
This patch covers multiple testing scenarios to ensure the generator
class works properly with different user input.

Resolves: #70253
Releases: master
Change-Id: I1c6ab42caffc26bd544625a6bb6092988d99d6c4
Reviewed-on: http://review.typo3.org/43686
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/composer.json [deleted file]
typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/ext_emconf.php [deleted file]

index 591f622..8e52155 100644 (file)
@@ -54,35 +54,152 @@ class ClassLoadingInformationGeneratorTest extends UnitTestCase {
                $generator = $this->getAccessibleMock(
                        ClassLoadingInformationGenerator::class,
                        ['dummy'],
-                       [$this->getMock(ClassLoader::class), $this->createPackagesMock(), __DIR__]
+                       [$this->getMock(ClassLoader::class), $this->createPackagesMock(array()), __DIR__]
                );
 
                $this->assertEquals($expectedResult, $generator->_call('isIgnoredClassName', $className));
        }
 
        /**
+        * Data provider for different autoload information
+        *
+        * @return array
+        */
+       public function autoloadFilesAreBuildCorrectlyDataProvider() {
+               return [
+                       'Psr-4 section' => [
+                               [
+                                       'autoload' => [
+                                               'psr-4' => [
+                                                       'TYPO3\\CMS\\TestExtension\\' => 'Classes/',
+                                               ],
+                                       ],
+                               ],
+                               [
+                                       '\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
+                               ],
+                               [],
+                       ],
+                       'Psr-4 section without trailing slash' => [
+                               [
+                                       'autoload' => [
+                                               'psr-4' => [
+                                                       'TYPO3\\CMS\\TestExtension\\' => 'Classes',
+                                               ],
+                                       ],
+                               ],
+                               [
+                                       '\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')',
+                               ],
+                               [],
+                       ],
+                       'Classmap section' => [
+                               [
+                                       'autoload' => [
+                                               'classmap' => [
+                                                       'Resources/PHP/',
+                                               ],
+                                       ],
+                               ],
+                               [],
+                               [
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
+                               ],
+                       ],
+                       'Classmap section without trailing slash' => [
+                               [
+                                       'autoload' => [
+                                               'classmap' => [
+                                                       'Resources/PHP',
+                                               ],
+                                       ],
+                               ],
+                               [],
+                               [
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
+                               ],
+                       ],
+                       'Classmap section pointing to a file' => [
+                               [
+                                       'autoload' => [
+                                               'classmap' => [
+                                                       'Resources/PHP/Test.php',
+                                               ],
+                                       ],
+                               ],
+                               [],
+                               [
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
+                                       '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
+                                       '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
+                               ],
+                       ],
+                       'Classmap section pointing to two files' => [
+                               [
+                                       'autoload' => [
+                                               'classmap' => [
+                                                       'Resources/PHP/Test.php',
+                                                       'Resources/PHP/AnotherTestFile.php',
+                                               ],
+                                       ],
+                               ],
+                               [],
+                               [
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'',
+                                       '$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php\'',
+                                       '!$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php\'',
+                               ],
+                       ],
+               ];
+       }
+
+       /**
         * @test
+        * @dataProvider autoloadFilesAreBuildCorrectlyDataProvider
+        *
+        * @param string $packageManifest
+        * @param array $expectedPsr4Files
+        * @param array $expectedClassMapFiles
         */
-       public function autoloadFilesAreBuildCorrectly() {
+       public function autoloadFilesAreBuildCorrectly($packageManifest, $expectedPsr4Files, $expectedClassMapFiles) {
                /** @var ClassLoader|\PHPUnit_Framework_MockObject_MockObject $classLoaderMock */
                $classLoaderMock = $this->getMock(ClassLoader::class);
-               $generator = new ClassLoadingInformationGenerator($classLoaderMock, $this->createPackagesMock(), __DIR__);
+               $generator = new ClassLoadingInformationGenerator($classLoaderMock, $this->createPackagesMock($packageManifest), __DIR__);
                $files = $generator->buildAutoloadInformationFiles();
 
                $this->assertArrayHasKey('psr-4File', $files);
                $this->assertArrayHasKey('classMapFile', $files);
-               $this->assertContains('\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\')', $files['psr-4File']);
-               $this->assertContains('$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP/Test.php\'', $files['classMapFile']);
+               foreach ($expectedPsr4Files as $expectation) {
+                       if ($expectation[0] === '!') {
+                               $expectedCount = 0;
+                       } else {
+                               $expectedCount = 1;
+                       }
+                       $this->assertSame($expectedCount, substr_count($files['psr-4File'], $expectation));
+               }
+               foreach ($expectedClassMapFiles as $expectation) {
+                       if ($expectation[0] === '!') {
+                               $expectedCount = 0;
+                       } else {
+                               $expectedCount = 1;
+                       }
+                       $this->assertSame($expectedCount, substr_count($files['classMapFile'], $expectation));
+               }
        }
 
        /**
+        * @param array Array which should be returned as composer manifest
         * @return PackageInterface[]
         */
-       protected function createPackagesMock() {
+       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(
-                       json_decode(file_get_contents(__DIR__ . '/Fixtures/test_extension/composer.json'))
+                       json_decode(json_encode($packageManifest))
                );
 
                return [$packageStub];
diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/AnotherTestFile.php
new file mode 100644 (file)
index 0000000..a080adf
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP;
+
+/*                                                                        *
+ * 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!                         *
+ *                                                                        */
+
+/**
+ * Class Test
+ */
+class AnotherTestFile {
+
+}
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/Resources/PHP/Subdirectory/SubdirectoryTest.php
new file mode 100644 (file)
index 0000000..82fb3e6
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Core\Fixtures\test_extension\Resources\PHP\Subdirectory;
+
+/*                                                                        *
+ * 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!                         *
+ *                                                                        */
+
+/**
+ * Class Test
+ */
+class SubdirectoryTest {
+
+}
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/composer.json b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/composer.json
deleted file mode 100644 (file)
index 215103b..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-{
-       "name": "typo3/cms-core-test",
-       "type": "typo3-cms-extension",
-       "description": "TYPO3 Core Test",
-       "homepage": "https://typo3.org",
-       "license": ["GPL-2.0+"],
-
-       "require": {
-               "php" : ">=5.5.0"
-       },
-       "replace": {
-               "core": "*"
-       },
-       "autoload": {
-               "psr-4": {
-                       "TYPO3\\CMS\\TestExtension\\": "Classes/"
-               },
-               "classmap": ["Resources/PHP/Test.php"]
-       }
-}
diff --git a/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/ext_emconf.php b/typo3/sysext/core/Tests/Unit/Core/Fixtures/test_extension/ext_emconf.php
deleted file mode 100644 (file)
index 29fcff4..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php
-$EM_CONF[$_EXTKEY] = array(
-       'title' => 'TYPO3 Core Test',
-       'description' => 'Test Extension',
-       'category' => 'be',
-       'state' => 'stable',
-       'uploadfolder' => 0,
-       'createDirs' => '',
-       'clearCacheOnLoad' => 0,
-       'author' => 'Helmut Hummel',
-       'author_email' => 'helmut@typo3.org',
-       'author_company' => '',
-       'version' => '7.6.0',
-       'constraints' => array(
-               'depends' => array(),
-               'conflicts' => array(),
-               'suggests' => array(),
-       ),
-);