More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Annotation / AnnotatedClassDiscoveryTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Annotation;
4
5 use Drupal\Component\Annotation\Plugin;
6 use Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery;
7 use Drupal\Component\FileCache\FileCacheFactory;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11  * @coversDefaultClass \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
12  * @group Annotation
13  */
14 class AnnotatedClassDiscoveryTest extends TestCase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected function setUp() {
20     parent::setUp();
21     // Ensure the file cache is disabled.
22     FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
23     // Ensure that FileCacheFactory has a prefix.
24     FileCacheFactory::setPrefix('prefix');
25   }
26
27   /**
28    * @covers ::__construct
29    * @covers ::getPluginNamespaces
30    */
31   public function testGetPluginNamespaces() {
32     $discovery = new AnnotatedClassDiscovery(['com/example' => [__DIR__]]);
33
34     $reflection = new \ReflectionMethod($discovery, 'getPluginNamespaces');
35     $reflection->setAccessible(TRUE);
36
37     $result = $reflection->invoke($discovery);
38     $this->assertEquals(['com/example' => [__DIR__]], $result);
39   }
40
41   /**
42    * @covers ::getDefinitions
43    * @covers ::prepareAnnotationDefinition
44    * @covers ::getAnnotationReader
45    */
46   public function testGetDefinitions() {
47     $discovery = new AnnotatedClassDiscovery(['com\example' => [__DIR__ . '/Fixtures']]);
48     $this->assertEquals([
49       'discovery_test_1' => [
50         'id' => 'discovery_test_1',
51         'class' => 'com\example\PluginNamespace\DiscoveryTest1',
52       ],
53     ], $discovery->getDefinitions());
54
55     $custom_annotation_discovery = new AnnotatedClassDiscovery(['com\example' => [__DIR__ . '/Fixtures']], CustomPlugin::class, ['Drupal\Tests\Component\Annotation']);
56     $this->assertEquals([
57       'discovery_test_1' => [
58         'id' => 'discovery_test_1',
59         'class' => 'com\example\PluginNamespace\DiscoveryTest1',
60         'title' => 'Discovery test plugin',
61       ],
62     ], $custom_annotation_discovery->getDefinitions());
63
64     $empty_discovery = new AnnotatedClassDiscovery(['com\example' => [__DIR__ . '/Fixtures']], CustomPlugin2::class, ['Drupal\Tests\Component\Annotation']);
65     $this->assertEquals([], $empty_discovery->getDefinitions());
66   }
67
68 }
69
70 /**
71  * Custom plugin annotation.
72  *
73  * @Annotation
74  */
75 class CustomPlugin extends Plugin {
76
77   /**
78    * The plugin ID.
79    *
80    * @var string
81    */
82   public $id;
83
84   /**
85    * The plugin title.
86    *
87    * @var string
88    *
89    * @ingroup plugin_translatable
90    */
91   public $title = '';
92
93 }
94
95 /**
96  * Custom plugin annotation.
97  *
98  * @Annotation
99  */
100 class CustomPlugin2 extends Plugin {}