function AttributeClassDiscoveryCachedTest::testGetDefinitionsMissingTrait

Tests discovery with missing traits.

@covers ::getDefinitions

File

core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryCachedTest.php, line 105

Class

AttributeClassDiscoveryCachedTest
@coversDefaultClass \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery @covers \Drupal\Component\Discovery\MissingClassDetectionClassLoader @group Attribute @runTestsInSeparateProcesses

Namespace

Drupal\Tests\Component\Plugin\Attribute

Code

public function testGetDefinitionsMissingTrait() : void {
    // Path to the classes which we'll discover and parse annotation.
    $discovery_path = __DIR__ . "/../../../../../fixtures/plugins/Plugin";
    // Define file paths within the directory that should not be discovered.
    $non_discoverable_file_paths = [
        $discovery_path . '/PluginNamespace/AttributeDiscoveryTest2.php',
        $discovery_path . '/PluginNamespace/AttributeDiscoveryTestMissingInterface.php',
        $discovery_path . '/PluginNamespace/AttributeDiscoveryTestMissingTrait.php',
    ];
    $discovery = new AttributeClassDiscovery([
        'com\\example' => [
            $discovery_path,
        ],
    ]);
    $this->assertEquals([
        'discovery_test_1' => [
            'id' => 'discovery_test_1',
            'class' => 'com\\example\\PluginNamespace\\AttributeDiscoveryTest1',
        ],
    ], $discovery->getDefinitions());
    // Gain access to the file cache.
    $ref_file_cache = new \ReflectionProperty($discovery, 'fileCache');
    $ref_file_cache->setAccessible(TRUE);
    
    /** @var \Drupal\Component\FileCache\FileCacheInterface $file_cache */
    $file_cache = $ref_file_cache->getValue($discovery);
    // The plugins that extend a missing class, implement a missing interface,
    // and use a missing trait should not be cached.
    foreach ($non_discoverable_file_paths as $non_discoverable_file_path) {
        $this->assertTrue(file_exists($non_discoverable_file_path));
        $this->assertNull($file_cache->get($non_discoverable_file_path));
    }
    $discovery = new AttributeClassDiscovery([
        'com\\example' => [
            $discovery_path,
        ],
        'Drupal\\a_module_that_does_not_exist' => [
            $discovery_path,
        ],
    ]);
    $this->assertEquals([
        'discovery_test_1' => [
            'id' => 'discovery_test_1',
            'class' => 'com\\example\\PluginNamespace\\AttributeDiscoveryTest1',
        ],
        'discovery_test_missing_trait' => [
            'id' => 'discovery_test_missing_trait',
            'class' => 'com\\example\\PluginNamespace\\AttributeDiscoveryTestMissingTrait',
            'title' => 'Discovery test plugin missing trait',
        ],
    ], $discovery->getDefinitions());
    // The plugins that extend a missing class, implement a missing interface,
    // and use a missing trait should not be cached. This is the case even for
    // the plugin that was just discovered.
    foreach ($non_discoverable_file_paths as $non_discoverable_file_path) {
        $this->assertTrue(file_exists($non_discoverable_file_path));
        $this->assertNull($file_cache->get($non_discoverable_file_path));
    }
}

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