ClassLoaderTest.php 2.0 KB

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