d52ada0da6a59c0915eb8cebe6e3e9522d789057
[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 die('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 die('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/var/tests');
100 $this->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient');
101
102 return $this;
103 }
104
105 /**
106 * Creates the directory $directory (recursively if required).
107 *
108 * If $directory already exists, this method is a no-op.
109 *
110 * @param string $directory absolute path of the directory to be created
111 * @return void
112 * @throws \RuntimeException
113 */
114 protected function createDirectory($directory)
115 {
116 if (is_dir($directory)) {
117 return;
118 }
119 @mkdir($directory, 0777, true);
120 clearstatcache();
121 if (!is_dir($directory)) {
122 throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
123 }
124 }
125
126 /**
127 * Returns the absolute path the TYPO3 document root.
128 *
129 * @return string the TYPO3 document root using Unix path separators
130 */
131 protected function getWebRoot()
132 {
133 if (getenv('TYPO3_PATH_WEB')) {
134 $webRoot = getenv('TYPO3_PATH_WEB');
135 } else {
136 $webRoot = getcwd();
137 }
138 return rtrim(strtr($webRoot, '\\', '/'), '/') . '/';
139 }
140 }
141
142 if (PHP_SAPI !== 'cli') {
143 die('This script supports command line usage only. Please check your command.');
144 }
145 $bootstrap = new FunctionalTestsBootstrap();
146 $bootstrap->bootstrapSystem();
147 unset($bootstrap);