[TASK] Tests: Reduce unit test bootstrap
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Build / UnitTestsBootstrap.php
1 <?php
2 namespace TYPO3\CMS\Core\Build;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * (c) 2013 Helmut Hummel <helmut.hummel@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * This file is defined in UnitTests.xml and called by phpunit
30 * before instantiating the test suites, it must also be included
31 * with phpunit parameter --bootstrap if executing single test case classes.
32 *
33 * For easy access to the PHPUnit and VFS framework, it is recommended to install the phpunit TYPO3 Extension
34 * It does not need to be activated, nor a cli user needs to be present.
35 * But it is also possible to use other installations of PHPUnit and VFS
36 *
37 * * Call whole unit test suite, example:
38 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
39 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml
40 *
41 * Call single test case, example:
42 * - cd /var/www/t3master/foo # Document root of TYPO3 CMS instance (location of index.php)
43 * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
44 * --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php \
45 * typo3/sysext/core/Tests/Uinit/DataHandling/DataHandlerTest.php
46 */
47
48 /**
49 * Be nice and give a hint if someone is executing the tests with cli dispatch
50 */
51 if (defined('TYPO3_MODE')) {
52 array_shift($_SERVER['argv']);
53 echo 'Please run the unit tests using the following command:' . chr(10);
54 echo sprintf(
55 'typo3conf/ext/phpunit/Composer/vendor/bin/phpunit %s',
56 implode(' ', $_SERVER['argv'])
57 ) . chr(10);
58 echo chr(10);
59 exit(1);
60 }
61
62 /**
63 * Find out web path by environment variable or current working directory
64 */
65 if (getenv('TYPO3_PATH_WEB')) {
66 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
67 } else {
68 $webRoot = getcwd() . '/';
69 }
70
71 /**
72 * Fail if configuration is not found
73 */
74 if (!file_exists($webRoot . 'typo3conf/LocalConfiguration.php')) {
75 throw new \Exception('TYPO3 web root not found. Call PHPUnit from that directory or set TYPO3_PATH_WEB to it.');
76 }
77
78 /**
79 * Define basic TYPO3 constants
80 */
81 define('PATH_site', $webRoot);
82 define('TYPO3_MODE', 'BE');
83 define('TYPO3_cliMode', TRUE);
84
85 unset($webRoot);
86
87 /**
88 * We need to fake the current script to be the cli dispatcher to satisfy some GeneralUtility::getIndpEnv tests
89 * TODO: properly mock these tests
90 */
91 define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
92 $_SERVER['SCRIPT_NAME'] = PATH_thisScript;
93
94 /**
95 * Basic bootstrap
96 * Can be simplified/ stripped down once the according tests are properly mocked
97 */
98 require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
99 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
100 ->baseSetup()
101 ->loadConfigurationAndInitialize(TRUE);
102
103 // Load ext_localconf of phpunit. It takes care of registering phpunit classes and dependencies like vfsStream
104 require PATH_site . '/typo3conf/ext/phpunit/ext_localconf.php';