7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Loader
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 if (!defined('PHPUnit_MAIN_METHOD')) {
24 define('PHPUnit_MAIN_METHOD', 'Zend_Loader_AutoloaderTest::main');
30 require_once dirname(__FILE__
) . '/../../TestHelper.php';
33 * @see Zend_Loader_Autoloader
35 require_once 'Zend/Loader/Autoloader.php';
38 * @see Zend_Loader_Autoloader_Interface
40 require_once 'Zend/Loader/Autoloader/Interface.php';
44 * @package Zend_Loader
45 * @subpackage UnitTests
46 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
47 * @license http://framework.zend.com/license/new-bsd New BSD License
50 class Zend_Loader_AutoloaderTest
extends PHPUnit_Framework_TestCase
52 public static function main()
54 $suite = new PHPUnit_Framework_TestSuite(__CLASS__
);
55 $result = PHPUnit_TextUI_TestRunner
::run($suite);
58 public function setUp()
60 // Store original autoloaders
61 $this->loaders
= spl_autoload_functions();
62 if (!is_array($this->loaders
)) {
63 // spl_autoload_functions does not return empty array when no
64 // autoloaders registered...
65 $this->loaders
= array();
68 // Store original include_path
69 $this->includePath
= get_include_path();
71 Zend_Loader_Autoloader
::resetInstance();
72 $this->autoloader
= Zend_Loader_Autoloader
::getInstance();
74 // initialize 'error' member for tests that utilize error handling
78 public function tearDown()
80 // Restore original autoloaders
81 $loaders = spl_autoload_functions();
82 foreach ($loaders as $loader) {
83 spl_autoload_unregister($loader);
86 foreach ($this->loaders
as $loader) {
87 spl_autoload_register($loader);
90 // Retore original include_path
91 set_include_path($this->includePath
);
93 // Reset autoloader instance so it doesn't affect other tests
94 Zend_Loader_Autoloader
::resetInstance();
97 public function testAutoloaderShouldBeSingleton()
99 $autoloader = Zend_Loader_Autoloader
::getInstance();
100 $this->assertSame($this->autoloader
, $autoloader);
103 public function testSingletonInstanceShouldAllowReset()
105 Zend_Loader_Autoloader
::resetInstance();
106 $autoloader = Zend_Loader_Autoloader
::getInstance();
107 $this->assertNotSame($this->autoloader
, $autoloader);
110 public function testAutoloaderShouldRegisterItselfWithSplAutoloader()
112 $autoloaders = spl_autoload_functions();
114 foreach ($autoloaders as $loader) {
115 if (is_array($loader)) {
116 if (('autoload' == $loader[1]) && ($loader[0] === get_class($this->autoloader
))) {
122 $this->assertTrue($found, 'Autoloader instance not found in spl_autoload stack: ' . var_export($autoloaders, 1));
125 public function testDefaultAutoloaderShouldBeZendLoader()
127 $this->assertSame(array('Zend_Loader', 'loadClass'), $this->autoloader
->getDefaultAutoloader());
130 public function testDefaultAutoloaderShouldBeMutable()
132 $this->autoloader
->setDefaultAutoloader(array($this, 'autoload'));
133 $this->assertSame(array($this, 'autoload'), $this->autoloader
->getDefaultAutoloader());
137 * @expectedException Zend_Loader_Exception
139 public function testSpecifyingInvalidDefaultAutoloaderShouldRaiseException()
141 $this->autoloader
->setDefaultAutoloader(uniqid());
144 public function testZfNamespacesShouldBeRegisteredByDefault()
146 $namespaces = $this->autoloader
->getRegisteredNamespaces();
147 $this->assertContains('Zend_', $namespaces);
148 $this->assertContains('ZendX_', $namespaces);
151 public function testAutoloaderShouldAllowRegisteringArbitraryNamespaces()
153 $this->autoloader
->registerNamespace('Phly_');
154 $namespaces = $this->autoloader
->getRegisteredNamespaces();
155 $this->assertContains('Phly_', $namespaces);
158 public function testAutoloaderShouldAllowRegisteringMultipleNamespacesAtOnce()
160 $this->autoloader
->registerNamespace(array('Phly_', 'Solar_'));
161 $namespaces = $this->autoloader
->getRegisteredNamespaces();
162 $this->assertContains('Phly_', $namespaces);
163 $this->assertContains('Solar_', $namespaces);
167 * @expectedException Zend_Loader_Exception
169 public function testRegisteringInvalidNamespaceSpecShouldRaiseException()
172 $this->autoloader
->registerNamespace($o);
175 public function testAutoloaderShouldAllowUnregisteringNamespaces()
177 $this->autoloader
->unregisterNamespace('Zend');
178 $namespaces = $this->autoloader
->getRegisteredNamespaces();
179 $this->assertNotContains('Zend', $namespaces);
182 public function testAutoloaderShouldAllowUnregisteringMultipleNamespacesAtOnce()
184 $this->autoloader
->unregisterNamespace(array('Zend', 'ZendX'));
185 $namespaces = $this->autoloader
->getRegisteredNamespaces();
186 $this->assertNotContains('Zend', $namespaces);
187 $this->assertNotContains('ZendX', $namespaces);
191 * @expectedException Zend_Loader_Exception
193 public function testUnregisteringInvalidNamespaceSpecShouldRaiseException()
196 $this->autoloader
->unregisterNamespace($o);
202 public function testWarningSuppressionShouldBeDisabledByDefault()
204 $this->assertFalse($this->autoloader
->suppressNotFoundWarnings());
207 public function testAutoloaderSuppressNotFoundWarningsFlagShouldBeMutable()
209 $this->autoloader
->suppressNotFoundWarnings(true);
210 $this->assertTrue($this->autoloader
->suppressNotFoundWarnings());
213 public function testFallbackAutoloaderFlagShouldBeOffByDefault()
215 $this->assertFalse($this->autoloader
->isFallbackAutoloader());
218 public function testFallbackAutoloaderFlagShouldBeMutable()
220 $this->autoloader
->setFallbackAutoloader(true);
221 $this->assertTrue($this->autoloader
->isFallbackAutoloader());
224 public function testUnshiftAutoloaderShouldAddToTopOfAutoloaderStack()
226 $this->autoloader
->unshiftAutoloader('require');
227 $autoloaders = $this->autoloader
->getAutoloaders();
228 $test = array_shift($autoloaders);
229 $this->assertEquals('require', $test);
232 public function testUnshiftAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
234 $this->autoloader
->unshiftAutoloader('require');
235 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('');
236 $test = array_shift($autoloaders);
237 $this->assertEquals('require', $test);
240 public function testUnshiftAutoloaderShouldAllowSpecifyingSingleNamespace()
242 $this->autoloader
->unshiftAutoloader('require', 'Foo');
243 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Foo');
244 $test = array_shift($autoloaders);
245 $this->assertEquals('require', $test);
248 public function testUnshiftAutoloaderShouldAllowSpecifyingMultipleNamespaces()
250 $this->autoloader
->unshiftAutoloader('require', array('Foo', 'Bar'));
252 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Foo');
253 $test = array_shift($autoloaders);
254 $this->assertEquals('require', $test);
256 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Bar');
257 $test = array_shift($autoloaders);
258 $this->assertEquals('require', $test);
261 public function testPushAutoloaderShouldAddToEndOfAutoloaderStack()
263 $this->autoloader
->pushAutoloader('require');
264 $autoloaders = $this->autoloader
->getAutoloaders();
265 $test = array_pop($autoloaders);
266 $this->assertEquals('require', $test);
269 public function testPushAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
271 $this->autoloader
->pushAutoloader('require');
272 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('');
273 $test = array_pop($autoloaders);
274 $this->assertEquals('require', $test);
277 public function testPushAutoloaderShouldAllowSpecifyingSingleNamespace()
279 $this->autoloader
->pushAutoloader('require', 'Foo');
280 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Foo');
281 $test = array_pop($autoloaders);
282 $this->assertEquals('require', $test);
285 public function testPushAutoloaderShouldAllowSpecifyingMultipleNamespaces()
287 $this->autoloader
->pushAutoloader('require', array('Foo', 'Bar'));
289 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Foo');
290 $test = array_pop($autoloaders);
291 $this->assertEquals('require', $test);
293 $autoloaders = $this->autoloader
->getNamespaceAutoloaders('Bar');
294 $test = array_pop($autoloaders);
295 $this->assertEquals('require', $test);
298 public function testAutoloaderShouldAllowRemovingConcreteAutoloadersFromStackByCallback()
300 $this->autoloader
->pushAutoloader('require');
301 $this->autoloader
->removeAutoloader('require');
302 $autoloaders = $this->autoloader
->getAutoloaders();
303 $this->assertNotContains('require', $autoloaders);
306 public function testRemovingAutoloaderShouldAlsoRemoveAutoloaderFromNamespacedAutoloaders()
308 $this->autoloader
->pushAutoloader('require', array('Foo', 'Bar'))
309 ->pushAutoloader('include');
310 $this->autoloader
->removeAutoloader('require');
311 $test = $this->autoloader
->getNamespaceAutoloaders('Foo');
312 $this->assertTrue(empty($test));
313 $test = $this->autoloader
->getNamespaceAutoloaders('Bar');
314 $this->assertTrue(empty($test));
317 public function testAutoloaderShouldAllowRemovingCallbackFromSpecifiedNamespaces()
319 $this->autoloader
->pushAutoloader('require', array('Foo', 'Bar'))
320 ->pushAutoloader('include');
321 $this->autoloader
->removeAutoloader('require', 'Foo');
322 $test = $this->autoloader
->getNamespaceAutoloaders('Foo');
323 $this->assertTrue(empty($test));
324 $test = $this->autoloader
->getNamespaceAutoloaders('Bar');
325 $this->assertFalse(empty($test));
328 public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegistered()
330 $this->assertFalse(Zend_Loader_Autoloader
::autoload('Foo_Bar'));
333 public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegisteredButClassfileExists()
335 $this->addTestIncludePath();
336 $this->assertFalse(Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Foo'));
339 public function testAutoloadShouldLoadClassWhenNamespaceIsRegisteredAndClassfileExists()
341 $this->addTestIncludePath();
342 $this->autoloader
->registerNamespace('ZendLoaderAutoloader');
343 $result = Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Foo');
344 $this->assertFalse($result === false);
345 $this->assertTrue(class_exists('ZendLoaderAutoloader_Foo', false));
348 public function testAutoloadShouldNotSuppressFileNotFoundWarningsWhenFlagIsDisabled()
350 $this->addTestIncludePath();
351 $this->autoloader
->suppressNotFoundWarnings(false);
352 $this->autoloader
->registerNamespace('ZendLoaderAutoloader');
353 set_error_handler(array($this, 'handleErrors'));
354 $this->assertFalse(Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Bar'));
355 restore_error_handler();
356 $this->assertNotNull($this->error
);
359 public function testAutoloadShouldReturnTrueIfFunctionBasedAutoloaderMatchesAndReturnsNonFalseValue()
361 $this->autoloader
->pushAutoloader('ZendLoaderAutoloader_Autoload');
362 $this->assertTrue(Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Foo_Bar'));
365 public function testAutoloadShouldReturnTrueIfMethodBasedAutoloaderMatchesAndReturnsNonFalseValue()
367 $this->autoloader
->pushAutoloader(array($this, 'autoload'));
368 $this->assertTrue(Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Foo_Bar'));
371 public function testAutoloadShouldReturnTrueIfAutoloaderImplementationReturnsNonFalseValue()
373 $this->autoloader
->pushAutoloader(new Zend_Loader_AutoloaderTest_Autoloader());
374 $this->assertTrue(Zend_Loader_Autoloader
::autoload('ZendLoaderAutoloader_Foo_Bar'));
377 public function testUsingAlternateDefaultLoaderShouldOverrideUsageOfZendLoader()
379 $this->autoloader
->setDefaultAutoloader(array($this, 'autoload'));
380 $class = $this->autoloader
->autoload('Zend_ThisClass_WilNever_Exist');
381 $this->assertEquals('Zend_ThisClass_WilNever_Exist', $class);
382 $this->assertFalse(class_exists($class, false));
385 public function addTestIncludePath()
387 set_include_path(dirname(__FILE__
) . '/_files/' . PATH_SEPARATOR
. $this->includePath
);
390 public function handleErrors($errno, $errstr)
392 $this->error
= $errstr;
395 public function autoload($class)
401 function ZendLoaderAutoloader_Autoload($class)
406 class Zend_Loader_AutoloaderTest_Autoloader
implements Zend_Loader_Autoloader_Interface
408 public function autoload($class)
414 if (PHPUnit_MAIN_METHOD
== 'Zend_Loader_AutoloaderTest::main') {
415 Zend_Loader_AutoloaderTest
::main();