ClassLoaderTest.php 2.3 KB

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