ActionSetExpressionTest.php in Rules 8.3
File
tests/src/Unit/ActionSetExpressionTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Engine\ActionExpressionInterface;
use Drupal\rules\Plugin\RulesExpression\ActionSetExpression;
use Drupal\rules\Plugin\RulesExpression\ActionExpression;
use Prophecy\Argument;
class ActionSetExpressionTest extends RulesUnitTestBase {
protected $actionSet;
protected function setUp() : void {
parent::setUp();
$this->actionSet = new TestActionSetExpression([], '', [], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
public function testActionExecution() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal())
->execute();
}
public function testTwoActionExecution() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$second_action = $this
->prophesize(ActionExpressionInterface::class);
$second_action
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$second_action
->getUuid()
->willReturn('uuid2');
$second_action
->getWeight()
->willReturn(0);
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal())
->addExpressionObject($second_action
->reveal())
->execute();
}
public function testNestedActionExecution() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(2);
$inner = new ActionSetExpression([], '', [], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$inner
->addExpressionObject($this->testActionExpression
->reveal());
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal())
->addExpressionObject($inner)
->execute();
}
public function testLookupAction() {
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal());
$uuid = $this->testActionExpression
->reveal()
->getUuid();
$lookup_action = $this->actionSet
->getExpression($uuid);
$this
->assertSame($this->testActionExpression
->reveal(), $lookup_action);
$this
->assertFalse($this->actionSet
->getExpression('invalid UUID'));
}
public function testDeletingAction() {
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal());
$second_action = $this
->prophesize(ActionExpression::class);
$this->actionSet
->addExpressionObject($second_action
->reveal());
$uuid = $this->testActionExpression
->reveal()
->getUuid();
$this
->assertTrue($this->actionSet
->deleteExpression($uuid));
foreach ($this->actionSet as $action) {
$this
->assertSame($second_action
->reveal(), $action);
}
}
public function testEvaluationOrder() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->testFirstActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->actionSet
->addExpressionObject($this->testActionExpression
->reveal())
->addExpressionObject($this->testFirstActionExpression
->reveal());
$this
->assertEquals([
'action_uuid0',
'action_uuid1',
], $this->actionSet
->execute());
}
}
class TestActionSetExpression extends ActionSetExpression {
public function executeWithState(ExecutionStateInterface $state) {
$uuids = [];
foreach ($this as $action) {
$action
->executeWithState($state);
$uuids[] = $action
->getUuid();
}
return $uuids;
}
}