PluginDiscoveryDecoratorTest.php in Plugin 8.2
File
tests/src/Unit/PluginDiscovery/PluginDiscoveryDecoratorTest.php
View source
<?php
namespace Drupal\Tests\plugin\Unit\PluginDiscovery;
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\plugin\PluginDiscovery\PluginDiscoveryDecorator;
use Drupal\Tests\UnitTestCase;
class PluginDiscoveryDecoratorTest extends UnitTestCase {
protected $decoratedDiscovery;
protected $sut;
protected function setUp() : void {
$this->decoratedDiscovery = $this
->createMock(DiscoveryInterface::class);
$this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
}
public function testConstruct() {
$this->sut = new PluginDiscoveryDecorator($this->decoratedDiscovery);
$this
->assertInstanceOf(PluginDiscoveryDecorator::class, $this->sut);
}
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());
}
public function testClearCachedDefinitionsWithUncachedDecoratedDiscovery() {
$this->decoratedDiscovery
->expects($this
->exactly(2))
->method('getDefinitions')
->willReturn([]);
$this->sut
->getDefinitions();
$this->sut
->getDefinitions();
$this->sut
->clearCachedDefinitions();
$this->sut
->getDefinitions();
}
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([]);
$this->sut
->getDefinitions();
$this->sut
->getDefinitions();
$this->sut
->clearCachedDefinitions();
$this->sut
->getDefinitions();
}
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([]);
$this->sut
->getDefinitions();
$this->sut
->getDefinitions();
$this->sut
->useCaches(FALSE);
$this->sut
->getDefinitions();
$this->sut
->getDefinitions();
}
}
abstract class PluginDiscoveryDecoratorTestCachedDiscovery implements DiscoveryInterface, CachedDiscoveryInterface {
}