View source
<?php
namespace Drupal\Tests\plugin\Unit\ParamConverter;
use Drupal\plugin\ParamConverter\PluginTypeConverter;
use Drupal\plugin\PluginType\PluginTypeInterface;
use Drupal\plugin\PluginType\PluginTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class PluginTypeConverterTest extends UnitTestCase {
protected $pluginTypeManager;
protected $sut;
protected function setUp() : void {
parent::setUp();
$this->pluginTypeManager = $this
->prophesize(PluginTypeManagerInterface::class);
$this->sut = new PluginTypeConverter($this->pluginTypeManager
->reveal());
}
public function testApplies($expected, $definition) {
$name = 'foo_bar';
$route = $this
->prophesize(Route::class);
$this
->assertSame($expected, $this->sut
->applies($definition, $name, $route
->reveal()));
}
public function provideApplies() {
$data = [];
$data['applies-because-implicitly-enabled'] = [
TRUE,
[
'plugin.plugin_type' => [],
],
];
$data['applies-because-explicitly-enabled'] = [
TRUE,
[
'plugin.plugin_type' => [
'enabled' => TRUE,
],
],
];
$data['applies-not-because-disabled'] = [
FALSE,
[
'plugin.plugin_type' => [
'enabled' => FALSE,
],
],
];
$data['applies-not-because-non-existent'] = [
FALSE,
[],
];
return $data;
}
public function testConvertWithExceptionReturnsNull() {
$plugin_type_id = 'foo_bar.baz';
$definition = [
'plugin.plugin_type' => [],
];
$name = 'foo_bar';
$defaults = [];
$this->pluginTypeManager
->hasPluginType($plugin_type_id)
->willReturn(FALSE);
$original_error_reporting = error_reporting();
error_reporting($original_error_reporting & ~E_USER_WARNING);
$this
->assertNull($this->sut
->convert($plugin_type_id, $definition, $name, $defaults));
error_reporting($original_error_reporting);
}
public function testConvertWithKnownPluginType() {
$plugin_type_id = 'foo_bar.baz';
$definition = [
'plugin.plugin_type' => [],
];
$name = 'foo_bar';
$defaults = [];
$plugin_type = $this
->prophesize(PluginTypeInterface::class);
$this->pluginTypeManager
->hasPluginType($plugin_type_id)
->willReturn(TRUE);
$this->pluginTypeManager
->getPluginType($plugin_type_id)
->willReturn($plugin_type
->reveal());
$this
->assertSame($plugin_type
->reveal(), $this->sut
->convert($plugin_type_id, $definition, $name, $defaults));
}
public function testConvertWithUnknownPluginType() {
$plugin_type_id = 'foo_bar.baz';
$definition = [
'plugin.plugin_type' => [],
];
$name = 'foo_bar';
$defaults = [];
$this->pluginTypeManager
->hasPluginType($plugin_type_id)
->willReturn(FALSE);
$this
->assertNull($this->sut
->convert($plugin_type_id, $definition, $name, $defaults));
}
public function testConvertWithInvalidDefinition() {
$definition = [];
$plugin_type_id = 'foozaar.bazaar';
$name = 'foo_bar';
$defaults = [];
$this
->assertNull($this->sut
->convert($plugin_type_id, $definition, $name, $defaults));
}
}