AutoloaderTest.php 14 KB

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