[BUGFIX] Remove possibly undefined constant from test bootstraps
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Build / FunctionalTestsBootstrap.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 use TYPO3\CMS\Core\Tests\BaseTestCase;
18
19 /**
20 * This file is defined in FunctionalTests.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.
23 */
24 class FunctionalTestsBootstrap
25 {
26 /**
27 * Bootstraps the system for unit tests.
28 *
29 * @return void
30 */
31 public function bootstrapSystem()
32 {
33 $this->enableDisplayErrors()
34 ->loadClassFiles()
35 ->defineOriginalRootPath()
36 ->createNecessaryDirectoriesInDocumentRoot();
37 }
38
39 /**
40 * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
41 *
42 * @return FunctionalTestsBootstrap fluent interface
43 */
44 protected function enableDisplayErrors()
45 {
46 @ini_set('display_errors', 1);
47 return $this;
48 }
49
50 /**
51 * Requires classes the functional test classes extend from or use for further bootstrap.
52 * Only files required for "new TestCaseClass" are required here and a general exception
53 * that is thrown by setUp() code.
54 *
55 * @return FunctionalTestsBootstrap fluent interface
56 */
57 protected function loadClassFiles()
58 {
59 if (!class_exists('PHPUnit_Framework_TestCase')) {
60 $this->exitWithMessage('PHPUnit wasn\'t found. Please check your settings and command.');
61 }
62 if (!class_exists(BaseTestCase::class)) {
63 // PHPUnit is invoked globally, so we need to include the project autoload file
64 require_once __DIR__ . '/../../../../vendor/autoload.php';
65 }
66
67 return $this;
68 }
69
70 /**
71 * Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root.
72 *
73 * If ORIGINAL_ROOT already is defined, this method is a no-op.
74 *
75 * @return FunctionalTestsBootstrap fluent interface
76 */
77 protected function defineOriginalRootPath()
78 {
79 if (!defined('ORIGINAL_ROOT')) {
80 /** @var string */
81 define('ORIGINAL_ROOT', $this->getWebRoot());
82 }
83
84 if (!file_exists(ORIGINAL_ROOT . 'typo3/cli_dispatch.phpsh')) {
85 $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.');
86 }
87
88 return $this;
89 }
90
91 /**
92 * Creates the following directories in the TYPO3 core:
93 * - typo3temp
94 *
95 * @return FunctionalTestsBootstrap fluent interface
96 */
97 protected function createNecessaryDirectoriesInDocumentRoot()
98 {
99 $this->createDirectory(ORIGINAL_ROOT . 'typo3temp');
100
101 return $this;
102 }
103
104 /**
105 * Creates the directory $directory (recursively if required).
106 *
107 * If $directory already exists, this method is a no-op.
108 *
109 * @param string $directory absolute path of the directory to be created
110 * @return void
111 * @throws \RuntimeException
112 */
113 protected function createDirectory($directory)
114 {
115 if (is_dir($directory)) {
116 return;
117 }
118 @mkdir($directory, 0777, true);
119 clearstatcache();
120 if (!is_dir($directory)) {
121 throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
122 }
123 }
124
125 /**
126 * Returns the absolute path the TYPO3 document root.
127 *
128 * @return string the TYPO3 document root using Unix path separators
129 */
130 protected function getWebRoot()
131 {
132 if (getenv('TYPO3_PATH_WEB')) {
133 $webRoot = getenv('TYPO3_PATH_WEB');
134 } else {
135 $webRoot = getcwd();
136 }
137 return rtrim(strtr($webRoot, '\\', '/'), '/') . '/';
138 }
139
140 /**
141 * Echo out a text message and exit with error code
142 *
143 * @param string $message
144 */
145 protected function exitWithMessage($message)
146 {
147 echo $message . chr(10);
148 exit(1);
149 }
150 }
151
152 if (PHP_SAPI !== 'cli') {
153 die('This script supports command line usage only. Please check your command.');
154 }
155 $bootstrap = new FunctionalTestsBootstrap();
156 $bootstrap->bootstrapSystem();
157 unset($bootstrap);