function AnnotatedClassDiscoveryCachedTest::testGetDefinitions

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php \Drupal\Tests\Component\Annotation\AnnotatedClassDiscoveryCachedTest::testGetDefinitions()
  2. 10 core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php \Drupal\Tests\Component\Annotation\AnnotatedClassDiscoveryCachedTest::testGetDefinitions()
  3. 11.x core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php \Drupal\Tests\Component\Annotation\AnnotatedClassDiscoveryCachedTest::testGetDefinitions()

Test that getDefinitions() retrieves the file cache correctly.

@covers ::getDefinitions

File

core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php, line 33

Class

AnnotatedClassDiscoveryCachedTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Component%21Annotation%21Plugin%21Discovery%21AnnotatedClassDiscovery.php/class/AnnotatedClassDiscovery/8.9.x" title="Defines a discovery mechanism to find annotated plugins in PSR-4 namespaces." class="local">\Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery</a> @group Annotation @runTestsInSeparateProcesses

Namespace

Drupal\Tests\Component\Annotation

Code

public function testGetDefinitions() {
    // Path to the classes which we'll discover and parse annotation.
    $discovery_path = __DIR__ . '/Fixtures';
    // File path that should be discovered within that directory.
    $file_path = $discovery_path . '/PluginNamespace/DiscoveryTest1.php';
    $discovery = new AnnotatedClassDiscovery([
        'com\\example' => [
            $discovery_path,
        ],
    ]);
    $this->assertEquals([
        'discovery_test_1' => [
            'id' => 'discovery_test_1',
            'class' => 'com\\example\\PluginNamespace\\DiscoveryTest1',
        ],
    ], $discovery->getDefinitions());
    // Gain access to the file cache so we can change it.
    $ref_file_cache = new \ReflectionProperty($discovery, 'fileCache');
    $ref_file_cache->setAccessible(TRUE);
    
    /* @var $file_cache \Drupal\Component\FileCache\FileCacheInterface */
    $file_cache = $ref_file_cache->getValue($discovery);
    // The file cache is keyed by the file path, and we'll add some known
    // content to test against.
    $file_cache->set($file_path, [
        'id' => 'wrong_id',
        'content' => serialize([
            'an' => 'array',
        ]),
    ]);
    // Now perform the same query and check for the cached results.
    $this->assertEquals([
        'wrong_id' => [
            'an' => 'array',
        ],
    ], $discovery->getDefinitions());
}

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