15f7257e16cf4b9e049859245d1fdfc963bb0521
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / AutoloaderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for TYPO3\CMS\Core\Autoloader
29 *
30 * @package TYPO3
31 * @subpackage t3lib
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 */
34 class AutoloaderTest extends \Tx_Phpunit_TestCase {
35
36 /**
37 * @var boolean Enable backup of global and system variables
38 */
39 protected $backupGlobals = TRUE;
40
41 /**
42 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
43 * because resource types cannot be handled during serializing
44 *
45 * @var array
46 */
47 protected $backupGlobalsBlacklist = array('TYPO3_DB');
48
49 /**
50 * @var array Backup of typo3CacheManager
51 */
52 protected $typo3CacheManager = NULL;
53
54 /**
55 * @var array Register of temporary extensions in typo3temp
56 */
57 protected $fakedExtensions = array();
58
59 /**
60 * Fix a race condition that t3lib_div is not available
61 * during tearDown if fiddling with the autoloader where
62 * backupGlobals is not set up again yet
63 */
64 public function setUp() {
65 $this->typo3CacheManager = $GLOBALS['typo3CacheManager'];
66 }
67
68 /**
69 * Clean up
70 * Warning: Since phpunit itself is php and we are fiddling with php
71 * autoloader code here, the tests are a bit fragile. This tearDown
72 * method ensures that all main classes are available again during
73 * tear down of a testcase.
74 * This construct will fail if the class under test is changed and
75 * not compatible anymore. Make sure to always run the whole test
76 * suite if fiddling with the autoloader unit tests to ensure that
77 * there is no fatal error thrown in other unit test classes triggered
78 * by errors in this one.
79 */
80 public function tearDown() {
81 $GLOBALS['typo3CacheManager'] = $this->typo3CacheManager;
82 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
83 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
84 foreach ($this->fakedExtensions as $extension) {
85 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
86 }
87 }
88
89 /**
90 * Creates a fake extension inside typo3temp/. No configuration is created,
91 * just the folder, plus the extension is registered in $TYPO3_LOADED_EXT
92 *
93 * @return string The extension key
94 */
95 protected function createFakeExtension() {
96 $extKey = strtolower(uniqid('testing'));
97 $absExtPath = PATH_site . 'typo3temp/' . $extKey . '/';
98 $relPath = 'typo3temp/' . $extKey . '/';
99 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
100 $GLOBALS['TYPO3_LOADED_EXT'][$extKey] = array(
101 'siteRelPath' => $relPath
102 );
103 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray'][] = $extKey;
104 $this->fakedExtensions[] = $extKey;
105 \TYPO3\CMS\Core\Extension\ExtensionManager::clearExtensionKeyMap();
106 return $extKey;
107 }
108
109 /**
110 * @test
111 */
112 public function unregisterAndRegisterAgainDoesNotFatal() {
113 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
114 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
115 // If this fatals the autoload re registering went wrong
116 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TimeTracker\\NullTimeTracker');
117 }
118
119 /**
120 * @test
121 */
122 public function unregisterAutoloaderSetsCacheEntryWithT3libNoTags() {
123 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
124 // Expect the mock cache set method to be called
125 // once with t3lib_autoloader as third parameter
126 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), array());
127 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
128 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
129 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
130 }
131
132 /**
133 * @test
134 * @expectedException \RuntimeException
135 */
136 public function autoloadFindsClassFileDefinedInExtAutoloadFile() {
137 $extKey = $this->createFakeExtension();
138 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
139 $autoloaderFile = $extPath . 'ext_autoload.php';
140 $class = strtolower("tx_{$extKey}_" . uniqid(''));
141 $file = $extPath . uniqid('') . '.php';
142 file_put_contents($file, '<?php
143
144 throw new \\RuntimeException(\'\', 1310203812);
145
146 ?>');
147 file_put_contents($autoloaderFile, '<?php
148
149 return array(\'' . $class . '\' => \'' . $file . '\');
150
151 ?>');
152 // Inject a dummy for the core_phpcode cache to force the autoloader
153 // to re calculate the registry
154 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
155 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
156 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
157 // Re-initialize autoloader registry to force it to recognize the new extension
158 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
159 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
160 // Expect the exception of the file to be thrown
161 \TYPO3\CMS\Core\Autoloader::autoload($class);
162 }
163
164 /**
165 * @test
166 */
167 public function unregisterAutoloaderWritesLowerCasedClassFileToCache() {
168 $extKey = $this->createFakeExtension();
169 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
170 $autoloaderFile = $extPath . 'ext_autoload.php';
171 // A case sensitive key (FooBar) in ext_autoload file
172 $class = "tx_{$extKey}_" . uniqid('FooBar');
173 $file = $extPath . uniqid('') . '.php';
174 file_put_contents($autoloaderFile, '<?php
175
176 return array(\'' . $class . '\' => \'' . $file . '\');
177
178 ?>');
179 // Inject a dummy for the core_phpcode cache to force the autoloader
180 // to re calculate the registry
181 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
182 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
183 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
184 // Expect that the lower case version of the class name is written to cache
185 $mockCache->expects($this->at(2))->method('set')->with($this->anything(), $this->stringContains(strtolower($class), FALSE));
186 // Re-initialize autoloader registry to force it to recognize the new extension
187 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
188 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
189 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
190 }
191
192 /**
193 * @test
194 * @expectedException \RuntimeException
195 */
196 public function autoloadFindsClassFileIfExtAutoloadEntryIsCamelCased() {
197 $extKey = $this->createFakeExtension();
198 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
199 // A case sensitive key (FooBar) in ext_autoload file
200 $class = "tx_{$extKey}_" . uniqid('FooBar');
201 $file = $extPath . uniqid('') . '.php';
202 file_put_contents($file, '<?php
203
204 throw new \\RuntimeException(\'\', 1336756850);
205
206 ?>');
207 $extAutoloadFile = $extPath . 'ext_autoload.php';
208 file_put_contents($extAutoloadFile, '<?php
209
210 return array(\'' . $class . '\' => \'' . $file . '\');
211
212 ?>');
213 // Inject cache and return false, so autoloader is forced to read ext_autoloads from extensions
214 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
215 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
216 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
217 $mockCache->expects($this->any())->method('has')->will($this->returnValue(FALSE));
218 // Re-initialize autoloader registry to force it to recognize the new extension
219 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
220 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
221 \TYPO3\CMS\Core\Autoloader::autoload($class);
222 }
223
224 /**
225 * @test
226 * @expectedException \RuntimeException
227 */
228 public function autoloadFindsCamelCasedClassFileIfExtAutoloadEntryIsReadLowerCasedFromCache() {
229 $extKey = $this->createFakeExtension();
230 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
231 // A case sensitive key (FooBar) in ext_autoload file
232 $class = "tx_{$extKey}_" . uniqid('FooBar');
233 $file = $extPath . uniqid('') . '.php';
234 file_put_contents($file, '<?php
235
236 throw new \RuntimeException(\'\', 1336756850);
237
238 ?>');
239 // Inject cache mock and let the cache entry return the lowercased class name as key
240 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
241 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
242 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
243 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
244 $mockCache->expects($this->once())->method('requireOnce')->will($this->returnValue(array(strtolower($class) => $file)));
245 // Re-initialize autoloader registry to force it to recognize the new extension
246 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
247 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
248 \TYPO3\CMS\Core\Autoloader::autoload($class);
249 }
250
251 /**
252 * @test
253 * @expectedException \RuntimeException
254 */
255 public function autoloadFindsClassFileThatRespectsExtbaseNamingSchemeWithoutExtAutoloadFile() {
256 $extKey = $this->createFakeExtension();
257 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
258 // Create a class named Tx_Extension_Foo123_Bar456
259 // to find file extension/Classes/Foo123/Bar456.php
260 $pathSegment = 'Foo' . uniqid();
261 $fileName = 'Bar' . uniqid();
262 $class = 'Tx_' . ucfirst($extKey) . '_' . $pathSegment . '_' . $fileName;
263
264 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
265 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($extPath . 'Classes/' . $pathSegment);
266 file_put_contents($file, '<?php
267
268 throw new \\RuntimeException(\'\', 1310203813);
269
270 ?>');
271 // Inject a dummy for the core_phpcode cache to cache
272 // the calculated cache entry to a dummy cache
273 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
274 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
275 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
276 // Expect the exception of the file to be thrown
277 \TYPO3\CMS\Core\Autoloader::autoload($class);
278 }
279
280 /**
281 * @test
282 */
283 public function unregisterAutoloaderWritesClassFileThatRespectsExtbaseNamingSchemeToCacheFile() {
284 $this->markTestSkipped('This test was a blocker for other namespaced tests.');
285 $extKey = $this->createFakeExtension();
286 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
287 $pathSegment = 'Foo' . uniqid();
288 $fileName = 'Bar' . uniqid();
289 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
290 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
291 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($extPath . 'Classes/' . $pathSegment);
292 file_put_contents($file, '<?php
293
294 $foo = \'bar\';
295
296 ?>');
297 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
298 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
299 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
300 // Expect that an entry to the cache is written containing the newly found class
301 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains(strtolower($class), $this->anything()));
302 \TYPO3\CMS\Core\Autoloader::autoload($class);
303 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
304 }
305
306 /**
307 * @test
308 */
309 public function unregisterAutoloaderWritesClassFileLocationOfClassRespectingExtbaseNamingSchemeToCacheFile() {
310 $this->markTestSkipped('This test was a blocker for other namespaced tests.');
311 $extKey = $this->createFakeExtension();
312 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
313 $pathSegment = 'Foo' . uniqid();
314 $fileName = 'Bar' . uniqid();
315 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
316 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
317 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($extPath . 'Classes/' . $pathSegment);
318 file_put_contents($file, '<?php
319
320 $foo = \'bar\';
321
322 ?>');
323 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
324 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
325 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
326 // Expect that an entry to the cache is written containing the newly found class
327 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains(strtolower($file), $this->anything()));
328 \TYPO3\CMS\Core\Autoloader::autoload($class);
329 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
330 }
331
332 /**
333 * @test
334 */
335 public function getClassPathByRegistryLookupFindsClassPrefixedWithUxRegisteredInExtAutoloadFile() {
336 // Create a dummy extension with a path to a 'ux_' prefixed php file
337 $extKey = $this->createFakeExtension();
338 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
339 $autoloaderFile = $extPath . 'ext_autoload.php';
340 $class = strtolower("ux_tx_{$extKey}_" . uniqid(''));
341 $file = $extPath . uniqid('') . '.php';
342 file_put_contents($autoloaderFile, '<?php
343
344 return array(\'' . $class . '\' => \'' . $file . '\');
345
346 ?>');
347 // Inject a dummy for the core_phpcode cache to force the autoloader
348 // to re calculate the registry
349 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
350 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
351 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
352 // Re-initialize autoloader registry to force it to recognize the new extension with the ux_ autoload definition
353 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
354 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
355 $this->assertSame($file, \TYPO3\CMS\Core\Autoloader::getClassPathByRegistryLookup($class));
356 }
357
358 /**
359 * @test
360 */
361 public function unregisterAutoloaderWritesNotExistingUxCLassLookupFromGetClassPathByRegistryLookupToCache() {
362 $uxClassName = 'ux_Tx_Foo' . uniqid();
363 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
364 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
365 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
366 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
367 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
368 // Class is not found by returning NULL
369 $this->assertSame(NULL, \TYPO3\CMS\Core\Autoloader::getClassPathByRegistryLookup($uxClassName));
370 // Expect NULL lookup is cached
371 $expectedCacheString = '\'' . strtolower($uxClassName) . '\' => NULL,';
372 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($expectedCacheString));
373 // Trigger writing new cache file
374 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
375 }
376
377 /**
378 * @test
379 */
380 public function unregisterAutoloaderWritesDeprecatedTypo3ConfVarsRegisteredXclassClassFoundByGetClassPathByRegistryLookupToCache() {
381 // Create a fake extension
382 $extKey = $this->createFakeExtension();
383 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
384 $autoloaderFile = $extPath . 'ext_autoload.php';
385 // Feed ext_autoload with a base file and the class file
386 $class = strtolower("tx_{$extKey}_" . uniqid(''));
387 $fileName = uniqid('') . '.php';
388 $file = $extPath . $fileName;
389 $xClassFile = 'typo3temp/' . $extKey . '/xclassFile';
390 file_put_contents($autoloaderFile, '<?php
391
392 return array(\'' . $class . '\' => \'' . $file . '\');
393
394 ?>');
395 file_put_contents(PATH_site . $xClassFile, '<?php
396
397 die();
398
399 ?>');
400 // Register a xclass for the base file
401 $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3temp/' . $extKey . '/' . $fileName] = $xClassFile;
402 // Inject a dummy for the core_phpcode cache to force the autoloader
403 // to re calculate the registry
404 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
405 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
406 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
407 // Excpect the cache entry to be called once with the new class name
408 $mockCache->expects($this->at(2))->method('set')->with($this->anything(), $this->stringContains('ux_' . $class));
409 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
410 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
411 \TYPO3\CMS\Core\Autoloader::getClassPathByRegistryLookup('ux_' . $class);
412 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
413 }
414
415 /**
416 * @test
417 * @expectedException \RuntimeException
418 */
419 public function autoloadFindsClassFileThatRespectsExtbaseNamingSchemeWithNamespace() {
420 $extKey = $this->createFakeExtension();
421 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
422 // Create a class named \Tx\Extension\Foo123\Bar456
423 // to find file extension/Classes/Foo123/Bar456.php
424 $pathSegment = 'Foo' . uniqid();
425 $fileName = 'Bar' . uniqid();
426 $namespacedClass = '\\Vendor\\' . ucfirst($extKey) . '\\' . $pathSegment . '\\' . $fileName;
427 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
428 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($extPath . 'Classes/' . $pathSegment);
429 file_put_contents($file, '<?php' . LF . 'throw new \\RuntimeException(\'\', 1342800577);' . LF . '?>');
430 // Re-initialize autoloader registry to force it to recognize the new extension
431 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
432 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
433 // Expect the exception of the file to be thrown
434 \TYPO3\CMS\Core\Autoloader::autoload($namespacedClass);
435 }
436
437 /**
438 * @test
439 */
440 public function unregisterAutoloaderWritesClassFileLocationOfClassRespectingExtbaseNamingSchemeWithNamespaceToCacheFile() {
441 $this->markTestSkipped('This test was a blocker for other namespaced tests.');
442 $extKey = $this->createFakeExtension();
443 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
444 $pathSegment = 'Foo' . uniqid();
445 $fileName = 'Bar' . uniqid();
446 $namespacedClass = '\\Tx\\' . $extKey . '\\' . $pathSegment . '\\' . $fileName;
447 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
448 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($extPath . 'Classes/' . $pathSegment);
449 file_put_contents($file, '<?php' . LF . ($foo = 'bar;' . LF . '?>'));
450 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
451 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
452 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
453 // Expect that an entry to the cache is written containing the newly found class
454 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains(strtolower($file), $this->anything()));
455 \TYPO3\CMS\Core\Autoloader::autoload($namespacedClass);
456 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
457 }
458
459 /**
460 * @test
461 */
462 public function checkClassNamesNotExtbaseSchemePassAutoloaderUntouched() {
463 $class = '\\Symfony\\Foo\\Bar';
464 $this->assertNull(\TYPO3\CMS\Core\Autoloader::getClassPathByRegistryLookup($class));
465 }
466
467 /**
468 * @test
469 */
470 public function checkAutoloaderSetsNamespacedClassnamesInExtAutoloadAreWrittenToCache() {
471 $extKey = $this->createFakeExtension();
472 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
473 $pathSegment = 'Foo' . uniqid();
474 $fileName = 'Bar' . uniqid();
475 $autoloaderFile = $extPath . 'ext_autoload.php';
476 // A case sensitive key (FooBar) in ext_autoload file
477 $namespacedClass = '\\Tx\\' . $extKey . '\\' . $pathSegment . '\\' . $fileName;
478 file_put_contents($autoloaderFile, '<?php' . LF . 'return array(\'' . $namespacedClass . '\' => \'EXT:someExt/Classes/Foo/bar.php\');' . LF . '?>');
479 // Inject a dummy for the core_phpcode cache to force the autoloader
480 // to re calculate the registry
481 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
482 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
483 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
484 // Expect that the lower case version of the class name is written to cache
485 $mockCache->expects($this->at(2))->method('set')->with($this->anything(), $this->stringContains(strtolower($namespacedClass), FALSE));
486 // Re-initialize autoloader registry to force it to recognize the new extension
487 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
488 \TYPO3\CMS\Core\Autoloader::registerAutoloader();
489 \TYPO3\CMS\Core\Autoloader::unregisterAutoloader();
490 }
491
492 }
493
494 ?>