2 namespace TYPO3\CMS\Core\Build
;
5 * This file is part of the TYPO3 CMS project.
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Core\Bootstrap
;
20 * This file is defined in UnitTests.xml and called by phpunit
21 * before instantiating the test suites, it must also be included
22 * with phpunit parameter --bootstrap if executing single test case classes.
24 * For easy access to the PHPUnit and VFS framework, it is recommended to install the phpunit TYPO3 Extension
25 * It does not need to be activated, nor a cli user needs to be present.
26 * But it is also possible to use other installations of PHPUnit and VFS
28 * * Call whole unit test suite, example:
29 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
30 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml
32 * Call single test case, example:
33 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
34 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
35 * --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php \
36 * typo3/sysext/core/Tests/Uinit/DataHandling/DataHandlerTest.php
38 class UnitTestsBootstrap
{
40 * Bootstraps the system for unit tests.
44 public function bootstrapSystem() {
45 $this->enableDisplayErrors()
46 ->checkForCliDispatch()
49 ->createNecessaryDirectoriesInDocumentRoot()
50 ->includeAndStartCoreBootstrap()
51 ->initializeConfiguration()
52 ->finishCoreBootstrap();
56 * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
58 * @return UnitTestsBootstrap fluent interface
60 protected function enableDisplayErrors() {
61 @ini_set
('display_errors', 1);
66 * Checks whether the tests are run using the CLI dispatcher. If so, echos a helpful message and exits with
69 * @return UnitTestsBootstrap fluent interface
71 protected function checkForCliDispatch() {
72 if (!defined('TYPO3_MODE')) {
76 array_shift($_SERVER['argv']);
77 $flatArguments = implode(' ', $_SERVER['argv']);
78 echo 'Please run the unit tests using the following command:' . chr(10) .
79 sprintf('typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s', $flatArguments) . chr(10) .
86 * Defines the PATH_site and PATH_thisScript constant and sets $_SERVER['SCRIPT_NAME'].
88 * @return UnitTestsBootstrap fluent interface
90 protected function defineSitePath() {
92 define('PATH_site', $this->getWebRoot());
94 define('PATH_thisScript', PATH_site
. 'typo3/cli_dispatch.phpsh');
95 $_SERVER['SCRIPT_NAME'] = PATH_thisScript
;
101 * Returns the absolute path the TYPO3 document root.
103 * @return string the TYPO3 document root using Unix path separators
105 * @throws \RuntimeException
107 protected function getWebRoot() {
108 if (getenv('TYPO3_PATH_WEB')) {
109 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
111 $webRoot = getcwd() . '/';
114 return strtr($webRoot, '\\', '/');
118 * Defines TYPO3_MODE, TYPO3_cliMode and sets the environment variable TYPO3_CONTEXT.
120 * @return UnitTestsBootstrap fluent interface
122 protected function setTypo3Context() {
124 define('TYPO3_MODE', 'BE');
126 define('TYPO3_cliMode', TRUE);
127 putenv('TYPO3_CONTEXT=Testing');
133 * Creates the following directories in the TYPO3 document root:
139 * @return UnitTestsBootstrap fluent interface
141 protected function createNecessaryDirectoriesInDocumentRoot() {
142 $this->createDirectory(PATH_site
. 'uploads');
143 $this->createDirectory(PATH_site
. 'typo3temp');
144 $this->createDirectory(PATH_site
. 'typo3conf/ext');
150 * Creates the directory $directory (recursively if required).
152 * If $directory already exists, this method is a no-op.
154 * @param string $directory absolute path of the directory to be created
158 * @throws \RuntimeException
160 protected function createDirectory($directory) {
161 if (is_dir($directory)) {
165 if (!mkdir($directory, 0777, TRUE)) {
166 throw new \
RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
171 * Includes the Core Bootstrap class and calls its first few functions.
173 * @return UnitTestsBootstrap fluent interface
175 protected function includeAndStartCoreBootstrap() {
176 require_once PATH_site
. '/typo3/sysext/core/Classes/Core/Bootstrap.php';
178 Bootstrap
::getInstance()
180 ->initializeClassLoader();
186 * Provides the default configuration in $GLOBALS['TYPO3_CONF_VARS'].
188 * @return UnitTestsBootstrap fluent interface
190 protected function initializeConfiguration() {
191 $configurationManager = new \TYPO3\CMS\Core\Configuration\
ConfigurationManager();
192 $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
194 // avoid failing tests that rely on HTTP_HOST retrieval
195 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
201 * Finishes the last steps of the Core Bootstrap.
203 * @return UnitTestsBootstrap fluent interface
205 protected function finishCoreBootstrap() {
206 Bootstrap
::getInstance()
207 ->disableCoreAndClassesCache()
208 ->initializeCachingFramework()
209 ->initializeClassLoaderCaches()
210 ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager
::class);
216 $bootstrap = new UnitTestsBootstrap();
217 $bootstrap->bootstrapSystem();