You are here

class PluginDiscoveryDecoratorTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\PluginDiscovery\PluginDiscoveryDecorator

@group Plugin

Hierarchy

Expanded class hierarchy of PluginDiscoveryDecoratorTest

File

tests/src/Unit/PluginDiscovery/PluginDiscoveryDecoratorTest.php, line 15

Namespace

Drupal\Tests\plugin\Unit\PluginDiscovery
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PluginDiscoveryDecoratorTest::$decoratedDiscovery protected property The decorated discovery.
PluginDiscoveryDecoratorTest::$sut protected property The class under test.
PluginDiscoveryDecoratorTest::setUp protected function Overrides UnitTestCase::setUp
PluginDiscoveryDecoratorTest::testClearCachedDefinitionsWithCachedDecoratedDiscovery public function @covers ::clearCachedDefinitions
PluginDiscoveryDecoratorTest::testClearCachedDefinitionsWithUncachedDecoratedDiscovery public function @covers ::clearCachedDefinitions
PluginDiscoveryDecoratorTest::testConstruct public function @covers ::__construct
PluginDiscoveryDecoratorTest::testGetDefinitions public function @covers ::getDefinitions @covers ::processDecoratedDefinitions
PluginDiscoveryDecoratorTest::testUseCachesWithCachedDecoratedDiscovery public function @covers ::useCaches
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.