[FEATURE] Use caching framework in autoloader
[Packages/TYPO3.CMS.git] / tests / t3lib / class.t3lib_autoloaderTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the t3lib_autoloader class in the TYPO3 Core.
27 *
28 * @package TYPO3
29 * @subpackage t3lib
30 *
31 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
32 */
33 class t3lib_autoloaderTest extends Tx_Phpunit_TestCase {
34
35 /**
36 * @var boolean Enable backup of global and system variables
37 */
38 protected $backupGlobals = TRUE;
39
40 /**
41 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
42 * because resource types cannot be handled during serializing
43 *
44 * @var array
45 */
46 protected $backupGlobalsBlacklist = array('TYPO3_DB');
47
48 /**
49 * @var array Register of temporary extensions in typo3temp
50 */
51 protected $fakedExtensions = array();
52
53 /**
54 * Clean up
55 */
56 public function tearDown() {
57 foreach ($this->fakedExtensions as $extension) {
58 t3lib_div::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
59 }
60 }
61
62 /**
63 * Creates a fake extension inside typo3temp/. No configuration is created,
64 * just the folder, plus the extension is registered in $TYPO3_LOADED_EXT
65 *
66 * @return string The extension key
67 */
68 protected function createFakeExtension() {
69 $extKey = uniqid('testing');
70 $absExtPath = PATH_site . "typo3temp/$extKey/";
71 $relPath = "typo3temp/$extKey/";
72 t3lib_div::mkdir($absExtPath);
73
74 $GLOBALS['TYPO3_LOADED_EXT'][$extKey] = array(
75 'siteRelPath' => $relPath
76 );
77 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] .= ',' . $extKey;
78
79 $this->fakedExtensions[] = $extKey;
80 t3lib_extMgm::clearExtensionKeyMap();
81
82 return $extKey;
83 }
84
85 /**
86 * @test
87 */
88 public function autoloaderCanBeUnregisteredAndRegisteredAgain() {
89 t3lib_autoloader::unregisterAutoloader();
90 t3lib_autoloader::registerAutoloader();
91 t3lib_div::makeInstance('t3lib_timetracknull');
92 }
93
94 /**
95 * @test
96 */
97 public function extensionAutoloadFileIsIncludedIfAvailable() {
98 $extKey = $this->createFakeExtension();
99 $extPath = PATH_site . "typo3temp/$extKey/";
100 $autoloaderFile = $extPath . "ext_autoload.php";
101
102 $class = strtolower("tx_${extKey}_" . uniqid(''));
103 $file = $extPath . uniqid('') . '.php';
104
105 file_put_contents($file, "<?php\n\nthrow new RuntimeException('', 1310203812);\n\n?>");
106 file_put_contents($autoloaderFile, "<?php\n\nreturn array('$class' => '$file');\n\n?>");
107
108 // Inject a dummy for the core_phpcode cache to force the autoloader
109 // to re calculate the registry
110 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
111 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
112 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
113
114 // Re-initialize autoloader registry to force it to recognize the new extension
115 t3lib_autoloader::unregisterAutoloader();
116 t3lib_autoloader::registerAutoloader();
117
118 // Expect the exception of the file to be thrown
119 $this->setExpectedException('RuntimeException', '', 1310203812);
120 t3lib_autoloader::autoload($class);
121 }
122 }
123 ?>