AutoloaderFactoryTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_Loader_AutoloaderFactoryTest::main');
  23. }
  24. /*
  25. * Preload a number of classes to ensure they're available once we've disabled
  26. * other autoloaders.
  27. */
  28. require_once 'Zend/Loader/AutoloaderFactory.php';
  29. require_once 'Zend/Loader/ClassMapAutoloader.php';
  30. require_once 'Zend/Loader/StandardAutoloader.php';
  31. /**
  32. * @package Zend_Loader
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Loader
  37. */
  38. class Zend_Loader_AutoloaderFactoryTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  43. $result = PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function setUp()
  46. {
  47. // Store original autoloaders
  48. $this->loaders = spl_autoload_functions();
  49. if (!is_array($this->loaders)) {
  50. // spl_autoload_functions does not return empty array when no
  51. // autoloaders registered...
  52. $this->loaders = array();
  53. }
  54. // Clear out other autoloaders to ensure those being tested are at the
  55. // top of the stack
  56. foreach ($this->loaders as $loader) {
  57. spl_autoload_unregister($loader);
  58. }
  59. // Store original include_path
  60. $this->includePath = get_include_path();
  61. }
  62. public function tearDown()
  63. {
  64. Zend_Loader_AutoloaderFactory::unregisterAutoloaders();
  65. // Restore original autoloaders
  66. $loaders = spl_autoload_functions();
  67. if (is_array($loaders)) {
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. }
  72. foreach ($this->loaders as $loader) {
  73. spl_autoload_register($loader);
  74. }
  75. // Restore original include_path
  76. set_include_path($this->includePath);
  77. }
  78. public function testRegisteringValidMapFilePopulatesAutoloader()
  79. {
  80. Zend_Loader_AutoloaderFactory::factory(array(
  81. 'Zend_Loader_ClassMapAutoloader' => array(
  82. dirname(__FILE__) . '/_files/goodmap.php',
  83. ),
  84. ));
  85. $loader = Zend_Loader_AutoloaderFactory::getRegisteredAutoloader('Zend_Loader_ClassMapAutoloader');
  86. $map = $loader->getAutoloadMap();
  87. $this->assertTrue(is_array($map));
  88. $this->assertEquals(2, count($map));
  89. }
  90. /**
  91. * This tests checks if invalid autoloaders cause exceptions
  92. *
  93. * @expectedException Zend_Loader_Exception_InvalidArgumentException
  94. */
  95. public function testFactoryCatchesInvalidClasses()
  96. {
  97. if (!version_compare(PHP_VERSION, '5.3.7', '>=')) {
  98. $this->markTestSkipped('Cannot test invalid interface loader with versions less than 5.3.7');
  99. }
  100. include dirname(__FILE__) . '/_files/InvalidInterfaceAutoloader.php';
  101. Zend_Loader_AutoloaderFactory::factory(array(
  102. 'InvalidInterfaceAutoloader' => array()
  103. ));
  104. }
  105. public function testFactoryDoesNotRegisterDuplicateAutoloaders()
  106. {
  107. Zend_Loader_AutoloaderFactory::factory(array(
  108. 'Zend_Loader_StandardAutoloader' => array(
  109. 'prefixes' => array(
  110. 'TestPrefix' => dirname(__FILE__) . '/TestAsset/TestPrefix',
  111. ),
  112. ),
  113. ));
  114. $this->assertEquals(1, count(Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders()));
  115. Zend_Loader_AutoloaderFactory::factory(array(
  116. 'Zend_Loader_StandardAutoloader' => array(
  117. 'prefixes' => array(
  118. 'ZendTest_Loader_TestAsset_TestPlugins' => dirname(__FILE__) . '/TestAsset/TestPlugins',
  119. ),
  120. ),
  121. ));
  122. $this->assertEquals(1, count(Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders()));
  123. $this->assertTrue(class_exists('TestPrefix_NoDuplicateAutoloadersCase'));
  124. $this->assertTrue(class_exists('ZendTest_Loader_TestAsset_TestPlugins_Foo'));
  125. }
  126. public function testCanUnregisterAutoloaders()
  127. {
  128. Zend_Loader_AutoloaderFactory::factory(array(
  129. 'Zend_Loader_StandardAutoloader' => array(
  130. 'prefixes' => array(
  131. 'TestPrefix' => dirname(__FILE__) . '/TestAsset/TestPrefix',
  132. ),
  133. ),
  134. ));
  135. Zend_Loader_AutoloaderFactory::unregisterAutoloaders();
  136. $this->assertEquals(0, count(Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders()));
  137. }
  138. public function testCanUnregisterAutoloadersByClassName()
  139. {
  140. Zend_Loader_AutoloaderFactory::factory(array(
  141. 'Zend_Loader_StandardAutoloader' => array(
  142. 'namespaces' => array(
  143. 'TestPrefix' => dirname(__FILE__) . '/TestAsset/TestPrefix',
  144. ),
  145. ),
  146. ));
  147. Zend_Loader_AutoloaderFactory::unregisterAutoloader('Zend_Loader_StandardAutoloader');
  148. $this->assertEquals(0, count(Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders()));
  149. }
  150. public function testCanGetValidRegisteredAutoloader()
  151. {
  152. Zend_Loader_AutoloaderFactory::factory(array(
  153. 'Zend_Loader_StandardAutoloader' => array(
  154. 'namespaces' => array(
  155. 'TestPrefix' => dirname(__FILE__) . '/TestAsset/TestPrefix',
  156. ),
  157. ),
  158. ));
  159. $autoloader = Zend_Loader_AutoloaderFactory::getRegisteredAutoloader('Zend_Loader_StandardAutoloader');
  160. $this->assertTrue($autoloader instanceof Zend_Loader_StandardAutoloader);
  161. }
  162. public function testDefaultAutoloader()
  163. {
  164. Zend_Loader_AutoloaderFactory::factory();
  165. $autoloader = Zend_Loader_AutoloaderFactory::getRegisteredAutoloader('Zend_Loader_StandardAutoloader');
  166. $this->assertTrue($autoloader instanceof Zend_Loader_StandardAutoloader);
  167. $this->assertEquals(1, count(Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders()));
  168. }
  169. public function testGetInvalidAutoloaderThrowsException()
  170. {
  171. $this->setExpectedException('Zend_Loader_Exception_InvalidArgumentException');
  172. $loader = Zend_Loader_AutoloaderFactory::getRegisteredAutoloader('InvalidAutoloader');
  173. }
  174. public function testFactoryWithInvalidArgumentThrowsException()
  175. {
  176. $this->setExpectedException('Zend_Loader_Exception_InvalidArgumentException');
  177. Zend_Loader_AutoloaderFactory::factory('InvalidArgument');
  178. }
  179. public function testFactoryWithInvalidAutoloaderClassThrowsException()
  180. {
  181. $this->setExpectedException('Zend_Loader_Exception_InvalidArgumentException');
  182. Zend_Loader_AutoloaderFactory::factory(array('InvalidAutoloader' => array()));
  183. }
  184. public function testCannotBeInstantiatedViaConstructor()
  185. {
  186. $reflection = new ReflectionClass('Zend_Loader_AutoloaderFactory');
  187. $constructor = $reflection->getConstructor();
  188. $this->assertNull($constructor);
  189. }
  190. public function testPassingNoArgumentsToFactoryInstantiatesAndRegistersStandardAutoloader()
  191. {
  192. Zend_Loader_AutoloaderFactory::factory();
  193. $loaders = Zend_Loader_AutoloaderFactory::getRegisteredAutoloaders();
  194. $this->assertEquals(1, count($loaders));
  195. $loader = array_shift($loaders);
  196. $this->assertTrue($loader instanceof Zend_Loader_StandardAutoloader);
  197. $test = array($loader, 'autoload');
  198. $found = false;
  199. foreach (spl_autoload_functions() as $function) {
  200. if ($function === $test) {
  201. $found = true;
  202. break;
  203. }
  204. }
  205. $this->assertTrue($found, 'StandardAutoloader not registered with spl_autoload');
  206. }
  207. }
  208. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_AutoloaderFactoryTest::main') {
  209. Zend_Loader_AutoloaderFactoryTest::main();
  210. }