AutoloaderFactoryTest.php 8.6 KB

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