Verified Commit 2d25dc4c authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2435607 by neclimdul, markdorison, Mile23, dawehner: Tests for Annotation Component

parent 18f322a2
Loading
Loading
Loading
Loading
+66 −0
Changes for core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryCachedTest.php: 66 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Component\Annotation;

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
 */
class AnnotatedClassDiscoveryCachedTest extends TestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    // Ensure FileCacheFactory::DISABLE_CACHE is *not* set, since we're testing
    // integration with the file cache.
    FileCacheFactory::setConfiguration([]);
    // Ensure that FileCacheFactory has a prefix.
    FileCacheFactory::setPrefix('prefix');
  }

  /**
   * Test that getDefinitions() retrieves the file cache correctly.
   *
   * @covers ::getDefinitions
   */
  public function testGetDefinitions() {
    // Path to the classes which we'll discover and parse annotation.
    $discovery_path  = __DIR__ . '/Fixtures';
    // File path that should be discovered within that directory.
    $file_path = $discovery_path . '/PluginNamespace/DiscoveryTest1.php';

    $discovery = new AnnotatedClassDiscovery(['com\example' => [$discovery_path]]);
    $this->assertEquals([
      'discovery_test_1' => [
        'id' => 'discovery_test_1',
        'class' => 'com\example\PluginNamespace\DiscoveryTest1',
      ],
    ], $discovery->getDefinitions());

    // Gain access to the file cache so we can change it.
    $ref_file_cache = new \ReflectionProperty($discovery, 'fileCache');
    $ref_file_cache->setAccessible(TRUE);
    /* @var $file_cache \Drupal\Component\FileCache\FileCacheInterface */
    $file_cache = $ref_file_cache->getValue($discovery);
    // The file cache is keyed by the file path, and we'll add some known
    // content to test against.
    $file_cache->set($file_path, [
      'id' => 'wrong_id',
      'content' => serialize(['an' => 'array']),
    ]);

    // Now perform the same query and check for the cached results.
    $this->assertEquals([
      'wrong_id' => [
        'an' => 'array',
      ],
    ], $discovery->getDefinitions());
  }

}
+100 −0
Changes for core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php: 100 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?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
 */
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 {}
+55 −0
Changes for core/tests/Drupal/Tests/Component/Annotation/AnnotationBaseTest.php: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Component\Annotation;

use Drupal\Component\Annotation\AnnotationBase;
use PHPUnit\Framework\TestCase;

/**
 * @coversDefaultClass \Drupal\Component\Annotation\AnnotationBase
 * @group Annotation
 */
class AnnotationBaseTest extends TestCase {

  /**
   * @covers ::getProvider
   * @covers ::setProvider
   */
  public function testSetProvider() {
    $plugin = new AnnotationBaseStub();
    $plugin->setProvider('example');
    $this->assertEquals('example', $plugin->getProvider());
  }

  /**
   * @covers ::getId
   */
  public function testGetId() {
    $plugin = new AnnotationBaseStub();
    // Doctrine sets the public prop directly.
    $plugin->id = 'example';
    $this->assertEquals('example', $plugin->getId());
  }

  /**
   * @covers ::getClass
   * @covers ::setClass
   */
  public function testSetClass() {
    $plugin = new AnnotationBaseStub();
    $plugin->setClass('example');
    $this->assertEquals('example', $plugin->getClass());
  }

}
/**
 * {@inheritdoc}
 */
class AnnotationBaseStub extends AnnotationBase {

  /**
   * {@inheritdoc}
   */
  public function get() {}

}
+16 −0
Changes for core/tests/Drupal/Tests/Component/Annotation/Fixtures/PluginNamespace/DiscoveryTest1.php: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace com\example\PluginNamespace;

/**
 * Provides a custom test plugin.
 *
 * @Plugin(
 *   id = "discovery_test_1"
 * )
 * @CustomPlugin(
 *   id = "discovery_test_1",
 *   title = "Discovery test plugin"
 * )
 */
class DiscoveryTest1 {}
+2 −0
Changes for core/tests/Drupal/Tests/Component/Annotation/Fixtures/PluginNamespace/discoverytest2.yml: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
# This should not be loaded by our annotated class discovery.
id:discovery_test_2
Loading