AnnotatedClassDiscoveryTest.php

Same filename in this branch
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  2. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
Same filename in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  2. 9 core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php
  3. 9 core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  4. 10 core/tests/Drupal/KernelTests/Core/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  5. 10 core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php
  6. 10 core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  7. 11.x core/tests/Drupal/KernelTests/Core/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
  8. 11.x core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php
  9. 11.x core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php

Namespace

Drupal\Tests\Component\Annotation

File

core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php

View source
<?php

namespace Drupal\Tests\Component\Annotation;

use Drupal\Component\Annotation\Plugin;
use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Component\FileCache\FileCacheFactory;
use PHPUnit\Framework\TestCase;

/**
 * @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
 * @group Annotation
 * @runTestsInSeparateProcesses
 */
class AnnotatedClassDiscoveryTest extends TestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        parent::setUp();
        // Ensure the file cache is disabled.
        FileCacheFactory::setConfiguration([
            FileCacheFactory::DISABLE_CACHE => TRUE,
        ]);
        // Ensure that FileCacheFactory has a prefix.
        FileCacheFactory::setPrefix('prefix');
    }
    
    /**
     * @covers ::__construct
     * @covers ::getPluginNamespaces
     */
    public function testGetPluginNamespaces() {
        $discovery = new AnnotatedClassDiscovery([
            'com/example' => [
                __DIR__,
            ],
        ]);
        $reflection = new \ReflectionMethod($discovery, 'getPluginNamespaces');
        $reflection->setAccessible(TRUE);
        $result = $reflection->invoke($discovery);
        $this->assertEquals([
            'com/example' => [
                __DIR__,
            ],
        ], $result);
    }
    
    /**
     * @covers ::getDefinitions
     * @covers ::prepareAnnotationDefinition
     * @covers ::getAnnotationReader
     */
    public function testGetDefinitions() {
        $discovery = new AnnotatedClassDiscovery([
            'com\\example' => [
                __DIR__ . '/Fixtures',
            ],
        ]);
        $this->assertEquals([
            'discovery_test_1' => [
                'id' => 'discovery_test_1',
                'class' => 'com\\example\\PluginNamespace\\DiscoveryTest1',
            ],
        ], $discovery->getDefinitions());
        $custom_annotation_discovery = new AnnotatedClassDiscovery([
            'com\\example' => [
                __DIR__ . '/Fixtures',
            ],
        ], CustomPlugin::class, [
            'Drupal\\Tests\\Component\\Annotation',
        ]);
        $this->assertEquals([
            'discovery_test_1' => [
                'id' => 'discovery_test_1',
                'class' => 'com\\example\\PluginNamespace\\DiscoveryTest1',
                'title' => 'Discovery test plugin',
            ],
        ], $custom_annotation_discovery->getDefinitions());
        $empty_discovery = new AnnotatedClassDiscovery([
            'com\\example' => [
                __DIR__ . '/Fixtures',
            ],
        ], CustomPlugin2::class, [
            'Drupal\\Tests\\Component\\Annotation',
        ]);
        $this->assertEquals([], $empty_discovery->getDefinitions());
    }

}

/**
 * Custom plugin annotation.
 *
 * @Annotation
 */
class CustomPlugin extends Plugin {
    
    /**
     * The plugin ID.
     *
     * @var string
     */
    public $id;
    
    /**
     * The plugin title.
     *
     * @var string
     *
     * @ingroup plugin_translatable
     */
    public $title = '';

}

/**
 * Custom plugin annotation.
 *
 * @Annotation
 */
class CustomPlugin2 extends Plugin {

}

Classes

Title Deprecated Summary
AnnotatedClassDiscoveryTest @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery @group Annotation @runTestsInSeparateProcesses
CustomPlugin Custom plugin annotation.
CustomPlugin2 Custom plugin annotation.

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