DefaultFactoryTest.php in Zircon Profile 8
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\Factory\DefaultFactory;
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;
use Drupal\Tests\UnitTestCase;
class DefaultFactoryTest extends UnitTestCase {
public function testGetPluginClassWithValidArrayPluginDefinition() {
$plugin_class = Cherry::class;
$class = DefaultFactory::getPluginClass('cherry', [
'class' => $plugin_class,
]);
$this
->assertEquals($plugin_class, $class);
}
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);
}
public function testGetPluginClassWithMissingClassWithArrayPluginDefinition() {
DefaultFactory::getPluginClass('cherry', []);
}
public function testGetPluginClassWithMissingClassWithObjectPluginDefinition() {
$plugin_definition = $this
->getMock(PluginDefinitionInterface::class);
DefaultFactory::getPluginClass('cherry', $plugin_definition);
}
public function testGetPluginClassWithNotExistingClassWithArrayPluginDefinition() {
DefaultFactory::getPluginClass('kiwifruit', [
'class' => '\\Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Kiwifruit',
]);
}
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);
}
public function testGetPluginClassWithInterfaceWithArrayPluginDefinition() {
$plugin_class = Cherry::class;
$class = DefaultFactory::getPluginClass('cherry', [
'class' => $plugin_class,
], FruitInterface::class);
$this
->assertEquals($plugin_class, $class);
}
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);
}
public function testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition() {
$plugin_class = Kale::class;
DefaultFactory::getPluginClass('cherry', [
'class' => $plugin_class,
'provider' => 'core',
], FruitInterface::class);
}
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);
}
}