RulesComponentTest.php in Rules 8.3
File
tests/src/Unit/Integration/Engine/RulesComponentTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Engine;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\RulesComponent;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
class RulesComponentTest extends RulesIntegrationTestBase {
public function testRuleExecutionWithContext() {
$rule = $this->rulesExpressionManager
->createRule();
$rule
->addAction('rules_test_string', ContextConfig::create()
->map('text', 'text'));
$result = RulesComponent::create($rule)
->addContextDefinition('text', ContextDefinition::create('string'))
->provideContext('concatenated')
->setContextValue('text', 'foo')
->execute();
$this
->assertTrue(isset($result['concatenated']) && $result['concatenated'] == 'foofoo');
}
public function testGetExpression() {
$rule = $this->rulesExpressionManager
->createRule();
$this
->assertSame(RulesComponent::create($rule)
->getExpression(), $rule);
}
public function testGetContextDefinitions() {
$rule = $this->rulesExpressionManager
->createRule();
$definition = ContextDefinition::create('string');
$component = RulesComponent::create($rule)
->addContextDefinition('test', $definition);
$this
->assertEquals(array_keys($component
->getContextDefinitions()), [
'test',
]);
$this
->assertSame($component
->getContextDefinitions()['test'], $definition);
}
public function testGetProvidedContext() {
$rule = $this->rulesExpressionManager
->createRule();
$component = RulesComponent::create($rule)
->provideContext('test');
$this
->assertEquals($component
->getProvidedContext(), [
'test',
]);
}
public function testGetState() {
$rule = $this->rulesExpressionManager
->createRule();
$component = RulesComponent::create($rule);
$this
->assertInstanceOf(ExecutionStateInterface::class, $component
->getState());
$component
->addContextDefinition('foo', ContextDefinition::create('string'))
->setContextValue('foo', 'bar');
$this
->assertEquals($component
->getState()
->getVariableValue('foo'), 'bar');
}
}