function ClassFinderTest::testFindFile

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php \Drupal\Tests\Component\ClassFinder\ClassFinderTest::testFindFile()
  2. 8.9.x core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php \Drupal\Tests\Component\ClassFinder\ClassFinderTest::testFindFile()
  3. 10 core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php \Drupal\Tests\Component\ClassFinder\ClassFinderTest::testFindFile()

@covers ::findFile

File

core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php, line 20

Class

ClassFinderTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Component%21ClassFinder%21ClassFinder.php/class/ClassFinder/11.x" title="A Utility class that uses active autoloaders to find a file for a class." class="local">\Drupal\Component\ClassFinder\ClassFinder</a> @group ClassFinder

Namespace

Drupal\Tests\Component\ClassFinder

Code

public function testFindFile() : void {
    $finder = new ClassFinder();
    // The full path is returned therefore only tests with
    // assertStringEndsWith() so the test is portable.
    $expected_path = str_replace('/', DIRECTORY_SEPARATOR, 'core/tests/Drupal/Tests/Component/ClassFinder/ClassFinderTest.php');
    $this->assertStringEndsWith($expected_path, $finder->findFile(ClassFinderTest::class));
    $class = 'Not\\A\\Class';
    $this->assertNull($finder->findFile($class));
    // Register an autoloader that can find this class.
    $loader = new ClassLoader();
    $loader->addClassMap([
        $class => __FILE__,
    ]);
    $loader->register();
    $this->assertEquals(__FILE__, $finder->findFile($class));
    // This shouldn't prevent us from finding the original file.
    $this->assertStringEndsWith($expected_path, $finder->findFile(ClassFinderTest::class));
    // Clean up the additional autoloader after the test.
    $loader->unregister();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.