You are here

class PluginSelectorDeriverTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\Plugin\Field\FieldWidget\PluginSelectorDeriver

@group Plugin

Hierarchy

Expanded class hierarchy of PluginSelectorDeriverTest

File

tests/src/Unit/Plugin/Field/FieldWidget/PluginSelectorDeriverTest.php, line 15

Namespace

Drupal\Tests\plugin\Unit\Plugin\Field\FieldWidget
View source
class PluginSelectorDeriverTest extends UnitTestCase {

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pluginSelectorManager;

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\Plugin\Field\FieldWidget\PluginSelectorDeriver
   */
  protected $sut;
  protected function setUp() : void {
    parent::setUp();
    $this->pluginSelectorManager = $this
      ->createMock(PluginSelectorManagerInterface::class);
    $this->sut = new PluginSelectorDeriver($this->pluginSelectorManager);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'plugin.manager.plugin.plugin_selector',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginSelectorManager,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = PluginSelectorDeriver::create($container, $this
      ->randomMachineName());
    $this
      ->assertInstanceOf(PluginSelectorDeriver::class, $sut);
  }

  /**
   * @covers ::getDerivativeDefinitions
   */
  function testGetDerivativeDefinitions() {
    $provider = $this
      ->randomMachineName();
    $plugin_selector_id_a = $this
      ->randomMachineName();
    $plugin_selector_label_a = $this
      ->randomMachineName();
    $plugin_selector_description_a = $this
      ->randomMachineName();
    $plugin_selector_definition_a = [
      'id' => $plugin_selector_id_a,
      'label' => $plugin_selector_label_a,
      'description' => $plugin_selector_description_a,
    ];
    $plugin_selector_id_b = $this
      ->randomMachineName();
    $plugin_selector_label_b = $this
      ->randomMachineName();
    $plugin_selector_description_b = '';
    $plugin_selector_definition_b = [
      'id' => $plugin_selector_id_b,
      'label' => $plugin_selector_label_b,
      'description' => $plugin_selector_description_b,
    ];
    $plugin_selector_definitions = [
      $plugin_selector_id_a => $plugin_selector_definition_a,
      $plugin_selector_id_b => $plugin_selector_definition_b,
    ];
    $this->pluginSelectorManager
      ->expects($this
      ->atLeastOnce())
      ->method('getDefinitions')
      ->willReturn($plugin_selector_definitions);
    $base_plugin_definition = [
      'provider' => $provider,
    ];
    $derivative_definitions = $this->sut
      ->getDerivativeDefinitions($base_plugin_definition);
    $this
      ->assertSame($plugin_selector_label_a, (string) $derivative_definitions[$plugin_selector_id_a]['label']);
    $this
      ->assertSame($plugin_selector_description_a, (string) $derivative_definitions[$plugin_selector_id_a]['description']);
    $this
      ->assertSame($provider, $derivative_definitions[$plugin_selector_id_a]['provider']);
    $this
      ->assertSame($plugin_selector_id_a, $derivative_definitions[$plugin_selector_id_a]['plugin_selector_id']);
    $this
      ->assertSame($plugin_selector_label_b, (string) $derivative_definitions[$plugin_selector_id_b]['label']);
    $this
      ->assertSame($plugin_selector_description_b, (string) $derivative_definitions[$plugin_selector_id_b]['description']);
    $this
      ->assertSame($provider, $derivative_definitions[$plugin_selector_id_b]['provider']);
    $this
      ->assertSame($plugin_selector_id_b, $derivative_definitions[$plugin_selector_id_b]['plugin_selector_id']);
  }

}

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.
PluginSelectorDeriverTest::$pluginSelectorManager protected property The plugin selector manager.
PluginSelectorDeriverTest::$sut protected property The class under test.
PluginSelectorDeriverTest::setUp protected function Overrides UnitTestCase::setUp
PluginSelectorDeriverTest::testCreate function @covers ::create @covers ::__construct
PluginSelectorDeriverTest::testGetDerivativeDefinitions function @covers ::getDerivativeDefinitions
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.