You are here

class PluginSelectorBaseTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase

@group Plugin

Hierarchy

Expanded class hierarchy of PluginSelectorBaseTest

File

tests/src/Unit/Plugin/Plugin/PluginSelector/PluginSelectorBaseTest.php, line 17

Namespace

Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector
View source
class PluginSelectorBaseTest extends PluginSelectorBaseTestBase {

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $configuration = [];
    $this->sut = $this
      ->getMockBuilder(PluginSelectorBase::class)
      ->setConstructorArgs([
      $configuration,
      $this->pluginId,
      $this->pluginDefinition,
      $this->defaultPluginResolver,
    ])
      ->getMockForAbstractClass();
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.default_plugin_resolver',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->defaultPluginResolver,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);

    /** @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase $class */
    $class = get_class($this->sut);
    $plugin = $class::create($container, [], $this->pluginId, $this->pluginDefinition);
    $this
      ->assertInstanceOf(get_class($this->sut), $plugin);
  }

  /**
   * @covers ::defaultConfiguration
   */
  public function testDefaultConfiguration() {
    $configuration = $this->sut
      ->defaultConfiguration();
    $this
      ->assertIsArray($configuration);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $this
      ->assertSame([], $this->sut
      ->calculateDependencies());
  }

  /**
   * @covers ::setConfiguration
   * @covers ::getConfiguration
   */
  public function testGetConfiguration() {
    $configuration = array(
      $this
        ->randomMachineName(),
    );
    $this
      ->assertSame($this->sut, $this->sut
      ->setConfiguration($configuration));
    $this
      ->assertSame($configuration, $this->sut
      ->getConfiguration());
  }

  /**
   * @covers ::setLabel
   * @covers ::getLabel
   */
  public function testGetLabel() {
    $label = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setLabel($label));
    $this
      ->assertSame($label, $this->sut
      ->getLabel());
  }

  /**
   * @covers ::setDescription
   * @covers ::getDescription
   */
  public function testGetDescription() {
    $description = $this
      ->randomMachineName();
    $this
      ->assertSame($this->sut, $this->sut
      ->setDescription($description));
    $this
      ->assertSame($description, $this->sut
      ->getDescription());
  }

  /**
   * @covers ::setCollectPluginConfiguration
   * @covers ::getCollectPluginConfiguration
   */
  public function testGetCollectPluginConfiguration() {
    $collect = (bool) mt_rand(0, 1);
    $this
      ->assertSame($this->sut, $this->sut
      ->setCollectPluginConfiguration($collect));
    $this
      ->assertSame($collect, $this->sut
      ->getCollectPluginConfiguration());
  }

  /**
   * @covers ::setPreviouslySelectedPlugins
   * @covers ::getPreviouslySelectedPlugins
   */
  public function testGetPreviouslySelectedPlugins() {
    $plugin = $this
      ->createMock(PluginInspectionInterface::class);
    $this->sut
      ->setPreviouslySelectedPlugins([
      $plugin,
    ]);
    $this
      ->assertSame([
      $plugin,
    ], $this->sut
      ->getPreviouslySelectedPlugins());
  }

  /**
   * @covers ::setKeepPreviouslySelectedPlugins
   * @covers ::getKeepPreviouslySelectedPlugins
   *
   * @depends testGetPreviouslySelectedPlugins
   */
  public function testGetKeepPreviouslySelectedPlugins() {
    $keep = (bool) mt_rand(0, 1);
    $plugin = $this
      ->createMock(PluginInspectionInterface::class);
    $this->sut
      ->setPreviouslySelectedPlugins([
      $plugin,
    ]);
    $this
      ->assertSame($this->sut, $this->sut
      ->setKeepPreviouslySelectedPlugins($keep));
    $this
      ->assertSame($keep, $this->sut
      ->getKeepPreviouslySelectedPlugins());

    // Confirm that all previously selected plugins are removed.
    $this->sut
      ->setPreviouslySelectedPlugins([
      $plugin,
    ]);
    $this->sut
      ->setKeepPreviouslySelectedPlugins(FALSE);
    $this
      ->assertEmpty($this->sut
      ->getPreviouslySelectedPlugins());
  }

  /**
   * @covers ::setSelectedPlugin
   * @covers ::getSelectedPlugin
   */
  public function testGetSelectedPlugin() {
    $this->sut
      ->setSelectablePluginType($this->selectablePluginType);
    $plugin = $this
      ->createMock(PluginInspectionInterface::class);
    $this
      ->assertSame($this->sut, $this->sut
      ->setSelectedPlugin($plugin));
    $this
      ->assertSame($plugin, $this->sut
      ->getSelectedPlugin());
  }

  /**
   * @covers ::setRequired
   * @covers ::isRequired
   */
  public function testGetRequired() {
    $this
      ->assertFalse($this->sut
      ->isRequired());
    $this
      ->assertSame($this->sut, $this->sut
      ->setRequired());
    $this
      ->assertTrue($this->sut
      ->isRequired());
    $this->sut
      ->setRequired(FALSE);
    $this
      ->assertFalse($this->sut
      ->isRequired());
  }

  /**
   * @covers ::buildSelectorForm
   * @covers ::setSelectablePluginType
   */
  public function testBuildSelectorForm() {
    $this->sut
      ->setSelectablePluginType($this->selectablePluginType);
    $form = [];
    $form_state = new FormState();
    $form = $this->sut
      ->buildSelectorForm($form, $form_state);
    $this
      ->assertIsArray($form);
  }

}

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.
PluginSelectorBaseTest::$sut protected property The class under test. Overrides PluginSelectorBaseTestBase::$sut
PluginSelectorBaseTest::setUp protected function Overrides PluginSelectorBaseTestBase::setUp
PluginSelectorBaseTest::testBuildSelectorForm public function @covers ::buildSelectorForm @covers ::setSelectablePluginType
PluginSelectorBaseTest::testCalculateDependencies public function @covers ::calculateDependencies
PluginSelectorBaseTest::testCreate function @covers ::create @covers ::__construct
PluginSelectorBaseTest::testDefaultConfiguration public function @covers ::defaultConfiguration
PluginSelectorBaseTest::testGetCollectPluginConfiguration public function @covers ::setCollectPluginConfiguration @covers ::getCollectPluginConfiguration
PluginSelectorBaseTest::testGetConfiguration public function @covers ::setConfiguration @covers ::getConfiguration
PluginSelectorBaseTest::testGetDescription public function @covers ::setDescription @covers ::getDescription
PluginSelectorBaseTest::testGetKeepPreviouslySelectedPlugins public function @covers ::setKeepPreviouslySelectedPlugins @covers ::getKeepPreviouslySelectedPlugins
PluginSelectorBaseTest::testGetLabel public function @covers ::setLabel @covers ::getLabel
PluginSelectorBaseTest::testGetPreviouslySelectedPlugins public function @covers ::setPreviouslySelectedPlugins @covers ::getPreviouslySelectedPlugins
PluginSelectorBaseTest::testGetRequired public function @covers ::setRequired @covers ::isRequired
PluginSelectorBaseTest::testGetSelectedPlugin public function @covers ::setSelectedPlugin @covers ::getSelectedPlugin
PluginSelectorBaseTestBase::$defaultPluginResolver protected property The default plugin resolver.
PluginSelectorBaseTestBase::$pluginDefinition protected property The plugin definition of the class under test.
PluginSelectorBaseTestBase::$pluginId protected property The plugin ID of the class plugin under test.
PluginSelectorBaseTestBase::$selectablePluginManager protected property The plugin manager through which to select plugins.
PluginSelectorBaseTestBase::$selectablePluginType protected property The plugin type of which to select plugins.
PluginSelectorBaseTestBase::$selectedPlugin protected property The selected plugin.
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.