[TASK] Removes eval() in core general utility unit tests 46/40446/5
authorPierrick Caillon <pierrick.caillon@plan-net.fr>
Thu, 18 Jun 2015 12:56:00 +0000 (14:56 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Thu, 18 Jun 2015 21:14:50 +0000 (23:14 +0200)
Adds and updates fixtures JavaScript minifying error test, class
replacement test, instantiation with constructor parameters test
and deprecated log permissions test and removes the ugly usage of
eval() from GeneralUtilityTest in unit tests.

Resolves: #67600
Related: #67468
Releases: master
Change-Id: Id5990bb21b2c395787ac5013a1440c5a71462e23
Reviewed-on: http://review.typo3.org/40446
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityFixture.php
typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMinifyJavaScriptFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/OriginalClassFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/OtherReplacementClassFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/ParentClassFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/ReplacementClassFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/TwoParametersConstructorFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index a79036e..e24c42b 100644 (file)
@@ -20,6 +20,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  * Class GeneralUtilityFixture
  */
 class GeneralUtilityFixture extends GeneralUtility {
+       const DEPRECATION_LOG_PATH = 'typo3temp/test_deprecation/test.log';
 
        /**
         * @var int
@@ -74,4 +75,19 @@ class GeneralUtilityFixture extends GeneralUtility {
                return parent::stripHttpHeaders($content);
        }
 
-}
\ No newline at end of file
+       /**
+        * Gets the absolute path to the deprecation log file.
+        *
+        * @return string Absolute path to the deprecation log file
+        */
+       static public function getDeprecationLogFileName() {
+               return PATH_site . static::DEPRECATION_LOG_PATH;
+       }
+
+       /**
+        * Resets the internal computed class name cache.
+        */
+       static public function resetFinalClassNameCache() {
+               static::$finalClassNameCache = array();
+       }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMinifyJavaScriptFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMinifyJavaScriptFixture.php
new file mode 100644 (file)
index 0000000..f63529d
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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 TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Fixture for javascript minification logging
+ */
+class GeneralUtilityMinifyJavaScriptFixture extends GeneralUtility {
+
+       /**
+        * Logs message to the development log.
+        *
+        * @param string $errorMessage Message (in english).
+        * @throws \UnexpectedValueException
+        * @throws \RuntimeException
+        */
+       static public function devLog($errorMessage) {
+               if ($errorMessage !== 'Error minifying java script: foo') {
+                       throw new \UnexpectedValueException('broken');
+               }
+               throw new \RuntimeException();
+       }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/OriginalClassFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/OriginalClassFixture.php
new file mode 100644 (file)
index 0000000..0c0487c
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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!
+ */
+
+/**
+ * Fixture for an original class
+ */
+class OriginalClassFixture {
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/OtherReplacementClassFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/OtherReplacementClassFixture.php
new file mode 100644 (file)
index 0000000..737adaa
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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!
+ */
+
+/**
+ * Fixture for a second replacement class for the original class
+ */
+class OtherReplacementClassFixture extends ReplacementClassFixture {
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/ParentClassFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/ParentClassFixture.php
new file mode 100644 (file)
index 0000000..ce4fdb9
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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!
+ */
+
+/**
+ * Fixture for a base class
+ */
+class ParentClassFixture {
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/ReplacementClassFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/ReplacementClassFixture.php
new file mode 100644 (file)
index 0000000..14a973d
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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!
+ */
+
+/**
+ * Fixture for a replacement class for the original class
+ */
+class ReplacementClassFixture extends OriginalClassFixture {
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/TwoParametersConstructorFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/TwoParametersConstructorFixture.php
new file mode 100644 (file)
index 0000000..5b578ff
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * 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!
+ */
+
+/**
+ * Fixture constructor two parameter test
+ */
+class TwoParametersConstructorFixture {
+
+       /**
+        * @var string
+        */
+       public $constructorParameter1;
+
+       /**
+        * @var string
+        */
+       public $constructorParameter2;
+
+       /**
+        * @param string $parameter1
+        * @param string $parameter2
+        */
+       public function __construct($parameter1, $parameter2) {
+               $this->constructorParameter1 = $parameter1;
+               $this->constructorParameter2 = $parameter2;
+       }
+}
index 8967040..a1722c2 100644 (file)
@@ -15,6 +15,11 @@ namespace TYPO3\CMS\Core\Tests\Unit\Utility;
  */
 
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMinifyJavaScriptFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OriginalClassFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OtherReplacementClassFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\ReplacementClassFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\TwoParametersConstructorFixture;
 use TYPO3\CMS\Core\Utility;
 use \org\bovigo\vfs\vfsStream;
 use \org\bovigo\vfs\vfsStreamDirectory;
@@ -2564,9 +2569,6 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function minifyJavaScriptWritesExceptionMessageToDevLog() {
-               $t3libDivMock = $this->getUniqueId('GeneralUtility');
-               eval('namespace ' . __NAMESPACE__ . '; class ' . $t3libDivMock . ' extends \\TYPO3\\CMS\\Core\\Utility\\GeneralUtility {' . '  static public function devLog($errorMessage) {' . '    if (!($errorMessage === \'Error minifying java script: foo\')) {' . '      throw new \\UnexpectedValue(\'broken\');' . '    }' . '    throw new \\RuntimeException();' . '  }' . '}');
-               $t3libDivMock = __NAMESPACE__ . '\\' . $t3libDivMock;
                $hookClassName = $this->getUniqueId('tx_coretest');
                $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
                $functionName = '&' . $hookClassName . '->minify';
@@ -2576,7 +2578,7 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
                $minifyHookMock->expects($this->any())->method('minify')->will($this->returnCallback(array($this, 'minifyJavaScriptErroneousCallback')));
                $this->setExpectedException('\\RuntimeException');
-               $t3libDivMock::minifyJavaScript('string to compress');
+               GeneralUtilityMinifyJavaScriptFixture::minifyJavaScript('string to compress');
        }
 
        /**
@@ -3793,11 +3795,7 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function makeInstancePassesParametersToConstructor() {
-               $className = $this->getUniqueId('testingClass');
-               if (!class_exists($className, FALSE)) {
-                       eval('class ' . $className . ' {' . '  public $constructorParameter1;' . '  public $constructorParameter2;' . '  public function __construct($parameter1, $parameter2) {' . '    $this->constructorParameter1 = $parameter1;' . '    $this->constructorParameter2 = $parameter2;' . '  }' . '}');
-               }
-               $instance = Utility\GeneralUtility::makeInstance($className, 'one parameter', 'another parameter');
+               $instance = Utility\GeneralUtility::makeInstance(TwoParametersConstructorFixture::class, 'one parameter', 'another parameter');
                $this->assertEquals('one parameter', $instance->constructorParameter1, 'The first constructor parameter has not been set.');
                $this->assertEquals('another parameter', $instance->constructorParameter2, 'The second constructor parameter has not been set.');
        }
@@ -3806,22 +3804,19 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function makeInstanceInstanciatesConfiguredImplementation() {
-               $classNameOriginal = get_class($this->getMock($this->getUniqueId('foo')));
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$classNameOriginal] = array('className' => $classNameOriginal . 'Other');
-               eval('class ' . $classNameOriginal . 'Other extends ' . $classNameOriginal . ' {}');
-               $this->assertInstanceOf($classNameOriginal . 'Other', Utility\GeneralUtility::makeInstance($classNameOriginal));
+               GeneralUtilityFixture::resetFinalClassNameCache();
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][OriginalClassFixture::class] = array('className' => ReplacementClassFixture::class);
+               $this->assertInstanceOf(ReplacementClassFixture::class, Utility\GeneralUtility::makeInstance(OriginalClassFixture::class));
        }
 
        /**
         * @test
         */
        public function makeInstanceResolvesConfiguredImplementationsRecursively() {
-               $classNameOriginal = get_class($this->getMock($this->getUniqueId('foo')));
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$classNameOriginal] = array('className' => $classNameOriginal . 'Other');
-               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$classNameOriginal . 'Other'] = array('className' => $classNameOriginal . 'OtherOther');
-               eval('class ' . $classNameOriginal . 'Other extends ' . $classNameOriginal . ' {}');
-               eval('class ' . $classNameOriginal . 'OtherOther extends ' . $classNameOriginal . 'Other {}');
-               $this->assertInstanceOf($classNameOriginal . 'OtherOther', Utility\GeneralUtility::makeInstance($classNameOriginal));
+               GeneralUtilityFixture::resetFinalClassNameCache();
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][OriginalClassFixture::class] = array('className' => ReplacementClassFixture::class);
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][ReplacementClassFixture::class] = array('className' => OtherReplacementClassFixture::class);
+               $this->assertInstanceOf(OtherReplacementClassFixture::class, Utility\GeneralUtility::makeInstance(OriginalClassFixture::class));
        }
 
        /**
@@ -4149,26 +4144,14 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                if (TYPO3_OS == 'WIN') {
                        $this->markTestSkipped('deprecationLogFixesPermissionsOnLogFile() test not available on Windows.');
                }
-               // Create extending class and let getDeprecationLogFileName return something within typo3temp/
-               $className = $this->getUniqueId('GeneralUtility');
-               /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Utility\GeneralUtility $subject */
-               $subject = __NAMESPACE__ . '\\' . $className;
-               eval(
-                       'namespace ' . __NAMESPACE__ . ';' .
-                       'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\GeneralUtility {' .
-                       '  static public function getDeprecationLogFileName() {' .
-                       '    return PATH_site . \'typo3temp/test_deprecation/test.log\';' .
-                       '  }' .
-                       '}'
-               );
-               $filePath = PATH_site . 'typo3temp/test_deprecation/';
-               @mkdir($filePath);
+               $filePath = PATH_site . GeneralUtilityFixture::DEPRECATION_LOG_PATH;
+               @mkdir(dirname($filePath));
                $this->testFilesToDelete[] = $filePath;
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog'] = TRUE;
                $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
-               $subject::deprecationLog('foo');
+               GeneralUtilityFixture::deprecationLog('foo');
                clearstatcache();
-               $resultFilePermissions = substr(decoct(fileperms($filePath . 'test.log')), 2);
+               $resultFilePermissions = substr(decoct(fileperms($filePath)), 2);
                $this->assertEquals('0777', $resultFilePermissions);
        }