class AnnotatedClassDiscoveryTest

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

@coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery

@group Annotation @group Plugin

Hierarchy

Expanded class hierarchy of AnnotatedClassDiscoveryTest

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php, line 20

Namespace

Drupal\Tests\Component\Plugin\Discovery
View source
class AnnotatedClassDiscoveryTest extends TestCase {
    
    /**
     * All the Drupal documentation standards tags.
     *
     * @var string[]
     */
    public static function provideBadAnnotations() {
        return [
            [
                'addtogroup',
            ],
            [
                'code',
            ],
            [
                'defgroup',
            ],
            [
                'deprecated',
            ],
            [
                'endcode',
            ],
            [
                'endlink',
            ],
            [
                'file',
            ],
            [
                'ingroup',
            ],
            [
                'group',
            ],
            [
                'link',
            ],
            [
                'mainpage',
            ],
            [
                'param',
            ],
            [
                'ref',
            ],
            [
                'return',
            ],
            [
                'section',
            ],
            [
                'see',
            ],
            [
                'subsection',
            ],
            [
                'throws',
            ],
            [
                'todo',
            ],
            [
                'var',
            ],
            [
                '{',
            ],
            [
                '}',
            ],
        ];
    }
    
    /**
     * Make sure AnnotatedClassDiscovery never tries to autoload bad annotations.
     *
     * @dataProvider provideBadAnnotations
     *
     * @coversNothing
     */
    public function testAutoloadBadAnnotations($annotation) : void {
        // Set up a class file in vfsStream.
        vfsStreamWrapper::register();
        $root = new vfsStreamDirectory('root');
        vfsStreamWrapper::setRoot($root);
        FileCacheFactory::setPrefix(__CLASS__);
        // Make a directory for discovery.
        $url = vfsStream::url('root');
        mkdir($url . '/DrupalTest');
        // Create a class docblock with our annotation.
        $php_file = "<?php\nnamespace DrupalTest;\n/**\n";
        $php_file .= " * @{$annotation}\n";
        $php_file .= " */\nclass TestClass {}";
        file_put_contents($url . '/DrupalTest/TestClass.php', $php_file);
        // Create an AnnotatedClassDiscovery object referencing the virtual file.
        $discovery = new AnnotatedClassDiscovery([
            '\\DrupalTest\\TestClass' => [
                vfsStream::url('root/DrupalTest'),
            ],
        ], '\\DrupalTest\\Component\\Annotation\\');
        // Register our class loader which will fail if the annotation reader tries
        // to autoload disallowed annotations.
        $class_loader = function ($class_name) use ($annotation) {
            $name_array = explode('\\', $class_name);
            $name = array_pop($name_array);
            if ($name == $annotation) {
                $this->fail('Attempted to autoload a non-plugin annotation: ' . $name);
            }
        };
        spl_autoload_register($class_loader, TRUE, TRUE);
        // Now try to get plugin definitions.
        $definitions = $discovery->getDefinitions();
        // Unregister to clean up.
        spl_autoload_unregister($class_loader);
        // Assert that no annotations were loaded.
        $this->assertEmpty($definitions);
    }

}

Members

Title Sort descending Modifiers Object type Summary
AnnotatedClassDiscoveryTest::provideBadAnnotations public static function All the Drupal documentation standards tags.
AnnotatedClassDiscoveryTest::testAutoloadBadAnnotations public function Make sure AnnotatedClassDiscovery never tries to autoload bad annotations.

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