2
0

AutoloaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /**
  170. * @group ZF-6536
  171. */
  172. public function testWarningSuppressionShouldBeDisabledByDefault()
  173. {
  174. $this->assertFalse($this->autoloader->suppressNotFoundWarnings());
  175. }
  176. public function testAutoloaderSuppressNotFoundWarningsFlagShouldBeMutable()
  177. {
  178. $this->autoloader->suppressNotFoundWarnings(true);
  179. $this->assertTrue($this->autoloader->suppressNotFoundWarnings());
  180. }
  181. public function testFallbackAutoloaderFlagShouldBeOffByDefault()
  182. {
  183. $this->assertFalse($this->autoloader->isFallbackAutoloader());
  184. }
  185. public function testFallbackAutoloaderFlagShouldBeMutable()
  186. {
  187. $this->autoloader->setFallbackAutoloader(true);
  188. $this->assertTrue($this->autoloader->isFallbackAutoloader());
  189. }
  190. public function testUnshiftAutoloaderShouldAddToTopOfAutoloaderStack()
  191. {
  192. $this->autoloader->unshiftAutoloader('require');
  193. $autoloaders = $this->autoloader->getAutoloaders();
  194. $test = array_shift($autoloaders);
  195. $this->assertEquals('require', $test);
  196. }
  197. public function testUnshiftAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  198. {
  199. $this->autoloader->unshiftAutoloader('require');
  200. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  201. $test = array_shift($autoloaders);
  202. $this->assertEquals('require', $test);
  203. }
  204. public function testUnshiftAutoloaderShouldAllowSpecifyingSingleNamespace()
  205. {
  206. $this->autoloader->unshiftAutoloader('require', 'Foo');
  207. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  208. $test = array_shift($autoloaders);
  209. $this->assertEquals('require', $test);
  210. }
  211. public function testUnshiftAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  212. {
  213. $this->autoloader->unshiftAutoloader('require', array('Foo', 'Bar'));
  214. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  215. $test = array_shift($autoloaders);
  216. $this->assertEquals('require', $test);
  217. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  218. $test = array_shift($autoloaders);
  219. $this->assertEquals('require', $test);
  220. }
  221. public function testPushAutoloaderShouldAddToEndOfAutoloaderStack()
  222. {
  223. $this->autoloader->pushAutoloader('require');
  224. $autoloaders = $this->autoloader->getAutoloaders();
  225. $test = array_pop($autoloaders);
  226. $this->assertEquals('require', $test);
  227. }
  228. public function testPushAutoloaderWithoutNamespaceShouldRegisterAsEmptyNamespace()
  229. {
  230. $this->autoloader->pushAutoloader('require');
  231. $autoloaders = $this->autoloader->getNamespaceAutoloaders('');
  232. $test = array_pop($autoloaders);
  233. $this->assertEquals('require', $test);
  234. }
  235. public function testPushAutoloaderShouldAllowSpecifyingSingleNamespace()
  236. {
  237. $this->autoloader->pushAutoloader('require', 'Foo');
  238. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  239. $test = array_pop($autoloaders);
  240. $this->assertEquals('require', $test);
  241. }
  242. public function testPushAutoloaderShouldAllowSpecifyingMultipleNamespaces()
  243. {
  244. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'));
  245. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Foo');
  246. $test = array_pop($autoloaders);
  247. $this->assertEquals('require', $test);
  248. $autoloaders = $this->autoloader->getNamespaceAutoloaders('Bar');
  249. $test = array_pop($autoloaders);
  250. $this->assertEquals('require', $test);
  251. }
  252. public function testAutoloaderShouldAllowRemovingConcreteAutoloadersFromStackByCallback()
  253. {
  254. $this->autoloader->pushAutoloader('require');
  255. $this->autoloader->removeAutoloader('require');
  256. $autoloaders = $this->autoloader->getAutoloaders();
  257. $this->assertNotContains('require', $autoloaders);
  258. }
  259. public function testRemovingAutoloaderShouldAlsoRemoveAutoloaderFromNamespacedAutoloaders()
  260. {
  261. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  262. ->pushAutoloader('include');
  263. $this->autoloader->removeAutoloader('require');
  264. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  265. $this->assertTrue(empty($test));
  266. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  267. $this->assertTrue(empty($test));
  268. }
  269. public function testAutoloaderShouldAllowRemovingCallbackFromSpecifiedNamespaces()
  270. {
  271. $this->autoloader->pushAutoloader('require', array('Foo', 'Bar'))
  272. ->pushAutoloader('include');
  273. $this->autoloader->removeAutoloader('require', 'Foo');
  274. $test = $this->autoloader->getNamespaceAutoloaders('Foo');
  275. $this->assertTrue(empty($test));
  276. $test = $this->autoloader->getNamespaceAutoloaders('Bar');
  277. $this->assertFalse(empty($test));
  278. }
  279. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegistered()
  280. {
  281. $this->assertFalse(Zend_Loader_Autoloader::autoload('Foo_Bar'));
  282. }
  283. public function testAutoloadShouldReturnFalseWhenNamespaceIsNotRegisteredButClassfileExists()
  284. {
  285. $this->addTestIncludePath();
  286. $this->assertFalse(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo'));
  287. }
  288. public function testAutoloadShouldLoadClassWhenNamespaceIsRegisteredAndClassfileExists()
  289. {
  290. $this->addTestIncludePath();
  291. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  292. $result = Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo');
  293. $this->assertFalse($result === false);
  294. $this->assertTrue(class_exists('ZendLoaderAutoloader_Foo', false));
  295. }
  296. public function testAutoloadShouldNotSuppressFileNotFoundWarningsWhenFlagIsDisabled()
  297. {
  298. $this->addTestIncludePath();
  299. $this->autoloader->suppressNotFoundWarnings(false);
  300. $this->autoloader->registerNamespace('ZendLoaderAutoloader');
  301. set_error_handler(array($this, 'handleErrors'));
  302. $this->assertFalse(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Bar'));
  303. restore_error_handler();
  304. $this->assertNotNull($this->error);
  305. }
  306. public function testAutoloadShouldReturnTrueIfFunctionBasedAutoloaderMatchesAndReturnsNonFalseValue()
  307. {
  308. $this->autoloader->pushAutoloader('ZendLoaderAutoloader_Autoload');
  309. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  310. }
  311. public function testAutoloadShouldReturnTrueIfMethodBasedAutoloaderMatchesAndReturnsNonFalseValue()
  312. {
  313. $this->autoloader->pushAutoloader(array($this, 'autoload'));
  314. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  315. }
  316. public function testAutoloadShouldReturnTrueIfAutoloaderImplementationReturnsNonFalseValue()
  317. {
  318. $this->autoloader->pushAutoloader(new Zend_Loader_AutoloaderTest_Autoloader());
  319. $this->assertTrue(Zend_Loader_Autoloader::autoload('ZendLoaderAutoloader_Foo_Bar'));
  320. }
  321. public function testUsingAlternateDefaultLoaderShouldOverrideUsageOfZendLoader()
  322. {
  323. $this->autoloader->setDefaultAutoloader(array($this, 'autoload'));
  324. $class = $this->autoloader->autoload('Zend_ThisClass_WilNever_Exist');
  325. $this->assertEquals('Zend_ThisClass_WilNever_Exist', $class);
  326. $this->assertFalse(class_exists($class, false));
  327. }
  328. public function addTestIncludePath()
  329. {
  330. set_include_path(dirname(__FILE__) . '/_files/' . PATH_SEPARATOR . $this->includePath);
  331. }
  332. public function handleErrors($errno, $errstr)
  333. {
  334. $this->error = $errstr;
  335. }
  336. public function autoload($class)
  337. {
  338. return $class;
  339. }
  340. }
  341. function ZendLoaderAutoloader_Autoload($class)
  342. {
  343. return $class;
  344. }
  345. class Zend_Loader_AutoloaderTest_Autoloader implements Zend_Loader_Autoloader_Interface
  346. {
  347. public function autoload($class)
  348. {
  349. return $class;
  350. }
  351. }
  352. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_AutoloaderTest::main') {
  353. Zend_Loader_AutoloaderTest::main();
  354. }