You are here

class PluginTypeConverterTest in Plugin 8.2

Same name in this branch
  1. 8.2 tests/src/Functional/ParamConverter/PluginTypeConverterTest.php \Drupal\Tests\plugin\Functional\ParamConverter\PluginTypeConverterTest
  2. 8.2 tests/src/Unit/ParamConverter/PluginTypeConverterTest.php \Drupal\Tests\plugin\Unit\ParamConverter\PluginTypeConverterTest

@coversDefaultClass \Drupal\plugin\ParamConverter\PluginTypeConverter

@group Plugin

Hierarchy

Expanded class hierarchy of PluginTypeConverterTest

File

tests/src/Unit/ParamConverter/PluginTypeConverterTest.php, line 16

Namespace

Drupal\Tests\plugin\Unit\ParamConverter
View source
class PluginTypeConverterTest extends UnitTestCase {

  /**
   * The plugin manager.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $pluginTypeManager;

  /**
   * The system under test.
   *
   * @var \Drupal\plugin\ParamConverter\PluginTypeConverter
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->pluginTypeManager = $this
      ->prophesize(PluginTypeManagerInterface::class);
    $this->sut = new PluginTypeConverter($this->pluginTypeManager
      ->reveal());
  }

  /**
   * @covers ::applies
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   *
   * @dataProvider provideApplies
   */
  public function testApplies($expected, $definition) {
    $name = 'foo_bar';
    $route = $this
      ->prophesize(Route::class);
    $this
      ->assertSame($expected, $this->sut
      ->applies($definition, $name, $route
      ->reveal()));
  }

  /**
   * Provides data to self::testApplies().
   */
  public function provideApplies() {
    $data = [];
    $data['applies-because-implicitly-enabled'] = [
      TRUE,
      [
        'plugin.plugin_type' => [],
      ],
    ];
    $data['applies-because-explicitly-enabled'] = [
      TRUE,
      [
        'plugin.plugin_type' => [
          'enabled' => TRUE,
        ],
      ],
    ];
    $data['applies-not-because-disabled'] = [
      FALSE,
      [
        'plugin.plugin_type' => [
          'enabled' => FALSE,
        ],
      ],
    ];
    $data['applies-not-because-non-existent'] = [
      FALSE,
      [],
    ];
    return $data;
  }

  /**
   * @covers ::convert
   * @covers ::doConvert
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   */
  public function testConvertWithExceptionReturnsNull() {
    $plugin_type_id = 'foo_bar.baz';
    $definition = [
      'plugin.plugin_type' => [],
    ];
    $name = 'foo_bar';
    $defaults = [];
    $this->pluginTypeManager
      ->hasPluginType($plugin_type_id)
      ->willReturn(FALSE);
    $original_error_reporting = error_reporting();
    error_reporting($original_error_reporting & ~E_USER_WARNING);
    $this
      ->assertNull($this->sut
      ->convert($plugin_type_id, $definition, $name, $defaults));
    error_reporting($original_error_reporting);
  }

  /**
   * @covers ::convert
   * @covers ::doConvert
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   */
  public function testConvertWithKnownPluginType() {
    $plugin_type_id = 'foo_bar.baz';
    $definition = [
      'plugin.plugin_type' => [],
    ];
    $name = 'foo_bar';
    $defaults = [];
    $plugin_type = $this
      ->prophesize(PluginTypeInterface::class);
    $this->pluginTypeManager
      ->hasPluginType($plugin_type_id)
      ->willReturn(TRUE);
    $this->pluginTypeManager
      ->getPluginType($plugin_type_id)
      ->willReturn($plugin_type
      ->reveal());
    $this
      ->assertSame($plugin_type
      ->reveal(), $this->sut
      ->convert($plugin_type_id, $definition, $name, $defaults));
  }

  /**
   * @covers ::convert
   * @covers ::doConvert
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   */
  public function testConvertWithUnknownPluginType() {
    $plugin_type_id = 'foo_bar.baz';
    $definition = [
      'plugin.plugin_type' => [],
    ];
    $name = 'foo_bar';
    $defaults = [];
    $this->pluginTypeManager
      ->hasPluginType($plugin_type_id)
      ->willReturn(FALSE);
    $this
      ->assertNull($this->sut
      ->convert($plugin_type_id, $definition, $name, $defaults));
  }

  /**
   * @covers ::convert
   * @covers ::doConvert
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   */
  public function testConvertWithInvalidDefinition() {

    // Leave out the "plugin.plugin_type" key.
    $definition = [];
    $plugin_type_id = 'foozaar.bazaar';
    $name = 'foo_bar';
    $defaults = [];
    $this
      ->assertNull($this->sut
      ->convert($plugin_type_id, $definition, $name, $defaults));
  }

}

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.
PluginTypeConverterTest::$pluginTypeManager protected property The plugin manager.
PluginTypeConverterTest::$sut protected property The system under test.
PluginTypeConverterTest::provideApplies public function Provides data to self::testApplies().
PluginTypeConverterTest::setUp protected function Overrides UnitTestCase::setUp
PluginTypeConverterTest::testApplies public function @covers ::applies @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginTypeConverterTest::testConvertWithExceptionReturnsNull public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginTypeConverterTest::testConvertWithInvalidDefinition public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginTypeConverterTest::testConvertWithKnownPluginType public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginTypeConverterTest::testConvertWithUnknownPluginType public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
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.