AutoloaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Loader_AutoloaderTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. /**
  30. * @see Zend_Loader_Autoloader
  31. */
  32. require_once 'Zend/Loader/Autoloader.php';
  33. /**
  34. * @see Zend_Loader_Autoloader_Interface
  35. */
  36. require_once 'Zend/Loader/Autoloader/Interface.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Loader
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Loader_AutoloaderTest extends PHPUnit_Framework_TestCase
  45. {
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. public function setUp()
  52. {
  53. // Store original autoloaders
  54. $this->loaders = spl_autoload_functions();
  55. if (!is_array($this->loaders)) {
  56. // spl_autoload_functions does not return empty array when no
  57. // autoloaders registered...
  58. $this->loaders = array();
  59. }
  60. // Store original include_path
  61. $this->includePath = get_include_path();
  62. Zend_Loader_Autoloader::resetInstance();
  63. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  64. // initialize 'error' member for tests that utilize error handling
  65. $this->error = null;
  66. }
  67. public function tearDown()
  68. {
  69. // Restore original autoloaders
  70. $loaders = spl_autoload_functions();
  71. foreach ($loaders as $loader) {
  72. spl_autoload_unregister($loader);
  73. }
  74. foreach ($this->loaders as $loader) {
  75. spl_autoload_register($loader);
  76. }
  77. // Retore original include_path
  78. set_include_path($this->includePath);
  79. }
  80. public function testAutoloaderShouldBeSingleton()
  81. {
  82. $autoloader = Zend_Loader_Autoloader::getInstance();
  83. $this->assertSame($this->autoloader, $autoloader);
  84. }
  85. public function testSingletonInstanceShouldAllowReset()
  86. {
  87. Zend_Loader_Autoloader::resetInstance();
  88. $autoloader = Zend_Loader_Autoloader::getInstance();
  89. $this->assertNotSame($this->autoloader, $autoloader);
  90. }
  91. public function testAutoloaderShouldRegisterItselfWithSplAutoloader()
  92. {
  93. $autoloaders = spl_autoload_functions();
  94. $found = false;
  95. foreach ($autoloaders as $loader) {
  96. if (is_array($loader)) {
  97. if (('autoload' == $loader[1]) && ($loader[0] === get_class($this->autoloader))) {
  98. $found = true;
  99. break;
  100. }
  101. }
  102. }
  103. $this->assertTrue($found, 'Autoloader instance not found in spl_autoload stack: ' . var_export($autoloaders, 1));
  104. }
  105. public function testDefaultAutoloaderShouldBeZendLoader()
  106. {
  107. $this->assertSame(array('Zend_Loader', 'loadClass'), $this->autoloader->getDefaultAutoloader());
  108. }
  109. public function testDefaultAutoloaderShouldBeMutable()
  110. {
  111. $this->autoloader->setDefaultAutoloader(array($this, 'autoload'));
  112. $this->assertSame(array($this, 'autoload'), $this->autoloader->getDefaultAutoloader());
  113. }
  114. /**
  115. * @expectedException Zend_Loader_Exception
  116. */
  117. public function testSpecifyingInvalidDefaultAutoloaderShouldRaiseException()
  118. {
  119. $this->autoloader->setDefaultAutoloader(uniqid());
  120. }
  121. public function testZfNamespacesShouldBeRegisteredByDefault()
  122. {
  123. $namespaces = $this->autoloader->getRegisteredNamespaces();
  124. $this->assertContains('Zend_', $namespaces);
  125. $this->assertContains('ZendX_', $namespaces);
  126. }
  127. public function testAutoloaderShouldAllowRegisteringArbitraryNamespaces()
  128. {
  129. $this->autoloader->registerNamespace('Phly_');
  130. $namespaces = $this->autoloader->getRegisteredNamespaces();
  131. $this->assertContains('Phly_', $namespaces);
  132. }
  133. public function testAutoloaderShouldAllowRegisteringMultipleNamespacesAtOnce()
  134. {
  135. $this->autoloader->registerNamespace(array('Phly_', 'Solar_'));
  136. $namespaces = $this->autoloader->getRegisteredNamespaces();
  137. $this->assertContains('Phly_', $namespaces);
  138. $this->assertContains('Solar_', $namespaces);
  139. }
  140. /**
  141. * @expectedException Zend_Loader_Exception
  142. */
  143. public function testRegisteringInvalidNamespaceSpecShouldRaiseException()
  144. {
  145. $o = new stdClass;
  146. $this->autoloader->registerNamespace($o);
  147. }
  148. public function testAutoloaderShouldAllowUnregisteringNamespaces()
  149. {
  150. $this->autoloader->unregisterNamespace('Zend');
  151. $namespaces = $this->autoloader->getRegisteredNamespaces();
  152. $this->assertNotContains('Zend', $namespaces);
  153. }
  154. public function testAutoloaderShouldAllowUnregisteringMultipleNamespacesAtOnce()
  155. {
  156. $this->autoloader->unregisterNamespace(array('Zend', 'ZendX'));
  157. $namespaces = $this->autoloader->getRegisteredNamespaces();
  158. $this->assertNotContains('Zend', $namespaces);
  159. $this->assertNotContains('ZendX', $namespaces);
  160. }
  161. /**
  162. * @expectedException Zend_Loader_Exception
  163. */
  164. public function testUnregisteringInvalidNamespaceSpecShouldRaiseException()
  165. {
  166. $o = new stdClass;
  167. $this->autoloader->unregisterNamespace($o);
  168. }
  169. public function testAutoloaderSuppressNotFoundWarningsFlagShouldBeEnabledByDefault()
  170. {
  171. $this->assertTrue($this->autoloader->suppressNotFoundWarnings());
  172. }
  173. public function testAutoloaderSuppressNotFoundWarningsFlagShouldBeMutable()
  174. {
  175. $this->autoloader->suppressNotFoundWarnings(false);
  176. $this->assertFalse($this->autoloader->suppressNotFoundWarnings());
  177. }
  178. public function testFallbackAutoloaderFlagShouldBeOffByDefault()
  179. {
  180. $this->assertFalse($this->autoloader->isFallbackAutoloader());
  181. }
  182. public function testFallbackAutoloaderFlagShouldBeMutable()
  183. {
  184. $this->autoloader->setFallbackAutoloader(true);
  185. $this->assertTrue($this->autoloader->isFallbackAutoloader());
  186. }
  187. public function testUnshiftAutoloaderShouldAddToTopOfAutoloaderStack()
  188. {
  189. $this->autoloader->unshiftAutoloader('require');
  190. $autoloaders = $this->autoloader->getAutoloaders();
  191. $test = array_shift($autoloaders);
  192. $this->assertEquals('require', $test);
  193. }
  194. public function testUnshiftAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  195. {
  196. $this->autoloader->unshiftAutoloader('require');
  197. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  198. $test = array_shift($autoloaders);
  199. $this->assertEquals('require', $test);
  200. }
  201. public function testUnshiftAutoloaderShouldAllowSpecifyingSingleNamespace()
  202. {
  203. $this->autoloader->unshiftAutoloader('require', 'Foo');
  204. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  205. $test = array_shift($autoloaders);
  206. $this->assertEquals('require', $test);
  207. }
  208. public function testUnshiftAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  209. {
  210. $this->autoloader->unshiftAutoloader('require', array('Foo', 'Bar'));
  211. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  212. $test = array_shift($autoloaders);
  213. $this->assertEquals('require', $test);
  214. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  215. $test = array_shift($autoloaders);
  216. $this->assertEquals('require', $test);
  217. }
  218. public function testPushAutoloaderShouldAddToEndOfAutoloaderStack()
  219. {
  220. $this->autoloader->pushAutoloader('require');
  221. $autoloaders = $this->autoloader->getAutoloaders();
  222. $test = array_pop($autoloaders);
  223. $this->assertEquals('require', $test);
  224. }
  225. public function testPushAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  226. {
  227. $this->autoloader->pushAutoloader('require');
  228. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  229. $test = array_pop($autoloaders);
  230. $this->assertEquals('require', $test);
  231. }
  232. public function testPushAutoloaderShouldAllowSpecifyingSingleNamespace()
  233. {
  234. $this->autoloader->pushAutoloader('require', 'Foo');
  235. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  236. $test = array_pop($autoloaders);
  237. $this->assertEquals('require', $test);
  238. }
  239. public function testPushAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  240. {
  241. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'));
  242. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  243. $test = array_pop($autoloaders);
  244. $this->assertEquals('require', $test);
  245. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  246. $test = array_pop($autoloaders);
  247. $this->assertEquals('require', $test);
  248. }
  249. public function testAutoloaderShouldAllowRemovingConcreteAutoloadersFromStackByCallback()
  250. {
  251. $this->autoloader->pushAutoloader('require');
  252. $this->autoloader->removeAutoloader('require');
  253. $autoloaders = $this->autoloader->getAutoloaders();
  254. $this->assertNotContains('require', $autoloaders);
  255. }
  256. public function testRemovingAutoloaderShouldAlsoRemoveAutoloaderFromNamespacedAutoloaders()
  257. {
  258. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  259. ->pushAutoloader('include');
  260. $this->autoloader->removeAutoloader('require');
  261. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  262. $this->assertTrue(empty($test));
  263. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  264. $this->assertTrue(empty($test));
  265. }
  266. public function testAutoloaderShouldAllowRemovingCallbackFromSpecifiedNamespaces()
  267. {
  268. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  269. ->pushAutoloader('include');
  270. $this->autoloader->removeAutoloader('require', 'Foo');
  271. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  272. $this->assertTrue(empty($test));
  273. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  274. $this->assertFalse(empty($test));
  275. }
  276. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegistered()
  277. {
  278. $this->assertFalse(Zend_Loader_Autoloader::autoload('Foo_Bar'));
  279. }
  280. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegisteredButClassfileExists()
  281. {
  282. $this->addTestIncludePath();
  283. $this->assertFalse(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo'));
  284. }
  285. public function testAutoloadShouldLoadClassWhenNamespaceIsRegisteredAndClassfileExists()
  286. {
  287. $this->addTestIncludePath();
  288. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  289. $result = Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo');
  290. $this->assertFalse($result === false);
  291. $this->assertTrue(class_exists('ZendLoaderAutoloader_Foo', false));
  292. }
  293. public function testAutoloadShouldNotSuppressFileNotFoundWarningsWhenFlagIsDisabled()
  294. {
  295. $this->addTestIncludePath();
  296. $this->autoloader->suppressNotFoundWarnings(false);
  297. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  298. set_error_handler(array($this, 'handleErrors'));
  299. try {
  300. Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Bar');
  301. $this->fail('No exception raised when error suppression was disabled');
  302. } catch (Zend_Exception $e) {
  303. $this->assertContains('not found', $e->getMessage());
  304. }
  305. restore_error_handler();
  306. $this->assertNotNull($this->error);
  307. }
  308. public function testAutoloadShouldReturnTrueIfFunctionBasedAutoloaderMatchesAndReturnsNonFalseValue()
  309. {
  310. $this->autoloader->pushAutoloader('ZendLoaderAutoloader_Autoload');
  311. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  312. }
  313. public function testAutoloadShouldReturnTrueIfMethodBasedAutoloaderMatchesAndReturnsNonFalseValue()
  314. {
  315. $this->autoloader->pushAutoloader(array($this, 'autoload'));
  316. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  317. }
  318. public function testAutoloadShouldReturnTrueIfAutoloaderImplementationReturnsNonFalseValue()
  319. {
  320. $this->autoloader->pushAutoloader(new Zend_Loader_AutoloaderTest_Autoloader());
  321. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  322. }
  323. public function testUsingAlternateDefaultLoaderShouldOverrideUsageOfZendLoader()
  324. {
  325. $this->autoloader->setDefaultAutoloader(array($this, 'autoload'));
  326. $class = $this->autoloader->autoload('Zend_ThisClass_WilNever_Exist');
  327. $this->assertEquals('Zend_ThisClass_WilNever_Exist', $class);
  328. $this->assertFalse(class_exists($class, false));
  329. }
  330. public function addTestIncludePath()
  331. {
  332. set_include_path(dirname(__FILE__) . '/_files/' . PATH_SEPARATOR . $this->includePath);
  333. }
  334. public function handleErrors($errno, $errstr)
  335. {
  336. $this->error = $errstr;
  337. }
  338. public function autoload($class)
  339. {
  340. return $class;
  341. }
  342. }
  343. function ZendLoaderAutoloader_Autoload($class)
  344. {
  345. return $class;
  346. }
  347. class Zend_Loader_AutoloaderTest_Autoloader implements Zend_Loader_Autoloader_Interface
  348. {
  349. public function autoload($class)
  350. {
  351. return $class;
  352. }
  353. }
  354. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_AutoloaderTest::main') {
  355. Zend_Loader_AutoloaderTest::main();
  356. }