LegacyUniversalClassLoaderTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\UniversalClassLoader;
  13. /**
  14. * @group legacy
  15. */
  16. class LegacyUniversalClassLoaderTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider getLoadClassTests
  20. */
  21. public function testLoadClass($className, $testClassName, $message)
  22. {
  23. $loader = new UniversalClassLoader();
  24. $loader->registerNamespace('Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  25. $loader->registerPrefix('Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  26. $this->assertTrue($loader->loadClass($testClassName));
  27. $this->assertTrue(class_exists($className), $message);
  28. }
  29. public function getLoadClassTests()
  30. {
  31. return array(
  32. array('\\Namespaced\\Foo', 'Namespaced\\Foo', '->loadClass() loads Namespaced\Foo class'),
  33. array('\\Pearlike_Foo', 'Pearlike_Foo', '->loadClass() loads Pearlike_Foo class'),
  34. );
  35. }
  36. public function testUseIncludePath()
  37. {
  38. $loader = new UniversalClassLoader();
  39. $this->assertFalse($loader->getUseIncludePath());
  40. $this->assertNull($loader->findFile('Foo'));
  41. $includePath = get_include_path();
  42. $loader->useIncludePath(true);
  43. $this->assertTrue($loader->getUseIncludePath());
  44. set_include_path(__DIR__.'/Fixtures/includepath'.PATH_SEPARATOR.$includePath);
  45. $this->assertEquals(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'includepath'.\DIRECTORY_SEPARATOR.'Foo.php', $loader->findFile('Foo'));
  46. set_include_path($includePath);
  47. }
  48. public function testGetNamespaces()
  49. {
  50. $loader = new UniversalClassLoader();
  51. $loader->registerNamespace('Foo', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  52. $loader->registerNamespace('Bar', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  53. $loader->registerNamespace('Bas', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  54. $namespaces = $loader->getNamespaces();
  55. $this->assertArrayHasKey('Foo', $namespaces);
  56. $this->assertArrayNotHasKey('Foo1', $namespaces);
  57. $this->assertArrayHasKey('Bar', $namespaces);
  58. $this->assertArrayHasKey('Bas', $namespaces);
  59. }
  60. public function testGetPrefixes()
  61. {
  62. $loader = new UniversalClassLoader();
  63. $loader->registerPrefix('Foo', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  64. $loader->registerPrefix('Bar', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  65. $loader->registerPrefix('Bas', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  66. $prefixes = $loader->getPrefixes();
  67. $this->assertArrayHasKey('Foo', $prefixes);
  68. $this->assertArrayNotHasKey('Foo1', $prefixes);
  69. $this->assertArrayHasKey('Bar', $prefixes);
  70. $this->assertArrayHasKey('Bas', $prefixes);
  71. }
  72. /**
  73. * @dataProvider getLoadClassFromFallbackTests
  74. */
  75. public function testLoadClassFromFallback($className, $testClassName, $message)
  76. {
  77. $loader = new UniversalClassLoader();
  78. $loader->registerNamespace('Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  79. $loader->registerPrefix('Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  80. $loader->registerNamespaceFallbacks(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/fallback'));
  81. $loader->registerPrefixFallbacks(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/fallback'));
  82. $this->assertTrue($loader->loadClass($testClassName));
  83. $this->assertTrue(class_exists($className), $message);
  84. }
  85. public function getLoadClassFromFallbackTests()
  86. {
  87. return array(
  88. array('\\Namespaced\\Baz', 'Namespaced\\Baz', '->loadClass() loads Namespaced\Baz class'),
  89. array('\\Pearlike_Baz', 'Pearlike_Baz', '->loadClass() loads Pearlike_Baz class'),
  90. array('\\Namespaced\\FooBar', 'Namespaced\\FooBar', '->loadClass() loads Namespaced\Baz class from fallback dir'),
  91. array('\\Pearlike_FooBar', 'Pearlike_FooBar', '->loadClass() loads Pearlike_Baz class from fallback dir'),
  92. );
  93. }
  94. public function testRegisterPrefixFallback()
  95. {
  96. $loader = new UniversalClassLoader();
  97. $loader->registerPrefixFallback(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/fallback');
  98. $this->assertEquals(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/fallback'), $loader->getPrefixFallbacks());
  99. }
  100. public function testRegisterNamespaceFallback()
  101. {
  102. $loader = new UniversalClassLoader();
  103. $loader->registerNamespaceFallback(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Namespaced/fallback');
  104. $this->assertEquals(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Namespaced/fallback'), $loader->getNamespaceFallbacks());
  105. }
  106. /**
  107. * @dataProvider getLoadClassNamespaceCollisionTests
  108. */
  109. public function testLoadClassNamespaceCollision($namespaces, $className, $message)
  110. {
  111. $loader = new UniversalClassLoader();
  112. $loader->registerNamespaces($namespaces);
  113. $this->assertTrue($loader->loadClass($className));
  114. $this->assertTrue(class_exists($className), $message);
  115. }
  116. public function getLoadClassNamespaceCollisionTests()
  117. {
  118. return array(
  119. array(
  120. array(
  121. 'NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  122. 'NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  123. ),
  124. 'NamespaceCollision\A\Foo',
  125. '->loadClass() loads NamespaceCollision\A\Foo from alpha.',
  126. ),
  127. array(
  128. array(
  129. 'NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  130. 'NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  131. ),
  132. 'NamespaceCollision\A\Bar',
  133. '->loadClass() loads NamespaceCollision\A\Bar from alpha.',
  134. ),
  135. array(
  136. array(
  137. 'NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  138. 'NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  139. ),
  140. 'NamespaceCollision\A\B\Foo',
  141. '->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
  142. ),
  143. array(
  144. array(
  145. 'NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  146. 'NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  147. ),
  148. 'NamespaceCollision\A\B\Bar',
  149. '->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
  150. ),
  151. );
  152. }
  153. /**
  154. * @dataProvider getLoadClassPrefixCollisionTests
  155. */
  156. public function testLoadClassPrefixCollision($prefixes, $className, $message)
  157. {
  158. $loader = new UniversalClassLoader();
  159. $loader->registerPrefixes($prefixes);
  160. $this->assertTrue($loader->loadClass($className));
  161. $this->assertTrue(class_exists($className), $message);
  162. }
  163. public function getLoadClassPrefixCollisionTests()
  164. {
  165. return array(
  166. array(
  167. array(
  168. 'PrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  169. 'PrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  170. ),
  171. 'PrefixCollision_A_Foo',
  172. '->loadClass() loads PrefixCollision_A_Foo from alpha.',
  173. ),
  174. array(
  175. array(
  176. 'PrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  177. 'PrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  178. ),
  179. 'PrefixCollision_A_Bar',
  180. '->loadClass() loads PrefixCollision_A_Bar from alpha.',
  181. ),
  182. array(
  183. array(
  184. 'PrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  185. 'PrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  186. ),
  187. 'PrefixCollision_A_B_Foo',
  188. '->loadClass() loads PrefixCollision_A_B_Foo from beta.',
  189. ),
  190. array(
  191. array(
  192. 'PrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/beta',
  193. 'PrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/alpha',
  194. ),
  195. 'PrefixCollision_A_B_Bar',
  196. '->loadClass() loads PrefixCollision_A_B_Bar from beta.',
  197. ),
  198. );
  199. }
  200. }