You are here

class DefaultFactoryTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php \Drupal\Tests\Component\Plugin\DefaultFactoryTest

@coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory @group Plugin

Hierarchy

  • class \Drupal\Tests\Component\Plugin\DefaultFactoryTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of DefaultFactoryTest

File

core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php, line 17

Namespace

Drupal\Tests\Component\Plugin
View source
class DefaultFactoryTest extends TestCase {

  /**
   * Tests getPluginClass() with a valid array plugin definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithValidArrayPluginDefinition() {
    $plugin_class = Corn::class;
    $class = DefaultFactory::getPluginClass('corn', [
      'class' => $plugin_class,
    ]);
    $this
      ->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a valid object plugin definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithValidObjectPluginDefinition() {
    $plugin_class = Corn::class;
    $plugin_definition = $this
      ->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    $class = DefaultFactory::getPluginClass('corn', $plugin_definition);
    $this
      ->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a missing class definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
    $this
      ->expectException(PluginException::class);
    $this
      ->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
    DefaultFactory::getPluginClass('corn', []);
  }

  /**
   * Tests getPluginClass() with a missing class definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
    $plugin_definition = $this
      ->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $this
      ->expectException(PluginException::class);
    $this
      ->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
    DefaultFactory::getPluginClass('corn', $plugin_definition);
  }

  /**
   * Tests getPluginClass() with a non-existent class definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
    $this
      ->expectException(PluginException::class);
    $this
      ->expectExceptionMessage('Plugin (carrot) instance class "Drupal\\Tests\\Component\\Plugin\\Fixtures\\vegetable\\Carrot" does not exist.');
    DefaultFactory::getPluginClass('carrot', [
      'class' => 'Drupal\\Tests\\Component\\Plugin\\Fixtures\\vegetable\\Carrot',
    ]);
  }

  /**
   * Tests getPluginClass() with a non-existent class definition.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
    $plugin_class = 'Drupal\\Tests\\Component\\Plugin\\Fixtures\\vegetable\\Carrot';
    $plugin_definition = $this
      ->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    $this
      ->expectException(PluginException::class);
    DefaultFactory::getPluginClass('carrot', $plugin_definition);
  }

  /**
   * Tests getPluginClass() with a required interface.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
    $plugin_class = Corn::class;
    $class = DefaultFactory::getPluginClass('corn', [
      'class' => $plugin_class,
    ], VegetableInterface::class);
    $this
      ->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a required interface.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
    $plugin_class = Corn::class;
    $plugin_definition = $this
      ->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    $class = DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
    $this
      ->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
    $this
      ->expectException(PluginException::class);
    $this
      ->expectExceptionMessage('Plugin "corn" (Drupal\\Tests\\Component\\Plugin\\Fixtures\\vegetable\\Broccoli) must implement interface Drupal\\Tests\\Component\\Plugin\\Fixtures\\vegetable\\VegetableInterface.');
    DefaultFactory::getPluginClass('corn', [
      'class' => Broccoli::class,
    ], VegetableInterface::class);
  }

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
   * @covers ::getPluginClass
   */
  public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
    $plugin_class = Broccoli::class;
    $plugin_definition = $this
      ->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    $this
      ->expectException(PluginException::class);
    DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultFactoryTest::testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition public function Tests getPluginClass() with a required interface but no implementation.
DefaultFactoryTest::testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition public function Tests getPluginClass() with a required interface but no implementation.
DefaultFactoryTest::testGetPluginClassWithInterfaceWithArrayPluginDefinition public function Tests getPluginClass() with a required interface.
DefaultFactoryTest::testGetPluginClassWithInterfaceWithObjectPluginDefinition public function Tests getPluginClass() with a required interface.
DefaultFactoryTest::testGetPluginClassWithMissingClassWithArrayPluginDefinition public function Tests getPluginClass() with a missing class definition.
DefaultFactoryTest::testGetPluginClassWithMissingClassWithObjectPluginDefinition public function Tests getPluginClass() with a missing class definition.
DefaultFactoryTest::testGetPluginClassWithNotExistingClassWithArrayPluginDefinition public function Tests getPluginClass() with a non-existent class definition.
DefaultFactoryTest::testGetPluginClassWithNotExistingClassWithObjectPluginDefinition public function Tests getPluginClass() with a non-existent class definition.
DefaultFactoryTest::testGetPluginClassWithValidArrayPluginDefinition public function Tests getPluginClass() with a valid array plugin definition.
DefaultFactoryTest::testGetPluginClassWithValidObjectPluginDefinition public function Tests getPluginClass() with a valid object plugin definition.