ClassLoaderTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Autoload;
  12. use Composer\Autoload\ClassLoader;
  13. /**
  14. * Tests the Composer\Autoload\ClassLoader class.
  15. */
  16. class ClassLoaderTest extends \PHPUnit_Framework_TestCase
  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. }