class PluginDiscoveryDecoratorTest in Plugin 8.2
@coversDefaultClass \Drupal\plugin\PluginDiscovery\PluginDiscoveryDecorator
@group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\plugin\Unit\PluginDiscovery\PluginDiscoveryDecoratorTest
 
Expanded class hierarchy of PluginDiscoveryDecoratorTest
File
- tests/src/ Unit/ PluginDiscovery/ PluginDiscoveryDecoratorTest.php, line 15 
Namespace
Drupal\Tests\plugin\Unit\PluginDiscoveryView source
class PluginDiscoveryDecoratorTest extends UnitTestCase {
  /**
   * The decorated discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $decoratedDiscovery;
  /**
   * The class under test.
   *
   * @var \Drupal\plugin\PluginDiscovery\PluginDiscoveryDecorator
   */
  protected $sut;
  protected function setUp() : void {
    $this->decoratedDiscovery = $this
      ->createMock(DiscoveryInterface::class);
    $this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
  }
  /**
   * @covers ::__construct
   */
  public function testConstruct() {
    $this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
    $this
      ->assertInstanceOf(PluginDiscoveryDecorator::class, $this->sut);
  }
  /**
   * @covers ::getDefinitions
   * @covers ::processDecoratedDefinitions
   */
  public function testGetDefinitions() {
    $plugin_id_a = $this
      ->randomMachineName();
    $plugin_definition_a = [
      'id' => $plugin_id_a,
    ];
    $plugin_id_b = $this
      ->randomMachineName();
    $plugin_definition_b = [
      'id' => $plugin_id_b,
    ];
    $plugin_id_c = $this
      ->randomMachineName();
    $plugin_definition_c = [
      'id' => $plugin_id_c,
    ];
    $plugin_definitions = [
      $plugin_id_a => $plugin_definition_a,
      $plugin_id_b => $plugin_definition_b,
      $plugin_id_c => $plugin_definition_c,
    ];
    $this->decoratedDiscovery
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $this
      ->assertEquals($plugin_definitions, $this->sut
      ->getDefinitions());
  }
  /**
   * @covers ::clearCachedDefinitions
   */
  public function testClearCachedDefinitionsWithUncachedDecoratedDiscovery() {
    $this->decoratedDiscovery
      ->expects($this
      ->exactly(2))
      ->method('getDefinitions')
      ->willReturn([]);
    // There are no cached definitions yet.
    $this->sut
      ->getDefinitions();
    // This should return the cached definitions.
    $this->sut
      ->getDefinitions();
    $this->sut
      ->clearCachedDefinitions();
    // This should return newly built definitions.
    $this->sut
      ->getDefinitions();
  }
  /**
   * @covers ::clearCachedDefinitions
   */
  public function testClearCachedDefinitionsWithCachedDecoratedDiscovery() {
    $this->decoratedDiscovery = $this
      ->getMockForAbstractClass(PluginDiscoveryDecoratorTestCachedDiscovery::class);
    $this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('clearCachedDefinitions');
    $this->decoratedDiscovery
      ->expects($this
      ->exactly(2))
      ->method('getDefinitions')
      ->willReturn([]);
    // There are no cached definitions yet.
    $this->sut
      ->getDefinitions();
    // This should return the cached definitions.
    $this->sut
      ->getDefinitions();
    $this->sut
      ->clearCachedDefinitions();
    // This should return newly built definitions.
    $this->sut
      ->getDefinitions();
  }
  /**
   * @covers ::useCaches
   */
  public function testUseCachesWithCachedDecoratedDiscovery() {
    $this->decoratedDiscovery = $this
      ->getMockForAbstractClass(PluginDiscoveryDecoratorTestCachedDiscovery::class);
    $this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('clearCachedDefinitions');
    $this->decoratedDiscovery
      ->expects($this
      ->exactly(3))
      ->method('getDefinitions')
      ->willReturn([]);
    // There are no cached definitions yet, so this should call the decorated
    // discovery.
    $this->sut
      ->getDefinitions();
    // This should return the cached definitions.
    $this->sut
      ->getDefinitions();
    $this->sut
      ->useCaches(FALSE);
    // This should return newly built definitions, so this should call the
    // decorated discovery.
    $this->sut
      ->getDefinitions();
    // This should return newly built definitions again, because we disabled
    // caching.
    $this->sut
      ->getDefinitions();
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| PhpunitCompatibilityTrait:: | public | function | Returns a mock object for the specified class using the available method. | |
| PhpunitCompatibilityTrait:: | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
| PluginDiscoveryDecoratorTest:: | protected | property | The decorated discovery. | |
| PluginDiscoveryDecoratorTest:: | protected | property | The class under test. | |
| PluginDiscoveryDecoratorTest:: | protected | function | Overrides UnitTestCase:: | |
| PluginDiscoveryDecoratorTest:: | public | function | @covers ::clearCachedDefinitions | |
| PluginDiscoveryDecoratorTest:: | public | function | @covers ::clearCachedDefinitions | |
| PluginDiscoveryDecoratorTest:: | public | function | @covers ::__construct | |
| PluginDiscoveryDecoratorTest:: | public | function | @covers ::getDefinitions @covers ::processDecoratedDefinitions | |
| PluginDiscoveryDecoratorTest:: | public | function | @covers ::useCaches | |
| UnitTestCase:: | protected | property | The random generator. | |
| UnitTestCase:: | protected | property | The app root. | 1 | 
| UnitTestCase:: | protected | function | Asserts if two arrays are equal by sorting them first. | |
| UnitTestCase:: | protected | function | Mocks a block with a block plugin. | 1 | 
| UnitTestCase:: | protected | function | Returns a stub class resolver. | |
| UnitTestCase:: | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase:: | public | function | Returns a stub config storage that returns the supplied configuration. | |
| UnitTestCase:: | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase:: | protected | function | Gets the random generator for the utility methods. | |
| UnitTestCase:: | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase:: | public | function | Generates a unique random string containing letters and numbers. | 
