[BUGFIX] Failing test in autoloader with phpunit 3.6
[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 Backup of typo3CacheManager
50 */
51 protected $typo3CacheManager = NULL;
52
53 /**
54 * @var array Register of temporary extensions in typo3temp
55 */
56 protected $fakedExtensions = array();
57
58 /**
59 * Fix a race condition that t3lib_div is not available
60 * during tearDown if fiddling with the autoloader where
61 * backupGlobals is not set up again yet
62 */
63 public function setUp() {
64 $this->typo3CacheManager = $GLOBALS['typo3CacheManager'];
65 }
66
67 /**
68 * Clean up
69 * Warning: Since phpunit itself is php and we are fiddling with php
70 * autoloader code here, the tests are a bit fragile. This tearDown
71 * method ensures that all main classes are available again during
72 * tear down of a testcase.
73 * This construct will fail if the class under test is changed and
74 * not compatible anymore. Make sure to always run the whole test
75 * suite if fiddling with the autoloader unit tests to ensure that
76 * there is no fatal error thrown in other unit test classes triggered
77 * by errors in this one.
78 */
79 public function tearDown() {
80 $GLOBALS['typo3CacheManager'] = $this->typo3CacheManager;
81 t3lib_autoloader::unregisterAutoloader();
82 t3lib_autoloader::registerAutoloader();
83 foreach ($this->fakedExtensions as $extension) {
84 t3lib_div::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
85 }
86 }
87
88 /**
89 * Creates a fake extension inside typo3temp/. No configuration is created,
90 * just the folder, plus the extension is registered in $TYPO3_LOADED_EXT
91 *
92 * @return string The extension key
93 */
94 protected function createFakeExtension() {
95 $extKey = strtolower(uniqid('testing'));
96 $absExtPath = PATH_site . "typo3temp/$extKey/";
97 $relPath = "typo3temp/$extKey/";
98 t3lib_div::mkdir($absExtPath);
99
100 $GLOBALS['TYPO3_LOADED_EXT'][$extKey] = array(
101 'siteRelPath' => $relPath
102 );
103 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] .= ',' . $extKey;
104
105 $this->fakedExtensions[] = $extKey;
106 t3lib_extMgm::clearExtensionKeyMap();
107
108 return $extKey;
109 }
110
111 /**
112 * @test
113 */
114 public function UnregisterAndRegisterAgainDoesNotFatal() {
115 t3lib_autoloader::unregisterAutoloader();
116 t3lib_autoloader::registerAutoloader();
117 // If this fatals the autoload re registering went wrong
118 t3lib_div::makeInstance('t3lib_timetracknull');
119 }
120
121 /**
122 * @test
123 */
124 public function registerSetsCacheEntryWithT3libAutoloaderTag() {
125 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
126 // Expect the mock cache set method to be called
127 // once with t3lib_autoloader as third parameter
128 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), array('t3lib_autoloader'));
129 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
130 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
131 t3lib_autoloader::unregisterAutoloader();
132 t3lib_autoloader::registerAutoloader();
133 }
134
135 /**
136 * @test
137 */
138 public function autoloadFindsClassFileDefinedInExtAutoloadFile() {
139 $extKey = $this->createFakeExtension();
140 $extPath = PATH_site . "typo3temp/$extKey/";
141 $autoloaderFile = $extPath . "ext_autoload.php";
142
143 $class = strtolower("tx_${extKey}_" . uniqid(''));
144 $file = $extPath . uniqid('') . '.php';
145
146 file_put_contents($file, "<?php\n\nthrow new RuntimeException('', 1310203812);\n\n?>");
147 file_put_contents($autoloaderFile, "<?php\n\nreturn array('$class' => '$file');\n\n?>");
148
149 // Inject a dummy for the core_phpcode cache to force the autoloader
150 // to re calculate the registry
151 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
152 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
153 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
154
155 // Re-initialize autoloader registry to force it to recognize the new extension
156 t3lib_autoloader::unregisterAutoloader();
157 t3lib_autoloader::registerAutoloader();
158
159 // Expect the exception of the file to be thrown
160 $this->setExpectedException('RuntimeException', '', 1310203812);
161 t3lib_autoloader::autoload($class);
162 }
163
164 /**
165 * @test
166 */
167 public function autoloadFindsClassFileThatRespectsExtbaseNamingSchemeWithoutExtAutoloadFile() {
168 $extKey = $this->createFakeExtension();
169 $extPath = PATH_site . "typo3temp/$extKey/";
170
171 // Create a class named Tx_Extension_Foo123_Bar456
172 // to find file extension/Classes/Foo123/Bar456.php
173 $pathSegment = 'Foo' . uniqid();
174 $fileName = 'Bar' . uniqid();
175 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
176 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
177
178 t3lib_div::mkdir_deep($extPath . 'Classes/' . $pathSegment);
179 file_put_contents($file, "<?php\n\nthrow new RuntimeException('', 1310203813);\n\n?>");
180
181 // Inject a dummy for the core_phpcode cache to cache
182 // the calculated cache entry to a dummy cache
183 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
184 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
185 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
186
187 // Expect the exception of the file to be thrown
188 $this->setExpectedException('RuntimeException', '', 1310203813);
189 t3lib_autoloader::autoload($class);
190 }
191
192 /**
193 * @test
194 */
195 public function autoloadWritesClassFileThatRespectsExtbaseNamingSchemeToCacheFile() {
196 $extKey = $this->createFakeExtension();
197 $extPath = PATH_site . "typo3temp/$extKey/";
198
199 $pathSegment = 'Foo' . uniqid();
200 $fileName = 'Bar' . uniqid();
201 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
202 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
203
204 t3lib_div::mkdir_deep($extPath . 'Classes/' . $pathSegment);
205 file_put_contents($file, "<?php\n\n\$foo = 'bar';\n\n?>");
206
207 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
208 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
209 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
210
211 // Expect that an entry to the cache is written containing the newly found class
212 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains(strtolower($class), $this->anything()));
213
214 t3lib_autoloader::autoload($class);
215 }
216
217 /**
218 * @test
219 */
220 public function autoloadWritesClassFileLocationOfClassRespectingExtbaseNamingSchemeToCacheFile() {
221 $extKey = $this->createFakeExtension();
222 $extPath = PATH_site . "typo3temp/$extKey/";
223
224 $pathSegment = 'Foo' . uniqid();
225 $fileName = 'Bar' . uniqid();
226 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
227 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
228
229 t3lib_div::mkdir_deep($extPath . 'Classes/' . $pathSegment);
230 file_put_contents($file, "<?php\n\n\$foo = 'bar';\n\n?>");
231
232 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
233 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
234 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
235
236 // Expect that an entry to the cache is written containing the newly found class
237 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains(strtolower($file), $this->anything()));
238
239 t3lib_autoloader::autoload($class);
240 }
241
242 /**
243 * @test
244 */
245 public function autoloadDoesNotSetCacheEntryForClassThatRespectsExtbaseNamingSchemeOnConsecutiveCallsForSameClass() {
246 $extKey = $this->createFakeExtension();
247 $extPath = PATH_site . "typo3temp/$extKey/";
248
249 $pathSegment = 'Foo' . uniqid();
250 $fileName = 'Bar' . uniqid();
251 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
252 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
253
254 t3lib_div::mkdir_deep($extPath . 'Classes/' . $pathSegment);
255 file_put_contents($file, "<?php\n\n\$foo = 'bar';\n\n?>");
256
257 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
258 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
259 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
260
261 // Expect the set method is called exactly once, even if the class is called multiple times.
262 // This means that the internal array of the autoloader class is successfully used
263 $mockCache->expects($this->once())->method('set');
264
265 t3lib_autoloader::autoload($class);
266 t3lib_autoloader::autoload($class);
267 }
268
269 /**
270 * @test
271 */
272 public function autoloadReadsClassFileLocationFromCacheFileForClassThatRespectsExtbaseNamingScheme() {
273 $extKey = $this->createFakeExtension();
274 $extPath = PATH_site . "typo3temp/$extKey/";
275
276 $pathSegment = 'Foo' . uniqid();
277 $fileName = 'Bar' . uniqid();
278 $class = 'Tx_' . $extKey . '_' . $pathSegment . '_' . $fileName;
279 $file = $extPath . 'Classes/' . $pathSegment . '/' . $fileName . '.php';
280
281 t3lib_div::mkdir_deep($extPath . 'Classes/' . $pathSegment);
282 file_put_contents($file, "<?php\n\n\$foo = 'bar';\n\n?>");
283
284 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'), array(), '', FALSE);
285 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager', array('getCache'));
286 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
287
288 // Expect the set method is called exactly once, even if the class is called multiple times.
289 // This means that the internal array of the autoloader class is successfully used
290 $mockCache->expects($this->once())
291 ->method('requireOnce')
292 ->will($this->returnValue(array(strtolower($class) => $file)));
293
294 t3lib_autoloader::autoload($class);
295 t3lib_autoloader::unregisterAutoloader();
296 t3lib_autoloader::registerAutoloader();
297 t3lib_autoloader::autoload($class);
298 }
299 }
300 ?>