AutoloaderFactoryTest.php 8.4 KB

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