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