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  die('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  return $this;
138  }
139 
150  {
151  $this->createDirectory(PATH_site . 'uploads');
152  $this->createDirectory(PATH_site . 'typo3temp');
153  $this->createDirectory(PATH_site . 'typo3conf/ext');
154 
155  return $this;
156  }
157 
167  protected function createDirectory($directory)
168  {
169  if (is_dir($directory)) {
170  return;
171  }
172  @mkdir($directory, 0777, true);
173  clearstatcache();
174  if (!is_dir($directory)) {
175  throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1423043755);
176  }
177  }
178 
184  protected function includeAndStartCoreBootstrap()
185  {
186  $classLoaderFilepath = __DIR__ . '/../../../../vendor/autoload.php';
187  if (!file_exists($classLoaderFilepath)) {
188  die('ClassLoader can\'t be loaded. Please check your path or set an environment variable \'TYPO3_PATH_WEB\' to your root path.');
189  }
190  $classLoader = require $classLoaderFilepath;
191 
193  ->initializeClassLoader($classLoader)
194  ->baseSetup();
195 
196  return $this;
197  }
198 
204  protected function initializeConfiguration()
205  {
206  $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager();
207  $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
208 
209  // avoid failing tests that rely on HTTP_HOST retrieval
210  $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
211 
212  return $this;
213  }
214 
220  protected function finishCoreBootstrap()
221  {
223  ->disableCoreCache()
224  ->initializeCachingFramework()
225  ->initializePackageManagement(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class)
226  ->ensureClassLoadingInformationExists();
227 
228  return $this;
229  }
230 }
231 
232 if (PHP_SAPI !== 'cli') {
233  die('This script supports command line usage only. Please check your command.');
234 }
235 $bootstrap = new UnitTestsBootstrap();
236 $bootstrap->bootstrapSystem();
237 unset($bootstrap);