Psr4ClassLoaderTest.php 2.0 KB

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