ClassLoaderTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ClassLoader\ClassLoader;
  13. class ClassLoaderTest extends TestCase
  14. {
  15. public function testGetPrefixes()
  16. {
  17. $loader = new ClassLoader();
  18. $loader->addPrefix('Foo', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  19. $loader->addPrefix('Bar', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  20. $loader->addPrefix('Bas', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  21. $prefixes = $loader->getPrefixes();
  22. $this->assertArrayHasKey('Foo', $prefixes);
  23. $this->assertArrayNotHasKey('Foo1', $prefixes);
  24. $this->assertArrayHasKey('Bar', $prefixes);
  25. $this->assertArrayHasKey('Bas', $prefixes);
  26. }
  27. public function testGetFallbackDirs()
  28. {
  29. $loader = new ClassLoader();
  30. $loader->addPrefix(null, __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  31. $loader->addPrefix(null, __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  32. $fallback_dirs = $loader->getFallbackDirs();
  33. $this->assertCount(2, $fallback_dirs);
  34. }
  35. /**
  36. * @dataProvider getLoadClassTests
  37. */
  38. public function testLoadClass($className, $testClassName, $message)
  39. {
  40. $loader = new ClassLoader();
  41. $loader->addPrefix('Namespaced2\\', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  42. $loader->addPrefix('Pearlike2_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  43. $loader->loadClass($testClassName);
  44. $this->assertTrue(class_exists($className), $message);
  45. }
  46. public function getLoadClassTests()
  47. {
  48. return array(
  49. array('\\Namespaced2\\Foo', 'Namespaced2\\Foo', '->loadClass() loads Namespaced2\Foo class'),
  50. array('\\Pearlike2_Foo', 'Pearlike2_Foo', '->loadClass() loads Pearlike2_Foo class'),
  51. );
  52. }
  53. /**
  54. * @dataProvider getLoadNonexistentClassTests
  55. */
  56. public function testLoadNonexistentClass($className, $testClassName, $message)
  57. {
  58. $loader = new ClassLoader();
  59. $loader->addPrefix('Namespaced2\\', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  60. $loader->addPrefix('Pearlike2_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  61. $loader->loadClass($testClassName);
  62. $this->assertFalse(class_exists($className), $message);
  63. }
  64. public function getLoadNonexistentClassTests()
  65. {
  66. return array(
  67. array('\\Pearlike3_Bar', '\\Pearlike3_Bar', '->loadClass() loads non existing Pearlike3_Bar class with a leading slash'),
  68. );
  69. }
  70. public function testAddPrefixSingle()
  71. {
  72. $loader = new ClassLoader();
  73. $loader->addPrefix('Foo', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  74. $loader->addPrefix('Foo', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  75. $prefixes = $loader->getPrefixes();
  76. $this->assertArrayHasKey('Foo', $prefixes);
  77. $this->assertCount(1, $prefixes['Foo']);
  78. }
  79. public function testAddPrefixesSingle()
  80. {
  81. $loader = new ClassLoader();
  82. $loader->addPrefixes(array('Foo' => array('foo', 'foo')));
  83. $loader->addPrefixes(array('Foo' => array('foo')));
  84. $prefixes = $loader->getPrefixes();
  85. $this->assertArrayHasKey('Foo', $prefixes);
  86. $this->assertCount(1, $prefixes['Foo'], print_r($prefixes, true));
  87. }
  88. public function testAddPrefixMulti()
  89. {
  90. $loader = new ClassLoader();
  91. $loader->addPrefix('Foo', 'foo');
  92. $loader->addPrefix('Foo', 'bar');
  93. $prefixes = $loader->getPrefixes();
  94. $this->assertArrayHasKey('Foo', $prefixes);
  95. $this->assertCount(2, $prefixes['Foo']);
  96. $this->assertContains('foo', $prefixes['Foo']);
  97. $this->assertContains('bar', $prefixes['Foo']);
  98. }
  99. public function testUseIncludePath()
  100. {
  101. $loader = new ClassLoader();
  102. $this->assertFalse($loader->getUseIncludePath());
  103. $this->assertNull($loader->findFile('Foo'));
  104. $includePath = get_include_path();
  105. $loader->setUseIncludePath(true);
  106. $this->assertTrue($loader->getUseIncludePath());
  107. set_include_path(__DIR__.'/Fixtures/includepath'.PATH_SEPARATOR.$includePath);
  108. $this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'includepath'.\DIRECTORY_SEPARATOR.'Foo.php', $loader->findFile('Foo'));
  109. set_include_path($includePath);
  110. }
  111. /**
  112. * @dataProvider getLoadClassFromFallbackTests
  113. */
  114. public function testLoadClassFromFallback($className, $testClassName, $message)
  115. {
  116. $loader = new ClassLoader();
  117. $loader->addPrefix('Namespaced2\\', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  118. $loader->addPrefix('Pearlike2_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  119. $loader->addPrefix('', array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/fallback'));
  120. $loader->loadClass($testClassName);
  121. $this->assertTrue(class_exists($className), $message);
  122. }
  123. public function getLoadClassFromFallbackTests()
  124. {
  125. return array(
  126. array('\\Namespaced2\\Baz', 'Namespaced2\\Baz', '->loadClass() loads Namespaced2\Baz class'),
  127. array('\\Pearlike2_Baz', 'Pearlike2_Baz', '->loadClass() loads Pearlike2_Baz class'),
  128. array('\\Namespaced2\\FooBar', 'Namespaced2\\FooBar', '->loadClass() loads Namespaced2\Baz class from fallback dir'),
  129. array('\\Pearlike2_FooBar', 'Pearlike2_FooBar', '->loadClass() loads Pearlike2_Baz class from fallback dir'),
  130. );
  131. }
  132. /**
  133. * @dataProvider getLoadClassNamespaceCollisionTests
  134. */
  135. public function testLoadClassNamespaceCollision($namespaces, $className, $message)
  136. {
  137. $loader = new ClassLoader();
  138. $loader->addPrefixes($namespaces);
  139. $loader->loadClass($className);
  140. $this->assertTrue(class_exists($className), $message);
  141. }
  142. public function getLoadClassNamespaceCollisionTests()
  143. {
  144. return array(
  145. array(
  146. array(
  147. 'NamespaceCollision\\C' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  148. 'NamespaceCollision\\C\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  149. ),
  150. 'NamespaceCollision\C\Foo',
  151. '->loadClass() loads NamespaceCollision\C\Foo from alpha.',
  152. ),
  153. array(
  154. array(
  155. 'NamespaceCollision\\C\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  156. 'NamespaceCollision\\C' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  157. ),
  158. 'NamespaceCollision\C\Bar',
  159. '->loadClass() loads NamespaceCollision\C\Bar from alpha.',
  160. ),
  161. array(
  162. array(
  163. 'NamespaceCollision\\C' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  164. 'NamespaceCollision\\C\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  165. ),
  166. 'NamespaceCollision\C\B\Foo',
  167. '->loadClass() loads NamespaceCollision\C\B\Foo from beta.',
  168. ),
  169. array(
  170. array(
  171. 'NamespaceCollision\\C\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  172. 'NamespaceCollision\\C' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  173. ),
  174. 'NamespaceCollision\C\B\Bar',
  175. '->loadClass() loads NamespaceCollision\C\B\Bar from beta.',
  176. ),
  177. array(
  178. array(
  179. 'PrefixCollision_C_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  180. 'PrefixCollision_C_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  181. ),
  182. 'PrefixCollision_C_Foo',
  183. '->loadClass() loads PrefixCollision_C_Foo from alpha.',
  184. ),
  185. array(
  186. array(
  187. 'PrefixCollision_C_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  188. 'PrefixCollision_C_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  189. ),
  190. 'PrefixCollision_C_Bar',
  191. '->loadClass() loads PrefixCollision_C_Bar from alpha.',
  192. ),
  193. array(
  194. array(
  195. 'PrefixCollision_C_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  196. 'PrefixCollision_C_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  197. ),
  198. 'PrefixCollision_C_B_Foo',
  199. '->loadClass() loads PrefixCollision_C_B_Foo from beta.',
  200. ),
  201. array(
  202. array(
  203. 'PrefixCollision_C_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  204. 'PrefixCollision_C_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  205. ),
  206. 'PrefixCollision_C_B_Bar',
  207. '->loadClass() loads PrefixCollision_C_B_Bar from beta.',
  208. ),
  209. );
  210. }
  211. }