|
| 1 | +<?php |
| 2 | +namespace Nimut\TestingFramework\Bootstrap; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the NIMUT testing-framework project. |
| 6 | + * |
| 7 | + * It was taken from the TYPO3 CMS project (www.typo3.org). |
| 8 | + * |
| 9 | + * It is free software; you can redistribute it and/or modify it under |
| 10 | + * the terms of the GNU General Public License, either version 2 |
| 11 | + * of the License, or any later version. |
| 12 | + * |
| 13 | + * For the full copyright and license information, please read |
| 14 | + * LICENSE file that was distributed with this source code. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * This file is defined in FunctionalTests.xml and called by phpunit |
| 19 | + * before instantiating the test suites, it must also be included |
| 20 | + * with phpunit parameter --bootstrap if executing single test case classes. |
| 21 | + */ |
| 22 | +class FunctionalTestsBootstrap |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Bootstraps the system for unit tests. |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function bootstrapSystem() |
| 30 | + { |
| 31 | + $this->enableDisplayErrors() |
| 32 | + ->loadClassFiles() |
| 33 | + ->defineOriginalRootPath() |
| 34 | + ->createNecessaryDirectoriesInDocumentRoot(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Makes sure error messages during the tests get displayed no matter what is set in php.ini. |
| 39 | + * |
| 40 | + * @return FunctionalTestsBootstrap fluent interface |
| 41 | + */ |
| 42 | + protected function enableDisplayErrors() |
| 43 | + { |
| 44 | + @ini_set('display_errors', 1); |
| 45 | + |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Requires classes the functional test classes extend from or use for further bootstrap. |
| 51 | + * Only files required for "new TestCaseClass" are required here and a general exception |
| 52 | + * that is thrown by setUp() code. |
| 53 | + * |
| 54 | + * @return FunctionalTestsBootstrap fluent interface |
| 55 | + */ |
| 56 | + protected function loadClassFiles() |
| 57 | + { |
| 58 | + if (!class_exists('PHPUnit_Framework_TestCase')) { |
| 59 | + $this->exitWithMessage('PHPUnit wasn\'t found. Please check your settings and command.'); |
| 60 | + } |
| 61 | + if (!class_exists('Nimut\\TestingFramework\\TestCase\\BaseTestCase')) { |
| 62 | + // PHPUnit is invoked globally, so we need to include the project autoload file |
| 63 | + require_once __DIR__ . '/../../../../../autoload.php'; |
| 64 | + } |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root. |
| 71 | + * |
| 72 | + * If ORIGINAL_ROOT already is defined, this method is a no-op. |
| 73 | + * |
| 74 | + * @return FunctionalTestsBootstrap fluent interface |
| 75 | + */ |
| 76 | + protected function defineOriginalRootPath() |
| 77 | + { |
| 78 | + if (!defined('ORIGINAL_ROOT')) { |
| 79 | + /** @var string */ |
| 80 | + define('ORIGINAL_ROOT', $this->getWebRoot()); |
| 81 | + } |
| 82 | + |
| 83 | + if (!file_exists(ORIGINAL_ROOT . 'typo3/cli_dispatch.phpsh')) { |
| 84 | + $this->exitWithMessage('Unable to determine path to entry script. Please check your path or set an environment variable \'TYPO3_PATH_WEB\' to your root path.'); |
| 85 | + } |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates the following directories in the TYPO3 core: |
| 92 | + * - typo3temp |
| 93 | + * |
| 94 | + * @return FunctionalTestsBootstrap fluent interface |
| 95 | + */ |
| 96 | + protected function createNecessaryDirectoriesInDocumentRoot() |
| 97 | + { |
| 98 | + $this->createDirectory(ORIGINAL_ROOT . 'typo3temp'); |
| 99 | + $this->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests'); |
| 100 | + $this->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient'); |
| 101 | + |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Creates the directory $directory (recursively if required). |
| 107 | + * |
| 108 | + * If $directory already exists, this method is a no-op. |
| 109 | + * |
| 110 | + * @param string $directory absolute path of the directory to be created |
| 111 | + * @throws \RuntimeException |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + protected function createDirectory($directory) |
| 115 | + { |
| 116 | + if (is_dir($directory)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + @mkdir($directory, 0777, true); |
| 120 | + clearstatcache(); |
| 121 | + if (!is_dir($directory)) { |
| 122 | + throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns the absolute path the TYPO3 document root. |
| 128 | + * |
| 129 | + * @return string the TYPO3 document root using Unix path separators |
| 130 | + */ |
| 131 | + protected function getWebRoot() |
| 132 | + { |
| 133 | + if (getenv('TYPO3_PATH_WEB')) { |
| 134 | + $webRoot = getenv('TYPO3_PATH_WEB'); |
| 135 | + } else { |
| 136 | + $webRoot = getcwd(); |
| 137 | + } |
| 138 | + |
| 139 | + return rtrim(strtr($webRoot, '\\', '/'), '/') . '/'; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Echo out a text message and exit with error code |
| 144 | + * |
| 145 | + * @param string $message |
| 146 | + */ |
| 147 | + protected function exitWithMessage($message) |
| 148 | + { |
| 149 | + echo $message . PHP_EOL; |
| 150 | + exit(1); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +if (PHP_SAPI !== 'cli') { |
| 155 | + die('This script supports command line usage only. Please check your command.'); |
| 156 | +} |
| 157 | +$bootstrap = new FunctionalTestsBootstrap(); |
| 158 | +$bootstrap->bootstrapSystem(); |
| 159 | +unset($bootstrap); |
0 commit comments