TYPO3 CMS  TYPO3_7-6
UnitTestsBootstrap.php
Go to the documentation of this file.
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 
18 
39 {
45  public function bootstrapSystem()
46  {
47  $this->enableDisplayErrors()
48  ->checkForCliDispatch()
49  ->defineSitePath()
50  ->setTypo3Context()
51  ->createNecessaryDirectoriesInDocumentRoot()
52  ->includeAndStartCoreBootstrap()
53  ->initializeConfiguration()
54  ->finishCoreBootstrap();
55  }
56 
62  protected function enableDisplayErrors()
63  {
64  @ini_set('display_errors', 1);
65  return $this;
66  }
67 
74  protected function checkForCliDispatch()
75  {
76  if (!defined('TYPO3_MODE')) {
77  return $this;
78  }
79 
80  array_shift($_SERVER['argv']);
81  $flatArguments = implode(' ', $_SERVER['argv']);
82  echo 'Please run the unit tests using the following command:' . chr(10) .
83  sprintf('typo3/../bin/phpunit %s', $flatArguments) . chr(10) .
84  chr(10);
85 
86  exit(1);
87  }
88 
94  protected function defineSitePath()
95  {
97  define('PATH_site', $this->getWebRoot());
99  define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
100  $_SERVER['SCRIPT_NAME'] = PATH_thisScript;
101 
102  if (!file_exists(PATH_thisScript)) {
103  $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.');
104  }
105 
106  return $this;
107  }
108 
114  protected function getWebRoot()
115  {
116  if (getenv('TYPO3_PATH_WEB')) {
117  $webRoot = getenv('TYPO3_PATH_WEB');
118  } else {
119  $webRoot = getcwd();
120  }
121  return rtrim(strtr($webRoot, '\\', '/'), '/') . '/';
122  }
123 
129  protected function setTypo3Context()
130  {
132  define('TYPO3_MODE', 'BE');
134  define('TYPO3_cliMode', true);
135  putenv('TYPO3_CONTEXT=Testing');
136 
137  // Set TYPO3_AJAX and define TYPO3_REQUESSTTYPE's to prevent PHP 7.2 warnings
138  $GLOBALS['TYPO3_AJAX'] = '';
139  Bootstrap::getInstance()->defineTypo3RequestTypes();
140 
141  return $this;
142  }
143 
154  {
155  $this->createDirectory(PATH_site . 'uploads');
156  $this->createDirectory(PATH_site . 'typo3temp');
157  $this->createDirectory(PATH_site . 'typo3conf/ext');
158 
159  return $this;
160  }
161 
171  protected function createDirectory($directory)
172  {
173  if (is_dir($directory)) {
174  return;
175  }
176  @mkdir($directory, 0777, true);
177  clearstatcache();
178  if (!is_dir($directory)) {
179  throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1423043755);
180  }
181  }
182 
188  protected function includeAndStartCoreBootstrap()
189  {
190  $classLoaderFilepath = __DIR__ . '/../../../../vendor/autoload.php';
191  if (!file_exists($classLoaderFilepath)) {
192  $this->exitWithMessage('ClassLoader can\'t be loaded. Please check your path or set an environment variable \'TYPO3_PATH_WEB\' to your root path.');
193  }
194  $classLoader = require $classLoaderFilepath;
195 
197  ->initializeClassLoader($classLoader)
198  ->baseSetup();
199 
200  return $this;
201  }
202 
208  protected function initializeConfiguration()
209  {
210  $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
211  $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
212 
213  // avoid failing tests that rely on HTTP_HOST retrieval
214  $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
215 
216  return $this;
217  }
218 
224  protected function finishCoreBootstrap()
225  {
227  ->disableCoreCache()
228  ->initializeCachingFramework()
229  ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class)
230  ->ensureClassLoadingInformationExists();
231 
232  return $this;
233  }
234 
240  protected function exitWithMessage($message)
241  {
242  echo $message . chr(10);
243  exit(1);
244  }
245 }
246 
247 if (PHP_SAPI !== 'cli') {
248  die('This script supports command line usage only. Please check your command.');
249 }
251 $bootstrap->bootstrapSystem();
252 unset($bootstrap);
if(PHP_SAPI !=='cli') $bootstrap
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']