You are here

class PluginInstanceConverterTest in Plugin 8.2

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

@coversDefaultClass \Drupal\plugin\ParamConverter\PluginInstanceConverter

@group Plugin

Hierarchy

Expanded class hierarchy of PluginInstanceConverterTest

File

tests/src/Unit/ParamConverter/PluginInstanceConverterTest.php, line 18

Namespace

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

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

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->pluginTypeManager = $this
      ->prophesize(PluginTypeManagerInterface::class);
    $this->sut = new PluginInstanceConverter($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_instance' => [
          'plugin_type_id' => 'foo.bar',
        ],
      ],
    ];
    $data['applies-because-explicitly-enabled'] = [
      TRUE,
      [
        'plugin.plugin_instance' => [
          'enabled' => TRUE,
          'plugin_type_id' => 'foo.bar',
        ],
      ],
    ];
    $data['applies-not-because-disabled'] = [
      FALSE,
      [
        'plugin.plugin_instance' => [
          'enabled' => FALSE,
          'plugin_type_id' => 'foo.bar',
        ],
      ],
    ];
    $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_instance' => [
        'plugin_type_id' => $plugin_type_id,
      ],
    ];
    $plugin_id = 'foozaar.bazaar';
    $name = 'foo_bar';
    $defaults = [];
    $plugin_manager = $this
      ->prophesize(PluginManagerInterface::class);
    $plugin_manager
      ->hasDefinition($plugin_id)
      ->willReturn(TRUE);
    $plugin_manager
      ->createInstance($plugin_id)
      ->willThrow(new PluginNotFoundException($plugin_id));
    $plugin_type = $this
      ->prophesize(PluginTypeInterface::class);
    $plugin_type
      ->getPluginManager()
      ->willReturn($plugin_manager);
    $this->pluginTypeManager
      ->getPluginType($plugin_type_id)
      ->willReturn($plugin_type);
    $original_error_reporting = error_reporting();
    error_reporting($original_error_reporting & ~E_USER_WARNING);
    $this
      ->assertNull($this->sut
      ->convert($plugin_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 testConvertWithKnownPlugin() {
    $plugin_type_id = 'foo_bar.baz';
    $definition = [
      'plugin.plugin_instance' => [
        'plugin_type_id' => $plugin_type_id,
      ],
    ];
    $plugin_id = 'foozaar.bazaar';
    $name = 'foo_bar';
    $defaults = [];
    $plugin_instance = new \stdClass();
    $plugin_manager = $this
      ->prophesize(PluginManagerInterface::class);
    $plugin_manager
      ->hasDefinition($plugin_id)
      ->willReturn(TRUE);
    $plugin_manager
      ->createInstance($plugin_id)
      ->willReturn($plugin_instance);
    $plugin_type = $this
      ->prophesize(PluginTypeInterface::class);
    $plugin_type
      ->getPluginManager()
      ->willReturn($plugin_manager);
    $this->pluginTypeManager
      ->getPluginType($plugin_type_id)
      ->willReturn($plugin_type);
    $this
      ->assertSame($plugin_instance, $this->sut
      ->convert($plugin_id, $definition, $name, $defaults));
  }

  /**
   * @covers ::convert
   * @covers ::doConvert
   * @covers ::validateParameterDefinition
   * @covers ::getConverterDefinitionConstraint
   * @covers ::getConverterDefinition
   * @covers ::getConverterDefinitionKey
   * @covers ::__construct
   */
  public function testConvertWithUnknownPlugin() {
    $plugin_type_id = 'foo_bar.baz';
    $definition = [
      'plugin.plugin_instance' => [
        'plugin_type_id' => $plugin_type_id,
      ],
    ];
    $plugin_id = 'foozaar.bazaar';
    $name = 'foo_bar';
    $defaults = [];
    $plugin_manager = $this
      ->prophesize(PluginManagerInterface::class);
    $plugin_manager
      ->hasDefinition($plugin_id)
      ->willReturn(FALSE);
    $plugin_type = $this
      ->prophesize(PluginTypeInterface::class);
    $plugin_type
      ->getPluginManager()
      ->willReturn($plugin_manager);
    $this->pluginTypeManager
      ->getPluginType($plugin_type_id)
      ->willReturn($plugin_type);
    $original_error_reporting = error_reporting();
    error_reporting($original_error_reporting & ~E_USER_WARNING);
    $this
      ->assertNull($this->sut
      ->convert($plugin_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 testConvertWithInvalidDefinition() {

    // Leave out the "plugin.plugin_instance" key.
    $definition = [];
    $plugin_id = 'foozaar.bazaar';
    $name = 'foo_bar';
    $defaults = [];
    $this
      ->assertNull($this->sut
      ->convert($plugin_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.
PluginInstanceConverterTest::$pluginTypeManager protected property The plugin manager.
PluginInstanceConverterTest::$sut protected property The system under test.
PluginInstanceConverterTest::provideApplies public function Provides data to self::testApplies().
PluginInstanceConverterTest::setUp protected function Overrides UnitTestCase::setUp
PluginInstanceConverterTest::testApplies public function @covers ::applies @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginInstanceConverterTest::testConvertWithExceptionReturnsNull public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginInstanceConverterTest::testConvertWithInvalidDefinition public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginInstanceConverterTest::testConvertWithKnownPlugin public function @covers ::convert @covers ::doConvert @covers ::validateParameterDefinition @covers ::getConverterDefinitionConstraint @covers ::getConverterDefinition @covers ::getConverterDefinitionKey @covers ::__construct
PluginInstanceConverterTest::testConvertWithUnknownPlugin 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.