Psr4ClassLoaderTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Component\ClassLoader\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ClassLoader\Psr4ClassLoader;
  13. /**
  14. * @group legacy
  15. */
  16. class Psr4ClassLoaderTest extends TestCase
  17. {
  18. /**
  19. * @param string $className
  20. * @dataProvider getLoadClassTests
  21. */
  22. public function testLoadClass($className)
  23. {
  24. $loader = new Psr4ClassLoader();
  25. $loader->addPrefix(
  26. 'Acme\\DemoLib',
  27. __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'psr-4'
  28. );
  29. $loader->loadClass($className);
  30. $this->assertTrue(class_exists($className), sprintf('loadClass() should load %s', $className));
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function getLoadClassTests()
  36. {
  37. return [
  38. ['Acme\\DemoLib\\Foo'],
  39. ['Acme\\DemoLib\\Class_With_Underscores'],
  40. ['Acme\\DemoLib\\Lets\\Go\\Deeper\\Foo'],
  41. ['Acme\\DemoLib\\Lets\\Go\\Deeper\\Class_With_Underscores'],
  42. ];
  43. }
  44. /**
  45. * @param string $className
  46. * @dataProvider getLoadNonexistentClassTests
  47. */
  48. public function testLoadNonexistentClass($className)
  49. {
  50. $loader = new Psr4ClassLoader();
  51. $loader->addPrefix(
  52. 'Acme\\DemoLib',
  53. __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'psr-4'
  54. );
  55. $loader->loadClass($className);
  56. $this->assertFalse(class_exists($className), sprintf('loadClass() should not load %s', $className));
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getLoadNonexistentClassTests()
  62. {
  63. return [
  64. ['Acme\\DemoLib\\I_Do_Not_Exist'],
  65. ['UnknownVendor\\SomeLib\\I_Do_Not_Exist'],
  66. ];
  67. }
  68. }