AutoloaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. // Reset autoloader instance so it doesn't affect other tests
  80. Zend_Loader_Autoloader::resetInstance();
  81. }
  82. public function testAutoloaderShouldBeSingleton()
  83. {
  84. $autoloader = Zend_Loader_Autoloader::getInstance();
  85. $this->assertSame($this->autoloader, $autoloader);
  86. }
  87. public function testSingletonInstanceShouldAllowReset()
  88. {
  89. Zend_Loader_Autoloader::resetInstance();
  90. $autoloader = Zend_Loader_Autoloader::getInstance();
  91. $this->assertNotSame($this->autoloader, $autoloader);
  92. }
  93. public function testAutoloaderShouldRegisterItselfWithSplAutoloader()
  94. {
  95. $autoloaders = spl_autoload_functions();
  96. $found = false;
  97. foreach ($autoloaders as $loader) {
  98. if (is_array($loader)) {
  99. if (('autoload' == $loader[1]) && ($loader[0] === get_class($this->autoloader))) {
  100. $found = true;
  101. break;
  102. }
  103. }
  104. }
  105. $this->assertTrue($found, 'Autoloader instance not found in spl_autoload stack: ' . var_export($autoloaders, 1));
  106. }
  107. public function testDefaultAutoloaderShouldBeZendLoader()
  108. {
  109. $this->assertSame(array('Zend_Loader', 'loadClass'), $this->autoloader->getDefaultAutoloader());
  110. }
  111. public function testDefaultAutoloaderShouldBeMutable()
  112. {
  113. $this->autoloader->setDefaultAutoloader(array($this, 'autoload'));
  114. $this->assertSame(array($this, 'autoload'), $this->autoloader->getDefaultAutoloader());
  115. }
  116. /**
  117. * @expectedException Zend_Loader_Exception
  118. */
  119. public function testSpecifyingInvalidDefaultAutoloaderShouldRaiseException()
  120. {
  121. $this->autoloader->setDefaultAutoloader(uniqid());
  122. }
  123. public function testZfNamespacesShouldBeRegisteredByDefault()
  124. {
  125. $namespaces = $this->autoloader->getRegisteredNamespaces();
  126. $this->assertContains('Zend_', $namespaces);
  127. $this->assertContains('ZendX_', $namespaces);
  128. }
  129. public function testAutoloaderShouldAllowRegisteringArbitraryNamespaces()
  130. {
  131. $this->autoloader->registerNamespace('Phly_');
  132. $namespaces = $this->autoloader->getRegisteredNamespaces();
  133. $this->assertContains('Phly_', $namespaces);
  134. }
  135. public function testAutoloaderShouldAllowRegisteringMultipleNamespacesAtOnce()
  136. {
  137. $this->autoloader->registerNamespace(array('Phly_', 'Solar_'));
  138. $namespaces = $this->autoloader->getRegisteredNamespaces();
  139. $this->assertContains('Phly_', $namespaces);
  140. $this->assertContains('Solar_', $namespaces);
  141. }
  142. /**
  143. * @expectedException Zend_Loader_Exception
  144. */
  145. public function testRegisteringInvalidNamespaceSpecShouldRaiseException()
  146. {
  147. $o = new stdClass;
  148. $this->autoloader->registerNamespace($o);
  149. }
  150. public function testAutoloaderShouldAllowUnregisteringNamespaces()
  151. {
  152. $this->autoloader->unregisterNamespace('Zend');
  153. $namespaces = $this->autoloader->getRegisteredNamespaces();
  154. $this->assertNotContains('Zend', $namespaces);
  155. }
  156. public function testAutoloaderShouldAllowUnregisteringMultipleNamespacesAtOnce()
  157. {
  158. $this->autoloader->unregisterNamespace(array('Zend', 'ZendX'));
  159. $namespaces = $this->autoloader->getRegisteredNamespaces();
  160. $this->assertNotContains('Zend', $namespaces);
  161. $this->assertNotContains('ZendX', $namespaces);
  162. }
  163. /**
  164. * @expectedException Zend_Loader_Exception
  165. */
  166. public function testUnregisteringInvalidNamespaceSpecShouldRaiseException()
  167. {
  168. $o = new stdClass;
  169. $this->autoloader->unregisterNamespace($o);
  170. }
  171. /**
  172. * @group ZF-6536
  173. */
  174. public function testWarningSuppressionShouldBeDisabledByDefault()
  175. {
  176. $this->assertFalse($this->autoloader->suppressNotFoundWarnings());
  177. }
  178. public function testAutoloaderSuppressNotFoundWarningsFlagShouldBeMutable()
  179. {
  180. $this->autoloader->suppressNotFoundWarnings(true);
  181. $this->assertTrue($this->autoloader->suppressNotFoundWarnings());
  182. }
  183. public function testFallbackAutoloaderFlagShouldBeOffByDefault()
  184. {
  185. $this->assertFalse($this->autoloader->isFallbackAutoloader());
  186. }
  187. public function testFallbackAutoloaderFlagShouldBeMutable()
  188. {
  189. $this->autoloader->setFallbackAutoloader(true);
  190. $this->assertTrue($this->autoloader->isFallbackAutoloader());
  191. }
  192. public function testUnshiftAutoloaderShouldAddToTopOfAutoloaderStack()
  193. {
  194. $this->autoloader->unshiftAutoloader('require');
  195. $autoloaders = $this->autoloader->getAutoloaders();
  196. $test = array_shift($autoloaders);
  197. $this->assertEquals('require', $test);
  198. }
  199. public function testUnshiftAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  200. {
  201. $this->autoloader->unshiftAutoloader('require');
  202. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  203. $test = array_shift($autoloaders);
  204. $this->assertEquals('require', $test);
  205. }
  206. public function testUnshiftAutoloaderShouldAllowSpecifyingSingleNamespace()
  207. {
  208. $this->autoloader->unshiftAutoloader('require', 'Foo');
  209. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  210. $test = array_shift($autoloaders);
  211. $this->assertEquals('require', $test);
  212. }
  213. public function testUnshiftAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  214. {
  215. $this->autoloader->unshiftAutoloader('require', array('Foo', 'Bar'));
  216. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  217. $test = array_shift($autoloaders);
  218. $this->assertEquals('require', $test);
  219. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  220. $test = array_shift($autoloaders);
  221. $this->assertEquals('require', $test);
  222. }
  223. public function testPushAutoloaderShouldAddToEndOfAutoloaderStack()
  224. {
  225. $this->autoloader->pushAutoloader('require');
  226. $autoloaders = $this->autoloader->getAutoloaders();
  227. $test = array_pop($autoloaders);
  228. $this->assertEquals('require', $test);
  229. }
  230. public function testPushAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  231. {
  232. $this->autoloader->pushAutoloader('require');
  233. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  234. $test = array_pop($autoloaders);
  235. $this->assertEquals('require', $test);
  236. }
  237. public function testPushAutoloaderShouldAllowSpecifyingSingleNamespace()
  238. {
  239. $this->autoloader->pushAutoloader('require', 'Foo');
  240. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  241. $test = array_pop($autoloaders);
  242. $this->assertEquals('require', $test);
  243. }
  244. public function testPushAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  245. {
  246. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'));
  247. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  248. $test = array_pop($autoloaders);
  249. $this->assertEquals('require', $test);
  250. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  251. $test = array_pop($autoloaders);
  252. $this->assertEquals('require', $test);
  253. }
  254. public function testAutoloaderShouldAllowRemovingConcreteAutoloadersFromStackByCallback()
  255. {
  256. $this->autoloader->pushAutoloader('require');
  257. $this->autoloader->removeAutoloader('require');
  258. $autoloaders = $this->autoloader->getAutoloaders();
  259. $this->assertNotContains('require', $autoloaders);
  260. }
  261. public function testRemovingAutoloaderShouldAlsoRemoveAutoloaderFromNamespacedAutoloaders()
  262. {
  263. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  264. ->pushAutoloader('include');
  265. $this->autoloader->removeAutoloader('require');
  266. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  267. $this->assertTrue(empty($test));
  268. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  269. $this->assertTrue(empty($test));
  270. }
  271. public function testAutoloaderShouldAllowRemovingCallbackFromSpecifiedNamespaces()
  272. {
  273. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  274. ->pushAutoloader('include');
  275. $this->autoloader->removeAutoloader('require', 'Foo');
  276. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  277. $this->assertTrue(empty($test));
  278. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  279. $this->assertFalse(empty($test));
  280. }
  281. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegistered()
  282. {
  283. $this->assertFalse(Zend_Loader_Autoloader::autoload('Foo_Bar'));
  284. }
  285. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegisteredButClassfileExists()
  286. {
  287. $this->addTestIncludePath();
  288. $this->assertFalse(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo'));
  289. }
  290. public function testAutoloadShouldLoadClassWhenNamespaceIsRegisteredAndClassfileExists()
  291. {
  292. $this->addTestIncludePath();
  293. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  294. $result = Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo');
  295. $this->assertFalse($result === false);
  296. $this->assertTrue(class_exists('ZendLoaderAutoloader_Foo', false));
  297. }
  298. public function testAutoloadShouldNotSuppressFileNotFoundWarningsWhenFlagIsDisabled()
  299. {
  300. $this->addTestIncludePath();
  301. $this->autoloader->suppressNotFoundWarnings(false);
  302. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  303. set_error_handler(array($this, 'handleErrors'));
  304. $this->assertFalse(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Bar'));
  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. }