DefaultFactoryTest.php in Drupal 9
File
core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php
View source
<?php
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Broccoli;
use Drupal\Tests\Component\Plugin\Fixtures\vegetable\Corn;
use Drupal\Tests\Component\Plugin\Fixtures\vegetable\VegetableInterface;
use PHPUnit\Framework\TestCase;
class DefaultFactoryTest extends TestCase {
public function testGetPluginClassWithValidArrayPluginDefinition() {
$plugin_class = Corn::class;
$class = DefaultFactory::getPluginClass('corn', [
'class' => $plugin_class,
]);
$this
->assertEquals($plugin_class, $class);
}
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);
}
public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
$this
->expectException(PluginException::class);
$this
->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
DefaultFactory::getPluginClass('corn', []);
}
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);
}
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',
]);
}
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);
}
public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
$plugin_class = Corn::class;
$class = DefaultFactory::getPluginClass('corn', [
'class' => $plugin_class,
], VegetableInterface::class);
$this
->assertEquals($plugin_class, $class);
}
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);
}
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);
}
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);
}
}