class AttributeClassDiscoveryTest

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php \Drupal\Tests\Component\Plugin\Attribute\AttributeClassDiscoveryTest

@coversDefaultClass \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery @group Attribute @runTestsInSeparateProcesses

Hierarchy

Expanded class hierarchy of AttributeClassDiscoveryTest

File

core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php, line 17

Namespace

Drupal\Tests\Component\Plugin\Attribute
View source
class AttributeClassDiscoveryTest extends TestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // Ensure the file cache is disabled.
        FileCacheFactory::setConfiguration([
            FileCacheFactory::DISABLE_CACHE => TRUE,
        ]);
        // Ensure that FileCacheFactory has a prefix.
        FileCacheFactory::setPrefix('prefix');
        // Normally the attribute classes would be autoloaded.
        include_once __DIR__ . '/../../../../../fixtures/plugins/CustomPlugin.php';
        $additionalClassLoader = new ClassLoader();
        $additionalClassLoader->addPsr4("com\\example\\PluginNamespace\\", __DIR__ . "/../../../../../fixtures/plugins/Plugin/PluginNamespace");
        $additionalClassLoader->register(TRUE);
    }
    
    /**
     * @covers ::__construct
     * @covers ::getPluginNamespaces
     */
    public function testGetPluginNamespaces() : void {
        // Path to the classes which we'll discover and parse annotation.
        $discovery = new AttributeClassDiscovery([
            'com/example' => [
                __DIR__,
            ],
        ]);
        $reflection = new \ReflectionMethod($discovery, 'getPluginNamespaces');
        $reflection->setAccessible(TRUE);
        $result = $reflection->invoke($discovery);
        $this->assertEquals([
            'com/example' => [
                __DIR__,
            ],
        ], $result);
    }
    
    /**
     * @covers ::getDefinitions
     * @covers ::prepareAttributeDefinition
     */
    public function testGetDefinitions() : void {
        $discovery = new AttributeClassDiscovery([
            'com\\example' => [
                __DIR__ . "/../../../../../fixtures/plugins/Plugin",
            ],
        ]);
        $this->assertEquals([
            'discovery_test_1' => [
                'id' => 'discovery_test_1',
                'class' => 'com\\example\\PluginNamespace\\AttributeDiscoveryTest1',
            ],
        ], $discovery->getDefinitions());
        $custom_annotation_discovery = new AttributeClassDiscovery([
            'com\\example' => [
                __DIR__ . "/../../../../../fixtures/plugins/Plugin",
            ],
        ], 'com\\example\\PluginNamespace\\CustomPlugin');
        $this->assertEquals([
            'discovery_test_1' => [
                'id' => 'discovery_test_1',
                'class' => 'com\\example\\PluginNamespace\\AttributeDiscoveryTest1',
                'title' => 'Discovery test plugin',
            ],
        ], $custom_annotation_discovery->getDefinitions());
        $empty_discovery = new AttributeClassDiscovery([
            'com\\example' => [
                __DIR__ . "/../../../../../fixtures/plugins/Plugin",
            ],
        ], 'com\\example\\PluginNamespace\\CustomPlugin2');
        $this->assertEquals([], $empty_discovery->getDefinitions());
    }

}

Members

Title Sort descending Modifiers Object type Summary
AttributeClassDiscoveryTest::setUp protected function
AttributeClassDiscoveryTest::testGetDefinitions public function @covers ::getDefinitions
@covers ::prepareAttributeDefinition
AttributeClassDiscoveryTest::testGetPluginNamespaces public function @covers ::__construct
@covers ::getPluginNamespaces

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