ContextAwarePluginBaseTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Plugin\Context;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\KernelTests\KernelTestBase;
class ContextAwarePluginBaseTest extends KernelTestBase {
private $plugin;
public function setUp() : void {
parent::setUp();
$configuration = [
'context' => [
'nato_letter' => 'Alpha',
],
];
$plugin_definition = new TestPluginDefinition();
$plugin_definition
->addContextDefinition('nato_letter', ContextDefinition::create('string'));
$this->plugin = $this
->getMockBuilder(ContextAwarePluginBase::class)
->setConstructorArgs([
$configuration,
'the_sisko',
$plugin_definition,
])
->setMethods([
'setContext',
])
->getMockForAbstractClass();
}
public function testGetContextDefinitions() {
$this
->assertIsArray($this->plugin
->getContextDefinitions());
}
public function testGetContextDefinition() {
$this
->expectException(ContextException::class);
$this
->expectExceptionMessage('The person context is not a valid context.');
$this->plugin
->getContextDefinition('person');
}
public function testGetContextValue() {
$this
->assertSame('Alpha', $this->plugin
->getContextValue('nato_letter'));
}
public function testSetContextValue() {
$typed_data_manager = $this
->prophesize(TypedDataManagerInterface::class);
$container = new ContainerBuilder();
$container
->set('typed_data_manager', $typed_data_manager
->reveal());
\Drupal::setContainer($container);
$this->plugin
->getPluginDefinition()
->addContextDefinition('foo', new ContextDefinition('string'));
$this->plugin
->expects($this
->exactly(1))
->method('setContext');
$this->plugin
->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
}
}
class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
use ContextAwarePluginDefinitionTrait;
}