ClassFinderTest.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Drupal\Tests\Component\ClassFinder;
  3. use Composer\Autoload\ClassLoader;
  4. use Drupal\Component\ClassFinder\ClassFinder;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @coversDefaultClass \Drupal\Component\ClassFinder\ClassFinder
  8. * @group ClassFinder
  9. */
  10. class ClassFinderTest extends TestCase {
  11. /**
  12. * @covers ::findFile
  13. */
  14. public function testFindFile() {
  15. $finder = new ClassFinder();
  16. // The full path is returned therefore only tests with
  17. // assertStringEndsWith() so the test is portable.
  18. $this->assertStringEndsWith('core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php', $finder->findFile(ClassFinderTest::class));
  19. $class = 'Not\\A\\Class';
  20. $this->assertNull($finder->findFile($class));
  21. // Register an autoloader that can find this class.
  22. $loader = new ClassLoader();
  23. $loader->addClassMap([$class => __FILE__]);
  24. $loader->register();
  25. $this->assertEquals(__FILE__, $finder->findFile($class));
  26. // This shouldn't prevent us from finding the original file.
  27. $this->assertStringEndsWith('core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php', $finder->findFile(ClassFinderTest::class));
  28. // Clean up the additional autoloader after the test.
  29. $loader->unregister();
  30. }
  31. }