UniversalClassLoaderTest.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Tests\Component\ClassLoader;
  11. use Symfony\Component\ClassLoader\UniversalClassLoader;
  12. class UniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getLoadClassTests
  16. */
  17. public function testLoadClass($className, $testClassName, $message)
  18. {
  19. $loader = new UniversalClassLoader();
  20. $loader->registerNamespace('Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  21. $loader->registerPrefix('Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  22. $loader->loadClass($testClassName);
  23. $this->assertTrue(class_exists($className), $message);
  24. }
  25. public function getLoadClassTests()
  26. {
  27. return array(
  28. array('\\Namespaced\\Foo', 'Namespaced\\Foo', '->loadClass() loads Namespaced\Foo class'),
  29. array('\\Pearlike_Foo', 'Pearlike_Foo', '->loadClass() loads Pearlike_Foo class'),
  30. array('\\Namespaced\\Bar', '\\Namespaced\\Bar', '->loadClass() loads Namespaced\Bar class with a leading slash'),
  31. array('\\Pearlike_Bar', '\\Pearlike_Bar', '->loadClass() loads Pearlike_Bar class with a leading slash'),
  32. );
  33. }
  34. /**
  35. * @dataProvider getLoadClassFromFallbackTests
  36. */
  37. public function testLoadClassFromFallback($className, $testClassName, $message)
  38. {
  39. $loader = new UniversalClassLoader();
  40. $loader->registerNamespace('Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  41. $loader->registerPrefix('Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  42. $loader->registerNamespaceFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'));
  43. $loader->registerPrefixFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/fallback'));
  44. $loader->loadClass($testClassName);
  45. $this->assertTrue(class_exists($className), $message);
  46. }
  47. public function getLoadClassFromFallbackTests()
  48. {
  49. return array(
  50. array('\\Namespaced\\Baz', 'Namespaced\\Baz', '->loadClass() loads Namespaced\Baz class'),
  51. array('\\Pearlike_Baz', 'Pearlike_Baz', '->loadClass() loads Pearlike_Baz class'),
  52. array('\\Namespaced\\FooBar', 'Namespaced\\FooBar', '->loadClass() loads Namespaced\Baz class from fallback dir'),
  53. array('\\Pearlike_FooBar', 'Pearlike_FooBar', '->loadClass() loads Pearlike_Baz class from fallback dir'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getLoadClassNamespaceCollisionTests
  58. */
  59. public function testLoadClassNamespaceCollision($namespaces, $className, $message)
  60. {
  61. $loader = new UniversalClassLoader();
  62. $loader->registerNamespaces($namespaces);
  63. $loader->loadClass($className);
  64. $this->assertTrue(class_exists($className), $message);
  65. }
  66. public function getLoadClassNamespaceCollisionTests()
  67. {
  68. return array(
  69. array(
  70. array(
  71. 'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  72. 'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  73. ),
  74. 'NamespaceCollision\A\Foo',
  75. '->loadClass() loads NamespaceCollision\A\Foo from alpha.',
  76. ),
  77. array(
  78. array(
  79. 'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  80. 'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  81. ),
  82. 'NamespaceCollision\A\Bar',
  83. '->loadClass() loads NamespaceCollision\A\Bar from alpha.',
  84. ),
  85. array(
  86. array(
  87. 'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  88. 'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  89. ),
  90. 'NamespaceCollision\A\B\Foo',
  91. '->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
  92. ),
  93. array(
  94. array(
  95. 'NamespaceCollision\\A\\B' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  96. 'NamespaceCollision\\A' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  97. ),
  98. 'NamespaceCollision\A\B\Bar',
  99. '->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
  100. ),
  101. );
  102. }
  103. /**
  104. * @dataProvider getLoadClassPrefixCollisionTests
  105. */
  106. public function testLoadClassPrefixCollision($prefixes, $className, $message)
  107. {
  108. $loader = new UniversalClassLoader();
  109. $loader->registerPrefixes($prefixes);
  110. $loader->loadClass($className);
  111. $this->assertTrue(class_exists($className), $message);
  112. }
  113. public function getLoadClassPrefixCollisionTests()
  114. {
  115. return array(
  116. array(
  117. array(
  118. 'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  119. 'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  120. ),
  121. 'PrefixCollision_A_Foo',
  122. '->loadClass() loads PrefixCollision_A_Foo from alpha.',
  123. ),
  124. array(
  125. array(
  126. 'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  127. 'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  128. ),
  129. 'PrefixCollision_A_Bar',
  130. '->loadClass() loads PrefixCollision_A_Bar from alpha.',
  131. ),
  132. array(
  133. array(
  134. 'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  135. 'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  136. ),
  137. 'PrefixCollision_A_B_Foo',
  138. '->loadClass() loads PrefixCollision_A_B_Foo from beta.',
  139. ),
  140. array(
  141. array(
  142. 'PrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/beta',
  143. 'PrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/alpha',
  144. ),
  145. 'PrefixCollision_A_B_Bar',
  146. '->loadClass() loads PrefixCollision_A_B_Bar from beta.',
  147. ),
  148. );
  149. }
  150. }