You are here

class PluginTypeTest in Plugin 8.2

@coversDefaultClass \Drupal\plugin\PluginType\PluginType

@group Plugin

Hierarchy

Expanded class hierarchy of PluginTypeTest

File

tests/src/Unit/PluginType/PluginTypeTest.php, line 20

Namespace

Drupal\Tests\plugin\Unit\PluginType
View source
class PluginTypeTest extends UnitTestCase {

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * The plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pluginManager;

  /**
   * The plugin type definition.
   *
   * @var mixed[]
   */
  protected $pluginTypeDefinition;

  /**
   * The class under test.
   *
   * @var \Drupal\plugin\PluginType\PluginType
   */
  protected $sut;
  protected function setUp() : void {
    $this->pluginTypeDefinition = [
      'id' => $this
        ->randomMachineName(),
      'label' => $this
        ->getRandomGenerator()
        ->string(),
      'description' => $this
        ->getRandomGenerator()
        ->string(),
      'provider' => $this
        ->randomMachineName(),
      'plugin_manager_service_id' => $this
        ->randomMachineName(),
      'field_type' => (bool) mt_rand(0, 1),
    ];
    $class_resolver = $this
      ->createMock(ClassResolverInterface::class);
    $typed_config_manager = $this
      ->createMock(TypedConfigManagerInterface::class);
    $typed_config_manager
      ->expects($this
      ->atLeastOnce())
      ->method('hasConfigSchema')
      ->willReturn(TRUE);
    $this->pluginManager = $this
      ->createMock(PluginManagerInterface::class);
    $this->container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'class_resolver',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $class_resolver,
      ],
      [
        'config.typed',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $typed_config_manager,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this
          ->getStringTranslationStub(),
      ],
      [
        $this->pluginTypeDefinition['plugin_manager_service_id'],
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->pluginManager,
      ],
    ];
    $this->container
      ->expects($this
      ->atLeastOnce())
      ->method('get')
      ->willReturnMap($map);
    $this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
  }

  /**
   * @covers ::createFromDefinition
   * @covers ::__construct
   */
  public function testCreateFromDefinition() {
    $this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
  }

  /**
   * @covers ::getId
   */
  public function testGetPluginId() {
    $this
      ->assertSame($this->pluginTypeDefinition['id'], $this->sut
      ->getId());
  }

  /**
   * @covers ::getLabel
   */
  public function testGetLabel() {
    $this
      ->assertSame($this->pluginTypeDefinition['label'], $this->sut
      ->getLabel()
      ->getUntranslatedString());
  }

  /**
   * @covers ::getDescription
   */
  public function testGetDescription() {
    $this
      ->assertSame($this->pluginTypeDefinition['description'], $this->sut
      ->getDescription()
      ->getUntranslatedString());
  }

  /**
   * @covers ::getProvider
   */
  public function testGetProvider() {
    $this
      ->assertSame($this->pluginTypeDefinition['provider'], $this->sut
      ->getProvider());
  }

  /**
   * @covers ::getPluginManager
   */
  public function testGetPluginManager() {
    $this
      ->assertSame($this->pluginManager, $this->sut
      ->getPluginManager());
  }

  /**
   * @covers ::isFieldType
   */
  public function testGetFieldType() {
    $this
      ->assertSame($this->pluginTypeDefinition['field_type'], $this->sut
      ->isFieldType());
  }

  /**
   * @covers ::getPluginConfigurationSchemaId
   */
  public function testGetPluginConfigurationSchemaIdWithDefaultId() {
    $plugin_id = 'FooBarQux';
    $expected_schema_id = sprintf('plugin.plugin_configuration.%s.%s', $this->pluginTypeDefinition['id'], $plugin_id);
    $this
      ->assertSame($expected_schema_id, $this->sut
      ->getPluginConfigurationSchemaId($plugin_id));
  }

  /**
   * @covers ::getPluginConfigurationSchemaId
   */
  public function testGetPluginConfigurationSchemaIdWithDefinedId() {
    $plugin_id = 'FooBarQux';
    $schema_id = 'foo_bar.qux.[plugin_id]';
    $this->pluginTypeDefinition['plugin_configuration_schema_id'] = $schema_id;
    $this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
    $expected_schema_id = 'foo_bar.qux.' . $plugin_id;
    $this
      ->assertSame($expected_schema_id, $this->sut
      ->getPluginConfigurationSchemaId($plugin_id));
  }

  /**
   * @covers ::ensureTypedPluginDefinition
   * @covers ::createFromDefinition
   * @covers ::__construct
   */
  public function testEnsureTypedPluginDefinition() {
    $decorated_plugin_definition = [
      'foo' => $this
        ->randomMachineName(),
    ];
    $this->pluginTypeDefinition['plugin_definition_decorator_class'] = ArrayPluginDefinitionDecorator::class;
    $this->sut = PluginType::createFromDefinition($this->container, $this->pluginTypeDefinition);
    $typed_plugin_definition = $this->sut
      ->ensureTypedPluginDefinition($decorated_plugin_definition);
    $this
      ->assertInstanceOf(PluginDefinitionInterface::class, $typed_plugin_definition);

    // We use ArrayPluginDefinitionDecorator for testing. The following
    // assertion makes sure the method under test correctly passes on the
    // decorated plugin definition to the decorator. The array handling is not
    // part of this test.

    /** @var \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator $typed_plugin_definition */
    $this
      ->assertSame($decorated_plugin_definition, $typed_plugin_definition
      ->getArrayDefinition());
  }

  /**
   * @covers ::ensureTypedPluginDefinition
   * @covers ::createFromDefinition
   * @covers ::__construct
   */
  public function testEnsureTypedPluginDefinitionWithAlreadyTypedDefinition() {
    $decorated_plugin_definition = $this
      ->createMock(PluginDefinitionInterface::class);
    $typed_plugin_definition = $this->sut
      ->ensureTypedPluginDefinition($decorated_plugin_definition);
    $this
      ->assertInstanceOf(PluginDefinitionInterface::class, $typed_plugin_definition);
  }

  /**
   * @covers ::ensureTypedPluginDefinition
   */
  public function testEnsureTypedPluginDefinitionWithoutDecorator() {
    $this
      ->expectException(Exception::class);
    $decorated_plugin_definition = [
      'foo' => $this
        ->randomMachineName(),
    ];
    $this->sut
      ->ensureTypedPluginDefinition($decorated_plugin_definition);
  }

}

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.
PluginTypeTest::$container protected property The service container.
PluginTypeTest::$pluginManager protected property The plugin manager.
PluginTypeTest::$pluginTypeDefinition protected property The plugin type definition.
PluginTypeTest::$sut protected property The class under test.
PluginTypeTest::setUp protected function Overrides UnitTestCase::setUp
PluginTypeTest::testCreateFromDefinition public function @covers ::createFromDefinition @covers ::__construct
PluginTypeTest::testEnsureTypedPluginDefinition public function @covers ::ensureTypedPluginDefinition @covers ::createFromDefinition @covers ::__construct
PluginTypeTest::testEnsureTypedPluginDefinitionWithAlreadyTypedDefinition public function @covers ::ensureTypedPluginDefinition @covers ::createFromDefinition @covers ::__construct
PluginTypeTest::testEnsureTypedPluginDefinitionWithoutDecorator public function @covers ::ensureTypedPluginDefinition
PluginTypeTest::testGetDescription public function @covers ::getDescription
PluginTypeTest::testGetFieldType public function @covers ::isFieldType
PluginTypeTest::testGetLabel public function @covers ::getLabel
PluginTypeTest::testGetPluginConfigurationSchemaIdWithDefaultId public function @covers ::getPluginConfigurationSchemaId
PluginTypeTest::testGetPluginConfigurationSchemaIdWithDefinedId public function @covers ::getPluginConfigurationSchemaId
PluginTypeTest::testGetPluginId public function @covers ::getId
PluginTypeTest::testGetPluginManager public function @covers ::getPluginManager
PluginTypeTest::testGetProvider public function @covers ::getProvider
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.