AndExpressionTest.php in Rules 8.3
File
tests/src/Unit/AndExpressionTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Engine\ConditionExpressionInterface;
use Drupal\rules\Plugin\RulesExpression\AndExpression;
use Prophecy\Argument;
class AndExpressionTest extends RulesUnitTestBase {
protected $and;
protected function setUp() : void {
parent::setUp();
$this->and = new AndExpression([], '', [
'label' => 'Condition set (AND)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
public function testOneCondition() {
$this->trueConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->and
->addExpressionObject($this->trueConditionExpression
->reveal());
$this
->assertTrue($this->and
->execute(), 'Single condition returns TRUE.');
}
public function testEmptyAnd() {
$property = new \ReflectionProperty($this->and, 'conditions');
$property
->setAccessible(TRUE);
$this
->assertEmpty($property
->getValue($this->and));
$this
->assertFalse($this->and
->execute(), 'Empty AND returns FALSE.');
}
public function testTwoConditions() {
$this->trueConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$second_condition = $this
->prophesize(ConditionExpressionInterface::class);
$second_condition
->getUuid()
->willReturn('true_uuid2');
$second_condition
->getWeight()
->willReturn(0);
$second_condition
->executeWithState(Argument::type(ExecutionStateInterface::class))
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$this->and
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($second_condition
->reveal());
$this
->assertTrue($this->and
->execute(), 'Two conditions returns TRUE.');
}
public function testTwoFalseConditions() {
$this->falseConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$second_condition = $this
->prophesize(ConditionExpressionInterface::class);
$second_condition
->getUuid()
->willReturn('false_uuid2');
$second_condition
->getWeight()
->willReturn(0);
$second_condition
->executeWithState(Argument::type(ExecutionStateInterface::class))
->willReturn(FALSE)
->shouldNotBeCalled();
$this->and
->addExpressionObject($this->falseConditionExpression
->reveal())
->addExpressionObject($second_condition
->reveal());
$this
->assertFalse($this->and
->execute(), 'Two false conditions return FALSE.');
}
public function testEvaluationOrder() {
$this->falseConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->falseConditionExpression
->getWeight()
->willReturn(1);
$second_condition = $this
->prophesize(ConditionExpressionInterface::class);
$second_condition
->getUuid()
->willReturn('true_uuid2');
$second_condition
->getWeight()
->willReturn(0);
$second_condition
->executeWithState(Argument::type(ExecutionStateInterface::class))
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$this->and
->addExpressionObject($this->falseConditionExpression
->reveal())
->addExpressionObject($second_condition
->reveal());
$this
->assertFalse($this->and
->execute(), 'Correct execution order of conditions.');
}
}