class AndExpressionTest in Rules 8.3
@coversDefaultClass \Drupal\rules\Plugin\RulesExpression\AndExpression @group Rules
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\rules\Unit\RulesUnitTestBase
- class \Drupal\Tests\rules\Unit\AndExpressionTest
- class \Drupal\Tests\rules\Unit\RulesUnitTestBase
Expanded class hierarchy of AndExpressionTest
File
- tests/
src/ Unit/ AndExpressionTest.php, line 14
Namespace
Drupal\Tests\rules\UnitView source
class AndExpressionTest extends RulesUnitTestBase {
/**
* The 'and' condition container being tested.
*
* @var \Drupal\rules\Engine\ConditionExpressionContainerInterface
*/
protected $and;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->and = new AndExpression([], '', [
'label' => 'Condition set (AND)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
/**
* Tests one condition.
*/
public function testOneCondition() {
// The method on the test condition must be called once.
$this->trueConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->and
->addExpressionObject($this->trueConditionExpression
->reveal());
$this
->assertTrue($this->and
->execute(), 'Single condition returns TRUE.');
}
/**
* Tests an empty AND.
*/
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.');
}
/**
* Tests two true conditions.
*/
public function testTwoConditions() {
// The method on the test condition must be called once.
$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.');
}
/**
* Tests two false conditions.
*/
public function testTwoFalseConditions() {
// The method on the test condition must be called once.
$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);
// Evaluation of an AND condition group should stop with first FALSE.
// The second condition should not be evaluated.
$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.');
}
/**
* Tests evaluation order with two conditions.
*/
public function testEvaluationOrder() {
// The method on the false test condition must be called once.
$this->falseConditionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
// Set weight to 1 so it will be evaluated second.
$this->falseConditionExpression
->getWeight()
->willReturn(1);
$second_condition = $this
->prophesize(ConditionExpressionInterface::class);
$second_condition
->getUuid()
->willReturn('true_uuid2');
$second_condition
->getWeight()
->willReturn(0);
// If the above false condition is evaluated first, the second condition
// will not be called. If the evaluation order is correct, then it should
// be called exactly once.
$second_condition
->executeWithState(Argument::type(ExecutionStateInterface::class))
->willReturn(TRUE)
->shouldBeCalledTimes(1);
// Second condition should be called first, because of weight.
$this->and
->addExpressionObject($this->falseConditionExpression
->reveal())
->addExpressionObject($second_condition
->reveal());
$this
->assertFalse($this->and
->execute(), 'Correct execution order of conditions.');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AndExpressionTest:: |
protected | property | The 'and' condition container being tested. | |
AndExpressionTest:: |
protected | function |
Overrides RulesUnitTestBase:: |
|
AndExpressionTest:: |
public | function | Tests an empty AND. | |
AndExpressionTest:: |
public | function | Tests evaluation order with two conditions. | |
AndExpressionTest:: |
public | function | Tests one condition. | |
AndExpressionTest:: |
public | function | Tests two true conditions. | |
AndExpressionTest:: |
public | function | Tests two false conditions. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
RulesUnitTestBase:: |
protected | property | The mocked expression manager object. | 1 |
RulesUnitTestBase:: |
protected | property | A mocked condition that always evaluates to FALSE. | |
RulesUnitTestBase:: |
protected | property | The mocked expression manager object. | |
RulesUnitTestBase:: |
protected | property | A mocked dummy action object. | |
RulesUnitTestBase:: |
protected | property | A mocked dummy action object. | |
RulesUnitTestBase:: |
protected | property | A mocked condition that always evaluates to TRUE. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |