DefaultFactoryTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Drupal\Tests\Component\Plugin;
  3. use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
  4. use Drupal\Component\Plugin\Exception\PluginException;
  5. use Drupal\Component\Plugin\Factory\DefaultFactory;
  6. use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli;
  7. use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Corn;
  8. use Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface;
  9. use PHPUnit\Framework\TestCase;
  10. /**
  11. * @coversDefaultClass \Drupal\Component\Plugin\Factory\DefaultFactory
  12. * @group Plugin
  13. */
  14. class DefaultFactoryTest extends TestCase {
  15. /**
  16. * Tests getPluginClass() with a valid array plugin definition.
  17. *
  18. * @covers ::getPluginClass
  19. */
  20. public function testGetPluginClassWithValidArrayPluginDefinition() {
  21. $plugin_class = Corn::class;
  22. $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class]);
  23. $this->assertEquals($plugin_class, $class);
  24. }
  25. /**
  26. * Tests getPluginClass() with a valid object plugin definition.
  27. *
  28. * @covers ::getPluginClass
  29. */
  30. public function testGetPluginClassWithValidObjectPluginDefinition() {
  31. $plugin_class = Corn::class;
  32. $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
  33. $plugin_definition->expects($this->atLeastOnce())
  34. ->method('getClass')
  35. ->willReturn($plugin_class);
  36. $class = DefaultFactory::getPluginClass('corn', $plugin_definition);
  37. $this->assertEquals($plugin_class, $class);
  38. }
  39. /**
  40. * Tests getPluginClass() with a missing class definition.
  41. *
  42. * @covers ::getPluginClass
  43. */
  44. public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
  45. if (method_exists($this, 'expectException')) {
  46. $this->expectException(PluginException::class);
  47. $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
  48. }
  49. else {
  50. $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
  51. }
  52. DefaultFactory::getPluginClass('corn', []);
  53. }
  54. /**
  55. * Tests getPluginClass() with a missing class definition.
  56. *
  57. * @covers ::getPluginClass
  58. */
  59. public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
  60. $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
  61. if (method_exists($this, 'expectException')) {
  62. $this->expectException(PluginException::class);
  63. $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
  64. }
  65. else {
  66. $this->setExpectedException(PluginException::class, 'The plugin (corn) did not specify an instance class.');
  67. }
  68. DefaultFactory::getPluginClass('corn', $plugin_definition);
  69. }
  70. /**
  71. * Tests getPluginClass() with a not existing class definition.
  72. *
  73. * @covers ::getPluginClass
  74. */
  75. public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
  76. if (method_exists($this, 'expectException')) {
  77. $this->expectException(PluginException::class);
  78. $this->expectExceptionMessage('Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
  79. }
  80. else {
  81. $this->setExpectedException(PluginException::class, 'Plugin (carrot) instance class "Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot" does not exist.');
  82. }
  83. DefaultFactory::getPluginClass('carrot', ['class' => 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot']);
  84. }
  85. /**
  86. * Tests getPluginClass() with a not existing class definition.
  87. *
  88. * @covers ::getPluginClass
  89. */
  90. public function testGetPluginClassWithNotExistingClassWithObjectPluginDefinition() {
  91. $plugin_class = 'Drupal\Tests\Component\Plugin\Fixtures\vegetable\Carrot';
  92. $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
  93. $plugin_definition->expects($this->atLeastOnce())
  94. ->method('getClass')
  95. ->willReturn($plugin_class);
  96. if (method_exists($this, 'expectException')) {
  97. $this->expectException(PluginException::class);
  98. }
  99. else {
  100. $this->setExpectedException(PluginException::class);
  101. }
  102. DefaultFactory::getPluginClass('carrot', $plugin_definition);
  103. }
  104. /**
  105. * Tests getPluginClass() with a required interface.
  106. *
  107. * @covers ::getPluginClass
  108. */
  109. public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
  110. $plugin_class = Corn::class;
  111. $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class], VegetableInterface::class);
  112. $this->assertEquals($plugin_class, $class);
  113. }
  114. /**
  115. * Tests getPluginClass() with a required interface.
  116. *
  117. * @covers ::getPluginClass
  118. */
  119. public function testGetPluginClassWithInterfaceWithObjectPluginDefinition() {
  120. $plugin_class = Corn::class;
  121. $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
  122. $plugin_definition->expects($this->atLeastOnce())
  123. ->method('getClass')
  124. ->willReturn($plugin_class);
  125. $class = DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
  126. $this->assertEquals($plugin_class, $class);
  127. }
  128. /**
  129. * Tests getPluginClass() with a required interface but no implementation.
  130. *
  131. * @covers ::getPluginClass
  132. */
  133. public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
  134. if (method_exists($this, 'expectException')) {
  135. $this->expectException(PluginException::class);
  136. $this->expectExceptionMessage('Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
  137. }
  138. else {
  139. $this->setExpectedException(PluginException::class, 'Plugin "corn" (Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli) must implement interface Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface.');
  140. }
  141. DefaultFactory::getPluginClass('corn', ['class' => Broccoli::class], VegetableInterface::class);
  142. }
  143. /**
  144. * Tests getPluginClass() with a required interface but no implementation.
  145. *
  146. * @covers ::getPluginClass
  147. */
  148. public function testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition() {
  149. $plugin_class = Broccoli::class;
  150. $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)->getMock();
  151. $plugin_definition->expects($this->atLeastOnce())
  152. ->method('getClass')
  153. ->willReturn($plugin_class);
  154. if (method_exists($this, 'expectException')) {
  155. $this->expectException(PluginException::class);
  156. }
  157. else {
  158. $this->setExpectedException(PluginException::class);
  159. }
  160. DefaultFactory::getPluginClass('corn', $plugin_definition, VegetableInterface::class);
  161. }
  162. }