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