AutoloaderTest.php 16 KB

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