DefaultFactoryTest.php 5.55 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\Component\Plugin;

5
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6
use Drupal\Component\Plugin\Factory\DefaultFactory;
7 8 9
use Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry;
use Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface;
use Drupal\plugin_test\Plugin\plugin_test\fruit\Kale;
10 11 12
use Drupal\Tests\UnitTestCase;

/**
13
 * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
14
 * @group Plugin
15 16 17 18
 */
class DefaultFactoryTest extends UnitTestCase {

  /**
19 20 21
   * Tests getPluginClass() with a valid array plugin definition.
   *
   * @covers ::getPluginClass
22
   */
23 24
  public function testGetPluginClassWithValidArrayPluginDefinition() {
    $plugin_class = Cherry::class;
25 26 27 28 29
    $class = DefaultFactory::getPluginClass('cherry', ['class' => $plugin_class]);

    $this->assertEquals($plugin_class, $class);
  }

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /**
   * 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);
  }

46 47 48
  /**
   * Tests getPluginClass() with a missing class definition.
   *
49 50
   * @covers ::getPluginClass
   *
51 52 53
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage The plugin (cherry) did not specify an instance class.
   */
54
  public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
55 56 57
    DefaultFactory::getPluginClass('cherry', []);
  }

58 59 60 61 62 63 64 65 66 67 68 69 70
  /**
   * 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);
  }

71 72 73
  /**
   * Tests getPluginClass() with a not existing class definition.
   *
74 75
   * @covers ::getPluginClass
   *
76 77 78
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   * @expectedExceptionMessage Plugin (kiwifruit) instance class "\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit" does not exist.
   */
79
  public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
80 81 82
    DefaultFactory::getPluginClass('kiwifruit', ['class' => '\Drupal\plugin_test\Plugin\plugin_test\fruit\Kiwifruit']);
  }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * 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);
  }

111 112
  /**
   * Tests getPluginClass() with a required interface.
113 114
   *
   * @covers ::getPluginClass
115
   */
116 117 118 119 120 121 122
  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);
123 124 125 126 127 128 129

    $this->assertEquals($plugin_class, $class);
  }

  /**
   * Tests getPluginClass() with a required interface but no implementation.
   *
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
   * @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
   *
145 146
   * @expectedException \Drupal\Component\Plugin\Exception\PluginException
   */
147 148 149 150 151 152 153
  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);
154 155 156
  }

}