[CLEANUP] Wrap the tests bootstrap code into classes 32/36532/2
authorOliver Klee <typo3-coding@oliverklee.de>
Sat, 31 Jan 2015 17:27:38 +0000 (18:27 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 1 Feb 2015 10:36:24 +0000 (11:36 +0100)
Resolves: #64662
Releases: master
Change-Id: I4763bf413190b0aa8ba0af62016b9971878435f7
Reviewed-on: http://review.typo3.org/36532
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Build/FunctionalTestsBootstrap.php
typo3/sysext/core/Build/UnitTestsBootstrap.php

index f2e33e7..68c54a1 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+namespace TYPO3\CMS\Core\Build;
+
 /*
  * This file is part of the TYPO3 CMS project.
  *
  * before instantiating the test suites, it must also be included
  * with phpunit parameter --bootstrap if executing single test case classes.
  */
+class FunctionalTestsBootstrap {
+       /**
+        * Bootstraps the system for unit tests.
+        *
+        * @return void
+        */
+       public function bootstrapSystem() {
+               $this->enableDisplayErrors()
+                       ->loadClassFiles()
+                       ->defineOriginalRootPath();
+       }
 
-/**
- * Make sure error messages during the tests get displayed no matter what is set in php.ini.
- */
-@ini_set('display_errors', 1);
+       /**
+        * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
+        *
+        * @return FunctionalTestsBootstrap fluent interface
+        */
+       protected function enableDisplayErrors() {
+               @ini_set('display_errors', 1);
+               return $this;
+       }
 
-/**
- * Require classes the functional test classes extend from or use for further bootstrap.
- * Only files required for "new TestCaseClass" are required here and a general exception
- * that is thrown by setUp() code.
- */
-require_once(__DIR__ . '/../Tests/BaseTestCase.php');
-require_once(__DIR__ . '/../Tests/FunctionalTestCase.php');
-require_once(__DIR__ . '/../Tests/FunctionalTestCaseBootstrapUtility.php');
-require_once(__DIR__ . '/../Tests/Exception.php');
+       /**
+        * Requires classes the functional test classes extend from or use for further bootstrap.
+        * Only files required for "new TestCaseClass" are required here and a general exception
+        * that is thrown by setUp() code.
+        *
+        * @return FunctionalTestsBootstrap fluent interface
+        */
+       protected function loadClassFiles() {
+               $testsDirectory = __DIR__ . '/../Tests/';
+               require_once($testsDirectory . 'BaseTestCase.php');
+               require_once($testsDirectory . 'FunctionalTestCase.php');
+               require_once($testsDirectory . 'FunctionalTestCaseBootstrapUtility.php');
+               require_once($testsDirectory . 'Exception.php');
 
-/**
- * Define a constant to specify the document root since this calculation would
- * be way more complicated if done within the test class.
- *
- * It is required that phpunit binary is called from the document root of the instance,
- * or TYPO3_PATH_WEB environement variable needs to be set,
- * otherwise path calculation, of the created TYPO3 CMS instance will fail.
- */
-if (getenv('TYPO3_PATH_WEB')) {
-       $webRoot = getenv('TYPO3_PATH_WEB') . '/';
-} elseif (isset($_SERVER['PWD'])) {
-       $webRoot = $_SERVER['PWD'] . '/';
-} else {
-       $webRoot = getcwd() . '/';
-}
-$webRoot = strtr($webRoot, '\\', '/');
+               return $this;
+       }
+
+       /**
+        * Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root.
+        *
+        * If ORIGINAL_ROOT already is defined, this method is a no-op.
+        *
+        * @return FunctionalTestsBootstrap fluent interface
+        */
+       protected function defineOriginalRootPath() {
+               if (!defined('ORIGINAL_ROOT')) {
+                       /** @var string */
+                       define('ORIGINAL_ROOT', $this->getWebRoot());
+               }
+
+               return $this;
+       }
+
+       /**
+        * Returns the absolute path the TYPO3 document root.
+        *
+        * @return string the TYPO3 document root using Unix path separators
+        *
+        * @throws \RuntimeException
+        */
+       protected function getWebRoot() {
+               if (getenv('TYPO3_PATH_WEB')) {
+                       $webRoot = getenv('TYPO3_PATH_WEB') . '/';
+               } else {
+                       $webRoot = getcwd() . '/';
+               }
 
-if (!defined('ORIGINAL_ROOT')) {
-       define('ORIGINAL_ROOT', $webRoot);
+               return strtr($webRoot, '\\', '/');
+       }
 }
 
-unset($webRoot);
\ No newline at end of file
+$bootstrap = new FunctionalTestsBootstrap();
+$bootstrap->bootstrapSystem();
+unset($bootstrap);
index fc2c0b7..f627214 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Core\Build;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Core\Bootstrap;
+
 /**
  * This file is defined in UnitTests.xml and called by phpunit
  * before instantiating the test suites, it must also be included
@@ -33,94 +35,184 @@ namespace TYPO3\CMS\Core\Build;
  *     --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php \
  *     typo3/sysext/core/Tests/Uinit/DataHandling/DataHandlerTest.php
  */
+class UnitTestsBootstrap {
+       /**
+        * Bootstraps the system for unit tests.
+        *
+        * @return void
+        */
+       public function bootstrapSystem() {
+               $this->enableDisplayErrors()
+                       ->checkForCliDispatch()
+                       ->defineSitePath()
+                       ->setTypo3Context()
+                       ->createNecessaryDirectoriesInDocumentRoot()
+                       ->includeAndStartCoreBootstrap()
+                       ->initializeConfiguration()
+                       ->finishCoreBootstrap();
+       }
 
-/**
- * Make sure error messages during the tests get displayed no matter what is set in php.ini.
- */
-@ini_set('display_errors', 1);
-
-/**
- * Be nice and give a hint if someone is executing the tests with cli dispatch
- */
-if (defined('TYPO3_MODE')) {
-       array_shift($_SERVER['argv']);
-       echo 'Please run the unit tests using the following command:' . chr(10);
-       echo sprintf(
-               'typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s',
-               implode(' ', $_SERVER['argv'])
-       ) . chr(10);
-       echo chr(10);
-       exit(1);
-}
+       /**
+        * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function enableDisplayErrors() {
+               @ini_set('display_errors', 1);
+               return $this;
+       }
 
-/**
- * Find out web path by environment variable or current working directory
- */
-if (getenv('TYPO3_PATH_WEB')) {
-       $webRoot = getenv('TYPO3_PATH_WEB') . '/';
-} else {
-       $webRoot = getcwd() . '/';
-}
-$webRoot = strtr($webRoot, '\\', '/');
+       /**
+        * Checks whether the tests are run using the CLI dispatcher. If so, echos a helpful message and exits with
+        * an error code 1.
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function checkForCliDispatch() {
+               if (!defined('TYPO3_MODE')) {
+                       return $this;
+               }
+
+               array_shift($_SERVER['argv']);
+               $flatArguments = implode(' ', $_SERVER['argv']);
+               echo 'Please run the unit tests using the following command:' . chr(10) .
+                       sprintf('typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s', $flatArguments) . chr(10) .
+                       chr(10);
+
+               exit(1);
+       }
 
-/**
- * Define basic TYPO3 constants
- */
-define('PATH_site', $webRoot);
-define('TYPO3_MODE', 'BE');
-define('TYPO3_cliMode', TRUE);
+       /**
+        * Defines the PATH_site and PATH_thisScript constant and sets $_SERVER['SCRIPT_NAME'].
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function defineSitePath() {
+               /** @var string */
+               define('PATH_site', $this->getWebRoot());
+               /** @var string */
+               define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
+               $_SERVER['SCRIPT_NAME'] = PATH_thisScript;
+
+               return $this;
+       }
 
-unset($webRoot);
+       /**
+        * Returns the absolute path the TYPO3 document root.
+        *
+        * @return string the TYPO3 document root using Unix path separators
+        *
+        * @throws \RuntimeException
+        */
+       protected function getWebRoot() {
+               if (getenv('TYPO3_PATH_WEB')) {
+                       $webRoot = getenv('TYPO3_PATH_WEB') . '/';
+               } else {
+                       $webRoot = getcwd() . '/';
+               }
+
+               return strtr($webRoot, '\\', '/');
+       }
 
-/**
- * We need to fake the current script to be the cli dispatcher to satisfy some GeneralUtility::getIndpEnv tests
- * @todo properly mock these tests
- */
-define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
-$_SERVER['SCRIPT_NAME'] = PATH_thisScript;
+       /**
+        * Defines TYPO3_MODE, TYPO3_cliMode and sets the environment variable TYPO3_CONTEXT.
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function setTypo3Context() {
+               /** @var string */
+               define('TYPO3_MODE', 'BE');
+               /** @var string */
+               define('TYPO3_cliMode', TRUE);
+               putenv('TYPO3_CONTEXT=Testing');
+
+               return $this;
+       }
 
-putenv('TYPO3_CONTEXT=Testing');
+       /**
+        * Creates the following directories in the TYPO3 document root:
+        * - typo3conf
+        * - typo3conf/ext
+        * - typo3temp
+        * - uploads
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function createNecessaryDirectoriesInDocumentRoot() {
+               $this->createDirectory(PATH_site . 'uploads');
+               $this->createDirectory(PATH_site . 'typo3temp');
+               $this->createDirectory(PATH_site . 'typo3conf/ext');
+
+               return $this;
+       }
 
-createDirectory(PATH_site . 'uploads');
-createDirectory(PATH_site . 'typo3temp');
-createDirectory(PATH_site . 'typo3conf/ext');
+       /**
+        * Creates the directory $directory (recursively if required).
+        *
+        * If $directory already exists, this method is a no-op.
+        *
+        * @param  string $directory absolute path of the directory to be created
+        *
+        * @return void
+        *
+        * @throws \RuntimeException
+        */
+       protected function createDirectory($directory) {
+               if (is_dir($directory)) {
+                       return;
+               }
+
+               if (!mkdir($directory, 0777, TRUE)) {
+                       throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
+               }
+       }
 
-require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
+       /**
+        * Includes the Core Bootstrap class and calls its first few functions.
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function includeAndStartCoreBootstrap() {
+               require_once PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
 
-\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
-       ->baseSetup()
-       ->initializeClassLoader();
+               Bootstrap::getInstance()
+                       ->baseSetup()
+                       ->initializeClassLoader();
 
-$configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
-$GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
+               return $this;
+       }
 
-// Avoid failing tests that rely on HTTP_HOST retrieval
-// @todo Check if we could do better mocking in these tests
-$GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
+       /**
+        * Provides the default configuration in $GLOBALS['TYPO3_CONF_VARS'].
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function initializeConfiguration() {
+               $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
+               $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
 
-\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
-       ->disableCoreAndClassesCache()
-       ->initializeCachingFramework()
-       ->initializeClassLoaderCaches()
-       ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class);
+               // avoid failing tests that rely on HTTP_HOST retrieval
+               $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
 
-/**
- * Creates directory (recursively if required).
- *
- * @param  string $directory path of the directory to be created
- * @return void
- * @throws \RuntimeException
- */
-function createDirectory($directory) {
-       if (is_dir($directory)) {
-               return;
+               return $this;
        }
 
-       if (!mkdir($directory, 0777, TRUE)) {
-               throw new \RuntimeException(
-                       'Directory "' . $directory . '" could not be created',
-                       1404038665
-               );
+       /**
+        * Finishes the last steps of the Core Bootstrap.
+        *
+        * @return UnitTestsBootstrap fluent interface
+        */
+       protected function finishCoreBootstrap() {
+               Bootstrap::getInstance()
+                       ->disableCoreAndClassesCache()
+                       ->initializeCachingFramework()
+                       ->initializeClassLoaderCaches()
+                       ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class);
+
+               return $this;
        }
-
 }
+
+$bootstrap = new UnitTestsBootstrap();
+$bootstrap->bootstrapSystem();
+unset($bootstrap);