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;
class AnnotatedClassDiscoveryTest extends TestCase {
protected function setUp() : void {
parent::setUp();
FileCacheFactory::setConfiguration([
FileCacheFactory::DISABLE_CACHE => TRUE,
]);
FileCacheFactory::setPrefix('prefix');
}
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);
}
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());
}
}
class CustomPlugin extends Plugin {
public $id;
public $title = '';
}
class CustomPlugin2 extends Plugin {
}