[BUGFIX] Create required directories in test bootstrap
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Build / UnitTestsBootstrap.php
1 <?php
2 namespace TYPO3\CMS\Core\Build;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
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.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This file is defined in UnitTests.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 * For easy access to the PHPUnit and VFS framework, it is recommended to install the phpunit TYPO3 Extension
23 * It does not need to be activated, nor a cli user needs to be present.
24 * But it is also possible to use other installations of PHPUnit and VFS
25 *
26 * * Call whole unit test suite, example:
27 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
28 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml
29 *
30 * Call single test case, example:
31 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
32 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
33 * --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php \
34 * typo3/sysext/core/Tests/Uinit/DataHandling/DataHandlerTest.php
35 */
36
37 /**
38 * Be nice and give a hint if someone is executing the tests with cli dispatch
39 */
40 if (defined('TYPO3_MODE')) {
41 array_shift($_SERVER['argv']);
42 echo 'Please run the unit tests using the following command:' . chr(10);
43 echo sprintf(
44 'typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s',
45 implode(' ', $_SERVER['argv'])
46 ) . chr(10);
47 echo chr(10);
48 exit(1);
49 }
50
51 /**
52 * Find out web path by environment variable or current working directory
53 */
54 if (getenv('TYPO3_PATH_WEB')) {
55 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
56 } else {
57 $webRoot = getcwd() . '/';
58 }
59 $webRoot = strtr($webRoot, '\\', '/');
60
61 /**
62 * Define basic TYPO3 constants
63 */
64 define('PATH_site', $webRoot);
65 define('TYPO3_MODE', 'BE');
66 define('TYPO3_cliMode', TRUE);
67
68 unset($webRoot);
69
70 /**
71 * We need to fake the current script to be the cli dispatcher to satisfy some GeneralUtility::getIndpEnv tests
72 * TODO: properly mock these tests
73 */
74 define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
75 $_SERVER['SCRIPT_NAME'] = PATH_thisScript;
76
77 putenv('TYPO3_CONTEXT=Testing');
78
79 createDirectory(PATH_site . 'uploads');
80 createDirectory(PATH_site . 'typo3temp');
81 createDirectory(PATH_site . 'typo3conf/ext');
82
83 require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
84
85 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
86 ->baseSetup()
87 ->initializeClassLoader();
88
89 $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
90 $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
91
92 // Avoid failing tests that rely on HTTP_HOST retrieval
93 // TODO: Check if we could do better mocking in these tests
94 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
95
96 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
97 ->disableCoreAndClassesCache()
98 ->initializeCachingFramework()
99 ->initializeClassLoaderCaches()
100 ->initializePackageManagement('TYPO3\\CMS\\Core\\Package\\UnitTestPackageManager');
101
102 /**
103 * Creates directory (recursively if required).
104 *
105 * @param string $directory path of the directory to be created
106 * @return void
107 * @throws \RuntimeException
108 */
109 function createDirectory($directory) {
110 if (is_dir($directory)) {
111 return;
112 }
113
114 if (!mkdir($directory, 0777, TRUE)) {
115 throw new \RuntimeException(
116 'Directory "' . $directory . '" could not be created',
117 1404038665
118 );
119 }
120 }