3c0a35b9a5947a815e4604cfa132292eddaed00f
[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 /**
18 * This file is defined in FuntionalTests.xml and called by phpunit
19 * before instantiating the test suites, it must also be included
20 * with phpunit parameter --bootstrap if executing single test case classes.
21 */
22 class FunctionalTestsBootstrap {
23 /**
24 * Bootstraps the system for unit tests.
25 *
26 * @return void
27 */
28 public function bootstrapSystem() {
29 $this->enableDisplayErrors()
30 ->loadClassFiles()
31 ->defineOriginalRootPath()
32 ->createNecessaryDirectoriesInDocumentRoot();
33 }
34
35 /**
36 * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
37 *
38 * @return FunctionalTestsBootstrap fluent interface
39 */
40 protected function enableDisplayErrors() {
41 @ini_set('display_errors', 1);
42 return $this;
43 }
44
45 /**
46 * Requires classes the functional test classes extend from or use for further bootstrap.
47 * Only files required for "new TestCaseClass" are required here and a general exception
48 * that is thrown by setUp() code.
49 *
50 * @return FunctionalTestsBootstrap fluent interface
51 */
52 protected function loadClassFiles() {
53 $testsDirectory = __DIR__ . '/../Tests/';
54 require_once($testsDirectory . 'BaseTestCase.php');
55 require_once($testsDirectory . 'FunctionalTestCase.php');
56 require_once($testsDirectory . 'FunctionalTestCaseBootstrapUtility.php');
57 require_once($testsDirectory . 'Exception.php');
58
59 return $this;
60 }
61
62 /**
63 * Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root.
64 *
65 * If ORIGINAL_ROOT already is defined, this method is a no-op.
66 *
67 * @return FunctionalTestsBootstrap fluent interface
68 */
69 protected function defineOriginalRootPath() {
70 if (!defined('ORIGINAL_ROOT')) {
71 /** @var string */
72 define('ORIGINAL_ROOT', $this->getWebRoot());
73 }
74
75 return $this;
76 }
77
78 /**
79 * Creates the following directories in the TYPO3 core:
80 * - typo3temp
81 *
82 * @return FunctionalTestsBootstrap fluent interface
83 */
84 protected function createNecessaryDirectoriesInDocumentRoot() {
85 $this->createDirectory(ORIGINAL_ROOT . 'typo3temp');
86
87 return $this;
88 }
89
90 /**
91 * Creates the directory $directory (recursively if required).
92 *
93 * If $directory already exists, this method is a no-op.
94 *
95 * @param string $directory absolute path of the directory to be created
96 * @return void
97 * @throws \RuntimeException
98 */
99 protected function createDirectory($directory) {
100 if (is_dir($directory)) {
101 return;
102 }
103
104 if (!mkdir($directory, 0777, TRUE)) {
105 throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
106 }
107 }
108
109 /**
110 * Returns the absolute path the TYPO3 document root.
111 *
112 * @return string the TYPO3 document root using Unix path separators
113 */
114 protected function getWebRoot() {
115 if (getenv('TYPO3_PATH_WEB')) {
116 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
117 } else {
118 $webRoot = getcwd() . '/';
119 }
120
121 return strtr($webRoot, '\\', '/');
122 }
123 }
124
125 $bootstrap = new FunctionalTestsBootstrap();
126 $bootstrap->bootstrapSystem();
127 unset($bootstrap);