You are here

class PluginManagerDecoratorTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\PluginManager\PluginManagerDecorator

@group Plugin

Hierarchy

Expanded class hierarchy of PluginManagerDecoratorTest

File

tests/src/Unit/PluginManager/PluginManagerDecoratorTest.php, line 18

Namespace

Drupal\Tests\plugin\Unit\PluginManager
View source
class PluginManagerDecoratorTest extends UnitTestCase {

  /**
   * The decorated plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $decoratedDiscovery;

  /**
   * The decorated plugin factory.
   *
   * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $decoratedFactory;

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\PluginManager\PluginManagerDecorator
   */
  protected $sut;

  /**
   * @covers ::__construct
   * @covers ::createInstance
   */
  public function testCreateInstanceWithExistingPlugin() {
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $this->decoratedDiscovery = $plugin_manager;
    $this->decoratedFactory = $plugin_manager;
    $this->sut = new PluginManagerDecorator($plugin_manager);
    $plugin_id = $this
      ->randomMachineName();
    $plugin = $this
      ->createMock(PluginInspectionInterface::class);
    $plugin_definitions = [
      $plugin_id => [
        'id' => $plugin_id,
      ],
    ];
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $this->decoratedFactory
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($plugin_id)
      ->willReturn($plugin);
    $this
      ->assertSame($plugin, $this->sut
      ->createInstance($plugin_id));
  }

  /**
   * @covers ::__construct
   * @covers ::createInstance
   */
  public function testCreateInstanceWithExistingPluginAndOverriddenDiscovery() {
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $this->decoratedDiscovery = $this
      ->createMock(DiscoveryInterface::class);
    $this->decoratedFactory = $plugin_manager;
    $this->sut = new PluginManagerDecorator($plugin_manager, $this->decoratedDiscovery);
    $plugin_id = $this
      ->randomMachineName();
    $plugin = $this
      ->createMock(PluginInspectionInterface::class);
    $plugin_definitions = [
      $plugin_id => [
        'id' => $plugin_id,
      ],
    ];
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $this->decoratedFactory
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($plugin_id)
      ->willReturn($plugin);
    $this
      ->assertSame($plugin, $this->sut
      ->createInstance($plugin_id));
  }

  /**
   * @covers ::__construct
   * @covers ::createInstance
   */
  public function testCreateInstanceWithNonExistingPlugin() {
    $this
      ->expectException(PluginNotFoundException::class);
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $this->decoratedDiscovery = $plugin_manager;
    $this->decoratedFactory = $plugin_manager;
    $this->sut = new PluginManagerDecorator($plugin_manager);
    $plugin_id = $this
      ->randomMachineName();
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn([]);
    $this->decoratedFactory
      ->expects($this
      ->never())
      ->method('createInstance');
    $this->sut
      ->createInstance($plugin_id);
  }

  /**
   * @covers ::__construct
   * @covers ::createInstance
   */
  public function testCreateInstanceWithNonExistingPluginAndOverriddenDiscovery() {
    $this
      ->expectException(PluginNotFoundException::class);
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $this->decoratedDiscovery = $this
      ->createMock(DiscoveryInterface::class);
    $this->decoratedFactory = $plugin_manager;
    $this->sut = new PluginManagerDecorator($plugin_manager, $this->decoratedDiscovery);
    $plugin_id = $this
      ->randomMachineName();
    $this->decoratedDiscovery
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn([]);
    $this->decoratedFactory
      ->expects($this
      ->never())
      ->method('createInstance');
    $this->sut
      ->createInstance($plugin_id);
  }

  /**
   * @covers ::getInstance
   */
  public function testGetInstance() {
    $this
      ->expectException(BadMethodCallException::class);
    $plugin_manager = $this
      ->createMock(PluginManagerInterface::class);
    $this->sut = new PluginManagerDecorator($plugin_manager);
    $this->sut
      ->getInstance([]);
  }

}

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.
PluginManagerDecoratorTest::$decoratedDiscovery protected property The decorated plugin discovery.
PluginManagerDecoratorTest::$decoratedFactory protected property The decorated plugin factory.
PluginManagerDecoratorTest::$sut protected property The class under test.
PluginManagerDecoratorTest::testCreateInstanceWithExistingPlugin public function @covers ::__construct @covers ::createInstance
PluginManagerDecoratorTest::testCreateInstanceWithExistingPluginAndOverriddenDiscovery public function @covers ::__construct @covers ::createInstance
PluginManagerDecoratorTest::testCreateInstanceWithNonExistingPlugin public function @covers ::__construct @covers ::createInstance
PluginManagerDecoratorTest::testCreateInstanceWithNonExistingPluginAndOverriddenDiscovery public function @covers ::__construct @covers ::createInstance
PluginManagerDecoratorTest::testGetInstance public function @covers ::getInstance
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.
UnitTestCase::setUp protected function 340