ContextHandlerTraitTest.php in Rules 8.3
File
tests/src/Unit/ContextHandlerTraitTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinitionInterface;
use Drupal\rules\Context\ContextHandlerTrait;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Exception\EvaluationException;
class ContextHandlerTraitTest extends RulesUnitTestBase {
public function testMissingContext() {
$this
->expectException(EvaluationException::class);
$this
->expectExceptionMessage("Required context 'test' is missing for plugin 'testplugin'");
$trait = $this
->getMockForTrait(ContextHandlerTrait::class, [], '', TRUE, TRUE, TRUE, [
'getContextValue',
]);
$context_definition = $this
->prophesize(ContextDefinitionInterface::class);
$trait->configuration = ContextConfig::create()
->toArray();
$context_definition
->isRequired()
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$plugin = $this
->prophesize(ContextAwarePluginInterface::class);
$plugin
->getContextDefinitions()
->willReturn([
'test' => $context_definition
->reveal(),
])
->shouldBeCalled(1);
$plugin
->getContextValue('test')
->willReturn(NULL)
->shouldBeCalled(1);
$plugin
->getPluginId()
->willReturn('testplugin')
->shouldBeCalledTimes(1);
$state = $this
->prophesize(ExecutionStateInterface::class);
$reflection = new \ReflectionClass($trait);
$method = $reflection
->getMethod('prepareContext');
$method
->setAccessible(TRUE);
$method
->invokeArgs($trait, [
$plugin
->reveal(),
$state
->reveal(),
]);
}
}