class AndExpressionTest

@coversDefaultClass \Drupal\rules\Plugin\RulesExpression\AndExpression @group Rules

Hierarchy

Expanded class hierarchy of AndExpressionTest

File

tests/src/Unit/AndExpressionTest.php, line 14

Namespace

Drupal\Tests\rules\Unit
View 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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
AndExpressionTest::$and protected property The 'and' condition container being tested.
AndExpressionTest::setUp protected function Overrides RulesUnitTestBase::setUp
AndExpressionTest::testEmptyAnd public function Tests an empty AND.
AndExpressionTest::testEvaluationOrder public function Tests evaluation order with two conditions.
AndExpressionTest::testOneCondition public function Tests one condition.
AndExpressionTest::testTwoConditions public function Tests two true conditions.
AndExpressionTest::testTwoFalseConditions public function Tests two false conditions.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RulesUnitTestBase::$expressionManager protected property The mocked expression manager object. 1
RulesUnitTestBase::$falseConditionExpression protected property A mocked condition that always evaluates to FALSE.
RulesUnitTestBase::$rulesDebugLogger protected property The mocked expression manager object.
RulesUnitTestBase::$testActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$testFirstActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$trueConditionExpression protected property A mocked condition that always evaluates to TRUE.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function