ClassLoaderTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Composer\Test\Autoload;
  3. use Composer\Autoload\ClassLoader;
  4. /**
  5. * Tests the Composer\Autoload\ClassLoader class.
  6. */
  7. class ClassLoaderTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testLoadClassDotPhp()
  10. {
  11. $loader = new ClassLoader();
  12. $loader->add('DirDotPhp\\', __DIR__ . '/Fixtures');
  13. $loader->addPsr4('DirDotPhp\\', __DIR__ . '/Fixtures/DirDotPhp/psr4');
  14. $class = 'DirDotPhp\\Dir';
  15. $loader->loadClass($class);
  16. $this->assertTrue(class_exists($class, false), "->loadClass() loads '$class'.");
  17. }
  18. /**
  19. * Tests regular PSR-0 and PSR-4 class loading.
  20. *
  21. * @dataProvider getLoadClassTests
  22. *
  23. * @param string $class The fully-qualified class name to test, without preceding namespace separator.
  24. * @param bool $prependSeparator Whether to call ->loadClass() with a class name with preceding
  25. * namespace separator, as it happens in PHP 5.3.0 - 5.3.2. See https://bugs.php.net/50731
  26. */
  27. public function testLoadClass($class, $prependSeparator = false)
  28. {
  29. $loader = new ClassLoader();
  30. $loader->add('Namespaced\\', __DIR__ . '/Fixtures');
  31. $loader->add('Pearlike_', __DIR__ . '/Fixtures');
  32. $loader->addPsr4('ShinyVendor\\ShinyPackage\\', __DIR__ . '/Fixtures');
  33. if ($prependSeparator) {
  34. $prepend = '\\';
  35. $message = "->loadClass() loads '$class'.";
  36. } else {
  37. $prepend = '';
  38. $message = "->loadClass() loads '\\$class', as required in PHP 5.3.0 - 5.3.2.";
  39. }
  40. $loader->loadClass($prepend . $class);
  41. $this->assertTrue(class_exists($class, false), $message);
  42. }
  43. /**
  44. * Provides arguments for ->testLoadClass().
  45. *
  46. * @return array Array of parameter sets to test with.
  47. */
  48. public function getLoadClassTests()
  49. {
  50. return array(
  51. array('Namespaced\\Foo'),
  52. array('Pearlike_Foo'),
  53. array('ShinyVendor\\ShinyPackage\\SubNamespace\\Foo'),
  54. // "Bar" would not work here, since it is defined in a ".inc" file,
  55. // instead of a ".php" file. So, use "Baz" instead.
  56. array('Namespaced\\Baz', true),
  57. array('Pearlike_Bar', true),
  58. array('ShinyVendor\\ShinyPackage\\SubNamespace\\Bar', true),
  59. );
  60. }
  61. /**
  62. * getPrefixes method should return empty array if ClassLoader does not have any psr-0 configuration
  63. */
  64. public function testGetPrefixesWithNoPSR0Configuration()
  65. {
  66. $loader = new ClassLoader();
  67. $this->assertEmpty($loader->getPrefixes());
  68. }
  69. }