You are here

class RulesConditionContainerTest in Rules 8.3

@coversDefaultClass \Drupal\rules\Engine\ConditionExpressionContainer @group Rules

Hierarchy

Expanded class hierarchy of RulesConditionContainerTest

File

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

Namespace

Drupal\Tests\rules\Unit
View source
class RulesConditionContainerTest extends RulesUnitTestBase {

  /**
   * Creates a mocked condition container.
   *
   * @param array $methods
   *   The methods to mock.
   * @param string $class
   *   The name of the created mock class.
   *
   * @return \Drupal\rules\Engine\ConditionExpressionContainerInterface
   *   The mocked condition container.
   */
  protected function getMockConditionContainer(array $methods = [], $class = 'RulesConditionContainerMock') {
    return $this
      ->getMockForAbstractClass(ConditionExpressionContainer::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], $class, TRUE, TRUE, TRUE, $methods);
  }

  /**
   * Tests adding conditions to the condition container.
   *
   * @covers ::addExpressionObject
   */
  public function testAddExpressionObject() {
    $container = $this
      ->getMockConditionContainer();
    $container
      ->addExpressionObject($this->trueConditionExpression
      ->reveal());
    $property = new \ReflectionProperty($container, 'conditions');
    $property
      ->setAccessible(TRUE);
    $this
      ->assertEquals([
      $this->trueConditionExpression
        ->reveal(),
    ], array_values($property
      ->getValue($container)));
  }

  /**
   * Tests negating the result of the condition container.
   *
   * @covers ::negate
   * @covers ::isNegated
   */
  public function testNegate() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [], '', FALSE);
    $this
      ->assertFalse($container
      ->isNegated());
    $this
      ->assertTrue($container
      ->execute());
    $container
      ->negate(TRUE);
    $this
      ->assertTrue($container
      ->isNegated());
    $this
      ->assertFalse($container
      ->execute());
  }

  /**
   * Tests executing the condition container.
   *
   * @covers ::execute
   */
  public function testExecute() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [], '', FALSE);
    $this
      ->assertTrue($container
      ->execute());
  }

  /**
   * Tests that an expression can be retrieved by UUID.
   */
  public function testLookupExpression() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $container
      ->addExpressionObject($this->trueConditionExpression
      ->reveal());
    $uuid = $this->trueConditionExpression
      ->reveal()
      ->getUuid();
    $this
      ->assertSame($this->trueConditionExpression
      ->reveal(), $container
      ->getExpression($uuid));
    $this
      ->assertFalse($container
      ->getExpression('invalid UUID'));
  }

  /**
   * Tests that a nested expression can be retrieved by UUID.
   */
  public function testLookupNestedExpression() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $container
      ->addExpressionObject($this->trueConditionExpression
      ->reveal());
    $nested_container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $nested_container
      ->addExpressionObject($this->falseConditionExpression
      ->reveal());
    $container
      ->addExpressionObject($nested_container);
    $uuid = $this->falseConditionExpression
      ->reveal()
      ->getUuid();
    $this
      ->assertSame($this->falseConditionExpression
      ->reveal(), $container
      ->getExpression($uuid));
  }

  /**
   * Tests deleting a condition from the container.
   */
  public function testDeletingCondition() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $container
      ->addExpressionObject($this->trueConditionExpression
      ->reveal());
    $container
      ->addExpressionObject($this->falseConditionExpression
      ->reveal());

    // Delete the first condition.
    $uuid = $this->trueConditionExpression
      ->reveal()
      ->getUuid();
    $this
      ->assertTrue($container
      ->deleteExpression($uuid));
    foreach ($container as $condition) {
      $this
        ->assertSame($this->falseConditionExpression
        ->reveal(), $condition);
    }
    $this
      ->assertFalse($container
      ->deleteExpression('invalid UUID'));
  }

  /**
   * Tests deleting a nested condition from the container.
   */
  public function testDeletingNestedCondition() {
    $container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $container
      ->addExpressionObject($this->trueConditionExpression
      ->reveal());
    $nested_container = $this
      ->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
      [],
      'test_id',
      [],
      $this
        ->prophesize(ExpressionManagerInterface::class)
        ->reveal(),
      $this
        ->prophesize(LoggerChannelInterface::class)
        ->reveal(),
    ], '', TRUE);
    $nested_container
      ->addExpressionObject($this->falseConditionExpression
      ->reveal());
    $container
      ->addExpressionObject($nested_container);
    $uuid = $this->falseConditionExpression
      ->reveal()
      ->getUuid();
    $this
      ->assertTrue($container
      ->deleteExpression($uuid));
    $this
      ->assertEquals(0, count($nested_container
      ->getIterator()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RulesConditionContainerTest::getMockConditionContainer protected function Creates a mocked condition container.
RulesConditionContainerTest::testAddExpressionObject public function Tests adding conditions to the condition container.
RulesConditionContainerTest::testDeletingCondition public function Tests deleting a condition from the container.
RulesConditionContainerTest::testDeletingNestedCondition public function Tests deleting a nested condition from the container.
RulesConditionContainerTest::testExecute public function Tests executing the condition container.
RulesConditionContainerTest::testLookupExpression public function Tests that an expression can be retrieved by UUID.
RulesConditionContainerTest::testLookupNestedExpression public function Tests that a nested expression can be retrieved by UUID.
RulesConditionContainerTest::testNegate public function Tests negating the result of the condition container.
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.
RulesUnitTestBase::setUp protected function Overrides UnitTestCase::setUp 4
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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.