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