ClassMapAutoloaderTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Loader
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 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_ClassMapAutoloaderTest::main');
  24. }
  25. require_once 'Zend/Loader/ClassMapAutoloader.php';
  26. /**
  27. * @category Zend
  28. * @package Loader
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Loader
  33. */
  34. class Zend_Loader_ClassMapAutoloaderTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function setUp()
  37. {
  38. // Store original autoloaders
  39. $this->loaders = spl_autoload_functions();
  40. if (!is_array($this->loaders)) {
  41. // spl_autoload_functions does not return empty array when no
  42. // autoloaders registered...
  43. $this->loaders = array();
  44. }
  45. // Store original include_path
  46. $this->includePath = get_include_path();
  47. $this->loader = new Zend_Loader_ClassMapAutoloader();
  48. }
  49. public function tearDown()
  50. {
  51. // Restore original autoloaders
  52. $loaders = spl_autoload_functions();
  53. if (is_array($loaders)) {
  54. foreach ($loaders as $loader) {
  55. spl_autoload_unregister($loader);
  56. }
  57. }
  58. foreach ($this->loaders as $loader) {
  59. spl_autoload_register($loader);
  60. }
  61. // Restore original include_path
  62. set_include_path($this->includePath);
  63. }
  64. public function testRegisteringNonExistentAutoloadMapRaisesInvalidArgumentException()
  65. {
  66. $dir = dirname(__FILE__) . '__foobar__';
  67. $this->setExpectedException('Zend_Loader_Exception_InvalidArgumentException');
  68. $this->loader->registerAutoloadMap($dir);
  69. }
  70. public function testValidMapFileNotReturningMapRaisesInvalidArgumentException()
  71. {
  72. $this->setExpectedException('Zend_Loader_Exception_InvalidArgumentException');
  73. $this->loader->registerAutoloadMap(dirname(__FILE__) . '/_files/badmap.php');
  74. }
  75. public function testAllowsRegisteringArrayAutoloadMapDirectly()
  76. {
  77. $map = array(
  78. 'Zend_Loader_Exception' => dirname(__FILE__) . '/../../../library/Zend/Loader/Exception.php',
  79. );
  80. $this->loader->registerAutoloadMap($map);
  81. $test = $this->loader->getAutoloadMap();
  82. $this->assertSame($map, $test);
  83. }
  84. public function testAllowsRegisteringArrayAutoloadMapViaConstructor()
  85. {
  86. $map = array(
  87. 'Zend_Loader_Exception' => dirname(__FILE__) . '/../../../library/Zend/Loader/Exception.php',
  88. );
  89. $loader = new Zend_Loader_ClassMapAutoloader(array($map));
  90. $test = $loader->getAutoloadMap();
  91. $this->assertSame($map, $test);
  92. }
  93. public function testRegisteringValidMapFilePopulatesAutoloader()
  94. {
  95. $this->loader->registerAutoloadMap(dirname(__FILE__) . '/_files/goodmap.php');
  96. $map = $this->loader->getAutoloadMap();
  97. $this->assertTrue(is_array($map));
  98. $this->assertEquals(2, count($map));
  99. // Just to make sure nothing changes after loading the same map again
  100. // (loadMapFromFile should just return)
  101. $this->loader->registerAutoloadMap(dirname(__FILE__) . '/_files/goodmap.php');
  102. $map = $this->loader->getAutoloadMap();
  103. $this->assertTrue(is_array($map));
  104. $this->assertEquals(2, count($map));
  105. }
  106. public function testRegisteringMultipleMapsMergesThem()
  107. {
  108. $map = array(
  109. 'Zend_Loader_Exception' => dirname(__FILE__) . '/../../../library/Zend/Loader/Exception.php',
  110. 'Zend_Loader_StandardAutoloaderTest' => 'some/bogus/path.php',
  111. );
  112. $this->loader->registerAutoloadMap($map);
  113. $this->loader->registerAutoloadMap(dirname(__FILE__) . '/_files/goodmap.php');
  114. $test = $this->loader->getAutoloadMap();
  115. $this->assertTrue(is_array($test));
  116. $this->assertEquals(3, count($test));
  117. $this->assertNotEquals($map['Zend_Loader_StandardAutoloaderTest'], $test['Zend_Loader_StandardAutoloaderTest']);
  118. }
  119. public function testCanRegisterMultipleMapsAtOnce()
  120. {
  121. $map = array(
  122. 'Zend_Loader_Exception' => dirname(__FILE__) . '/../../../library/Zend/Loader/Exception.php',
  123. 'Zend_Loader_StandardAutoloaderTest' => 'some/bogus/path.php',
  124. );
  125. $maps = array($map, dirname(__FILE__) . '/_files/goodmap.php');
  126. $this->loader->registerAutoloadMaps($maps);
  127. $test = $this->loader->getAutoloadMap();
  128. $this->assertTrue(is_array($test));
  129. $this->assertEquals(3, count($test));
  130. }
  131. public function testRegisterMapsThrowsExceptionForNonTraversableArguments()
  132. {
  133. $tests = array(true, 'string', 1, 1.0, new stdClass);
  134. foreach ($tests as $test) {
  135. try {
  136. $this->loader->registerAutoloadMaps($test);
  137. $this->fail('Should not register non-traversable arguments');
  138. } catch (Zend_Loader_Exception_InvalidArgumentException $e) {
  139. $this->assertContains('array or implement Traversable', $e->getMessage());
  140. }
  141. }
  142. }
  143. public function testAutoloadLoadsClasses()
  144. {
  145. $map = array('Zend_UnusualNamespace_ClassMappedClass' => dirname(__FILE__) . '/TestAsset/ClassMappedClass.php');
  146. $this->loader->registerAutoloadMap($map);
  147. $this->loader->autoload('Zend_UnusualNamespace_ClassMappedClass');
  148. $this->assertTrue(class_exists('Zend_UnusualNamespace_ClassMappedClass', false));
  149. }
  150. public function testIgnoresClassesNotInItsMap()
  151. {
  152. $map = array('Zend_UnusualNamespace_ClassMappedClass' => dirname(__FILE__) . '/TestAsset/ClassMappedClass.php');
  153. $this->loader->registerAutoloadMap($map);
  154. $this->loader->autoload('Zend_UnusualNamespace_UnMappedClass');
  155. $this->assertFalse(class_exists('Zend_UnusualNamespace_UnMappedClass', false));
  156. }
  157. public function testRegisterRegistersCallbackWithSplAutoload()
  158. {
  159. $this->loader->register();
  160. $loaders = spl_autoload_functions();
  161. $this->assertTrue(count($this->loaders) < count($loaders));
  162. $found = false;
  163. foreach ($loaders as $loader) {
  164. if ($loader == array($this->loader, 'autoload')) {
  165. $found = true;
  166. break;
  167. }
  168. }
  169. $this->assertTrue($found, 'Autoloader not found in stack');
  170. }
  171. public function testCanLoadClassMapFromPhar()
  172. {
  173. if (!class_exists('Phar')) {
  174. $this->markTestSkipped('Test requires Phar extension');
  175. }
  176. $map = 'phar://' . dirname(__FILE__) . '/_files/classmap.phar/test/.//../autoload_classmap.php';
  177. $this->loader->registerAutoloadMap($map);
  178. $this->loader->autoload('some_loadedclass');
  179. $this->assertTrue(class_exists('some_loadedclass', false));
  180. $test = $this->loader->getAutoloadMap();
  181. $this->assertEquals(2, count($test));
  182. // will not register duplicate, even with a different relative path
  183. $map = 'phar://' . __DIR__ . '/_files/classmap.phar/test/./foo/../../autoload_classmap.php';
  184. $this->loader->registerAutoloadMap($map);
  185. $test = $this->loader->getAutoloadMap();
  186. $this->assertEquals(2, count($test));
  187. }
  188. public function testCanLoadNamespacedClassFromPhar()
  189. {
  190. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  191. $this->markTestSkipped('Namespace support is valid for PHP >= 5.3.0 only');
  192. }
  193. $map = 'phar://' . __DIR__ . '/_files/classmap.phar/test/.//../autoload_classmap.php';
  194. $this->loader->registerAutoloadMap($map);
  195. $this->loader->autoload('some\namespacedclass');
  196. $this->assertTrue(class_exists('some\namespacedclass', false));
  197. }
  198. }
  199. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_ClassMapAutoloaderTest::main') {
  200. Zend_Loader_ClassMapAutoloaderTest::main();
  201. }