You are here

class DefaultFactoryTest in Zircon Profile 8

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

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

Hierarchy

Expanded class hierarchy of DefaultFactoryTest

File

core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php, line 21
Contains \Drupal\Tests\Component\Plugin\DefaultFactoryTest.

Namespace

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

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

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

  /**
   * Tests getPluginClass() with a missing class definition.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage The plugin (cherry) did not specify an instance class.
   */
  public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
    DefaultFactory::getPluginClass('cherry', []);
  }

  /**
   * Tests getPluginClass() with a missing class definition.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage The plugin (cherry) did not specify an instance class.
   */
  public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
    $plugin_definition = $this
      ->getMock(PluginDefinitionInterface::class);
    DefaultFactory::getPluginClass('cherry', $plugin_definition);
  }

  /**
   * Tests getPluginClass() with a not existing class definition.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.
   */
  public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
    DefaultFactory::getPluginClass('kiwifruit', [
      'class' => '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Kiwifruit',
    ]);
  }

  /**
   * Tests getPluginClass() with a not existing class definition.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
    $plugin_class = '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Kiwifruit';
    $plugin_definition = $this
      ->getMock(PluginDefinitionInterface::class);
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    DefaultFactory::getPluginClass('kiwifruit', $plugin_definition);
  }

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

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

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage Plugin "cherry" (Drupal\plugin_test\Plugin\plugin_test\fruit\Kale) must implement interface Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface.
   */
  public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
    $plugin_class = Kale::class;
    DefaultFactory::getPluginClass('cherry', [
      'class' => $plugin_class,
      'provider' => 'core',
    ], FruitInterface::class);
  }

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
   * @covers ::getPluginClass
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
    $plugin_class = Kale::class;
    $plugin_definition = $this
      ->getMock(PluginDefinitionInterface::class);
    $plugin_definition
      ->expects($this
      ->atLeastOnce())
      ->method('getClass')
      ->willReturn($plugin_class);
    DefaultFactory::getPluginClass('cherry', $plugin_definition, FruitInterface::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 not existing class definition.
DefaultFactoryTest::testGetPluginClassWithNotExistingClassWithObjectPluginDefinition public function Tests getPluginClass() with a not existing 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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.
UnitTestCase::setUp protected function 259