fc2c0b720f46e7fa259ca060ac23593ab5ae8fd4
[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 * Make sure error messages during the tests get displayed no matter what is set in php.ini.
39 */
40 @ini_set('display_errors', 1);
41
42 /**
43 * Be nice and give a hint if someone is executing the tests with cli dispatch
44 */
45 if (defined('TYPO3_MODE')) {
46 array_shift($_SERVER['argv']);
47 echo 'Please run the unit tests using the following command:' . chr(10);
48 echo sprintf(
49 'typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s',
50 implode(' ', $_SERVER['argv'])
51 ) . chr(10);
52 echo chr(10);
53 exit(1);
54 }
55
56 /**
57 * Find out web path by environment variable or current working directory
58 */
59 if (getenv('TYPO3_PATH_WEB')) {
60 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
61 } else {
62 $webRoot = getcwd() . '/';
63 }
64 $webRoot = strtr($webRoot, '\\', '/');
65
66 /**
67 * Define basic TYPO3 constants
68 */
69 define('PATH_site', $webRoot);
70 define('TYPO3_MODE', 'BE');
71 define('TYPO3_cliMode', TRUE);
72
73 unset($webRoot);
74
75 /**
76 * We need to fake the current script to be the cli dispatcher to satisfy some GeneralUtility::getIndpEnv tests
77 * @todo properly mock these tests
78 */
79 define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
80 $_SERVER['SCRIPT_NAME'] = PATH_thisScript;
81
82 putenv('TYPO3_CONTEXT=Testing');
83
84 createDirectory(PATH_site . 'uploads');
85 createDirectory(PATH_site . 'typo3temp');
86 createDirectory(PATH_site . 'typo3conf/ext');
87
88 require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
89
90 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
91 ->baseSetup()
92 ->initializeClassLoader();
93
94 $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
95 $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
96
97 // Avoid failing tests that rely on HTTP_HOST retrieval
98 // @todo Check if we could do better mocking in these tests
99 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
100
101 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
102 ->disableCoreAndClassesCache()
103 ->initializeCachingFramework()
104 ->initializeClassLoaderCaches()
105 ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class);
106
107 /**
108 * Creates directory (recursively if required).
109 *
110 * @param string $directory path of the directory to be created
111 * @return void
112 * @throws \RuntimeException
113 */
114 function createDirectory($directory) {
115 if (is_dir($directory)) {
116 return;
117 }
118
119 if (!mkdir($directory, 0777, TRUE)) {
120 throw new \RuntimeException(
121 'Directory "' . $directory . '" could not be created',
122 1404038665
123 );
124 }
125
126 }