ClassMapAutoloaderTest.php 8.7 KB

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