[TASK] Encapsulate bootstrap base code
[Packages/TYPO3.CMS.git] / tests / Unit / typo3 / Classes / Bootstrap / BaseSetupTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for class Typo3_Bootstrap_BaseSetup
30 *
31 * @author Christia Kuhn <lolli@schwarbu.ch>
32 * @package TYPO3
33 * @subpackage tests
34 */
35 class Typo3_Bootstrap_BaseSetupTest extends tx_phpunit_testcase {
36
37 /**
38 * Enable backup of global and system variables
39 *
40 * @var boolean
41 */
42 protected $backupGlobals = TRUE;
43
44 /**
45 * Exclude TYPO3_DB from backup/ restore of $GLOBALS because of included ressource
46 *
47 * @var array
48 */
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
50
51 /**
52 * Set up testcase
53 *
54 * @return void
55 */
56 public function setUp() {
57 $this->createAccessibleProxyClass();
58 }
59
60 /**
61 * Create a subclass of Typo3_Bootstrap_BaseSetup with
62 * protected methods made public
63 *
64 * @return void
65 */
66 protected function createAccessibleProxyClass() {
67 $className = 'Typo3_Bootstrap_BaseSetupAccessibleProxy';
68 if (!class_exists($className, FALSE)) {
69 eval(
70 'class ' . $className . ' extends Typo3_Bootstrap_BaseSetup {' .
71 ' public static function getPathThisScriptCli() {' .
72 ' return parent::getPathThisScriptCli();' .
73 ' }' .
74 ' public static function getUnifiedDirectoryNameWithTrailingSlash($absolutePath) {' .
75 ' return parent::getUnifiedDirectoryNameWithTrailingSlash($absolutePath);' .
76 ' }' .
77 ' public static function addCorePearPathToIncludePath() {' .
78 ' return parent::addCorePearPathToIncludePath();' .
79 ' }' .
80 ' public static function initializeGlobalVariables() {' .
81 ' return parent::initializeGlobalVariables();' .
82 ' }' .
83 ' public static function loadDefaultTypo3ConfVars() {' .
84 ' return parent::loadDefaultTypo3ConfVars();' .
85 ' }' .
86 ' public static function initializeGlobalTimeTrackingVariables() {' .
87 ' return parent::initializeGlobalTimeTrackingVariables();' .
88 ' }' .
89 ' public static function initializeBasicErrorReporting() {' .
90 ' return parent::initializeBasicErrorReporting();' .
91 ' }' .
92 '}'
93 );
94 }
95 }
96
97 /**
98 * @test
99 */
100 public function getPathThisScriptCliReadsLocalPartFromArgv() {
101 $fakedLocalPart = uniqid('Test');
102 $GLOBALS['_SERVER']['argv'][0] = $fakedLocalPart;
103 $this->assertStringEndsWith($fakedLocalPart, Typo3_Bootstrap_BaseSetupAccessibleProxy::getPathThisScriptCli());
104 }
105
106 /**
107 * @test
108 */
109 public function getPathThisScriptCliReadsLocalPartFromEnv() {
110 $fakedLocalPart = uniqid('Test');
111 unset($GLOBALS['_SERVER']['argv']);
112 $GLOBALS['_ENV']['_'] = $fakedLocalPart;
113 $this->assertStringEndsWith($fakedLocalPart, Typo3_Bootstrap_BaseSetupAccessibleProxy::getPathThisScriptCli());
114 }
115
116 /**
117 * @test
118 */
119 public function getPathThisScriptCliReadsLocalPartFromServer() {
120 $fakedLocalPart = uniqid('Test');
121 unset($GLOBALS['_SERVER']['argv']);
122 unset($GLOBALS['_ENV']['_']);
123 $GLOBALS['_SERVER']['_'] = $fakedLocalPart;
124 $this->assertStringEndsWith($fakedLocalPart, Typo3_Bootstrap_BaseSetupAccessibleProxy::getPathThisScriptCli());
125 }
126
127 /**
128 * @test
129 */
130 public function getPathThisScriptCliAddsCurrentWorkingDirectoryFromServerEnvironmentToLocalPathOnUnix() {
131 if (TYPO3_OS === 'WIN') {
132 $this->markTestSkipped('Test not available on Windows OS.');
133 }
134
135 $GLOBALS['_SERVER']['argv'][0] = 'foo';
136 $fakedAbsolutePart = '/' . uniqid('Absolute') . '/';
137 $_SERVER['PWD'] = $fakedAbsolutePart;
138 $this->assertStringStartsWith($fakedAbsolutePart, Typo3_Bootstrap_BaseSetupAccessibleProxy::getPathThisScriptCli());
139 }
140
141 /**
142 * @test
143 */
144 public function getUnifiedDirectoryNameWithTrailingSlashReturnsCorrectPathOnUnix() {
145 if (TYPO3_OS === 'WIN') {
146 $this->markTestSkipped('Test not available on Windows OS.');
147 }
148 $input = '/foo/bar/test.php';
149 $expected = '/foo/bar/';
150 $actual = Typo3_Bootstrap_BaseSetupAccessibleProxy::getUnifiedDirectoryNameWithTrailingSlash($input);
151 $this->assertEquals($expected, $actual);
152 }
153
154 /**
155 * @test
156 */
157 public function addCorePearPathToIncludePathAddsTypo3ContribPearToPathAsFirstEntry() {
158 $backupPath = get_include_path();
159 Typo3_Bootstrap_BaseSetupAccessibleProxy::addCorePearPathToIncludePath();
160 $actualValue = get_include_path();
161 set_include_path($backupPath);
162 $this->assertStringStartsWith(PATH_typo3 . 'contrib/pear/' . PATH_SEPARATOR, $actualValue);
163 }
164
165 /**
166 * @test
167 */
168 public function initializeGlobalVariablesUnsetsGlobalErrorArray() {
169 $GLOBALS['error'] = 'foo';
170 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalVariables();
171 $this->assertFalse(isset($GLOBALS['error']));
172 }
173
174 /**
175 * @test
176 */
177 public function initializeGlobalVariablesSetsGlobalClientArray() {
178 unset($GLOBALS['CLIENT']);
179 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalVariables();
180 $this->assertArrayHasKey('CLIENT', $GLOBALS);
181 }
182
183 /**
184 * @test
185 */
186 public function initializeGlobalVariablesSetsGlobalTypo3MiscArray() {
187 unset($GLOBALS['TYPO3_MISC']);
188 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalVariables();
189 $this->assertInternalType('array', $GLOBALS['TYPO3_MISC']);
190 }
191
192 /**
193 * @test
194 */
195 public function initializeGlobalVariablesSetsGlobalT3VarArray() {
196 unset($GLOBALS['T3_VAR']);
197 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalVariables();
198 $this->assertInternalType('array', $GLOBALS['T3_VAR']);
199 }
200
201 /**
202 * @test
203 */
204 public function initializeGlobalVariablesSetsGlobalT3ServicesArray() {
205 unset($GLOBALS['T3_SERVICES']);
206 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalVariables();
207 $this->assertInternalType('array', $GLOBALS['T3_SERVICES']);
208 }
209
210 /**
211 * @test
212 */
213 public function loadDefaultTypo3ConfVarsPopulatesTypo3ConfVarsArray() {
214 unset($GLOBALS['TYPO3_CONF_VARS']);
215 Typo3_Bootstrap_BaseSetupAccessibleProxy::loadDefaultTypo3ConfVars();
216 $this->assertInternalType('array', $GLOBALS['TYPO3_CONF_VARS']);
217 }
218
219 /**
220 * Data provider for initializeGlobalTimeTrackingVariablesSetsGlobalVariables
221 *
222 * @return array
223 */
224 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider() {
225 return array(
226 'PARSETIME_START' => array('PARSETIME_START'),
227 'EXEC_TIME' => array('EXEC_TIME'),
228 'ACCESS_TIME' => array('ACCESS_TIME'),
229 'SIM_EXEC_TIME' => array('SIM_EXEC_TIME'),
230 'SIM_ACCESS_TIME' => array('SIM_ACCESS_TIME'),
231 );
232 }
233
234 /**
235 * @test
236 * @dataProvider initializeGlobalTimeTrackingVariablesSetsGlobalVariablesDataProvider
237 * @param string $variable Variable to check for in $GLOBALS
238 */
239 public function initializeGlobalTimeTrackingVariablesSetsGlobalVariables($variable) {
240 unset($GLOBALS[$variable]);
241 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalTimeTrackingVariables();
242 $this->assertTrue(isset($GLOBALS[$variable]));
243 }
244
245 /**
246 * @test
247 */
248 public function initializeGlobalTimeTrackingVariablesSetsGlobalTypo3MiscMicrotimeStart() {
249 unset($GLOBALS['TYPO3_MISC']['microtime_start']);
250 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalTimeTrackingVariables();
251 $this->assertTrue(isset($GLOBALS['TYPO3_MISC']['microtime_start']));
252 }
253
254 /**
255 * @test
256 */
257 public function initializeGlobalTimeTrackingVariablesRoundsAccessTimeToSixtySeconds() {
258 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalTimeTrackingVariables();
259 $this->assertEquals(0, $GLOBALS['ACCESS_TIME'] % 60);
260 }
261
262 /**
263 * @test
264 */
265 public function initializeGlobalTimeTrackingVariablesRoundsSimAccessTimeToSixtySeconds() {
266 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeGlobalTimeTrackingVariables();
267 $this->assertEquals(0, $GLOBALS['SIM_ACCESS_TIME'] % 60);
268 }
269
270 /**
271 * @test
272 */
273 public function initializeBasicErrorReportingExcludesStrict() {
274 $backupReporting = error_reporting();
275 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeBasicErrorReporting();
276 $actualReporting = error_reporting();
277 error_reporting($backupReporting);
278 $this->assertEquals(0, $actualReporting & E_STRICT);
279 }
280
281 /**
282 * @test
283 */
284 public function initializeBasicErrorReportingExcludesNotice() {
285 $backupReporting = error_reporting();
286 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeBasicErrorReporting();
287 $actualReporting = error_reporting();
288 error_reporting($backupReporting);
289 $this->assertEquals(0, $actualReporting & E_NOTICE);
290 }
291
292 /**
293 * @test
294 */
295 public function initializeBasicErrorReportingExcludesDeprecated() {
296 $backupReporting = error_reporting();
297 Typo3_Bootstrap_BaseSetupAccessibleProxy::initializeBasicErrorReporting();
298 $actualReporting = error_reporting();
299 error_reporting($backupReporting);
300 $this->assertEquals(0, $actualReporting & E_DEPRECATED);
301 }
302 }
303 ?>