You are here

class ActionSetExpressionTest in Rules 8.3

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

Hierarchy

Expanded class hierarchy of ActionSetExpressionTest

File

tests/src/Unit/ActionSetExpressionTest.php, line 15

Namespace

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

  /**
   * The action set being tested.
   *
   * @var \Drupal\rules\Plugin\RulesExpression\ActionSetExpression
   */
  protected $actionSet;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();

    // TestActionSetExpression is defined below.
    $this->actionSet = new TestActionSetExpression([], '', [], $this->expressionManager
      ->reveal(), $this->rulesDebugLogger
      ->reveal());
  }

  /**
   * Tests that an action in the set fires.
   */
  public function testActionExecution() {

    // The execute method on the test action must be called once.
    $this->testActionExpression
      ->executeWithState(Argument::type(ExecutionStateInterface::class))
      ->shouldBeCalledTimes(1);
    $this->actionSet
      ->addExpressionObject($this->testActionExpression
      ->reveal())
      ->execute();
  }

  /**
   * Tests that two actions in the set fire both.
   */
  public function testTwoActionExecution() {

    // The execute method on the test action must be called once.
    $this->testActionExpression
      ->executeWithState(Argument::type(ExecutionStateInterface::class))
      ->shouldBeCalledTimes(1);

    // The execute method on the second action must be called once.
    $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();
  }

  /**
   * Tests that nested action sets work.
   */
  public function testNestedActionExecution() {

    // The execute method on the test action must be called twice.
    $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();
  }

  /**
   * Tests that a nested action can be retrieved by UUID.
   */
  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'));
  }

  /**
   * Tests deleting an action from the container.
   */
  public function testDeletingAction() {
    $this->actionSet
      ->addExpressionObject($this->testActionExpression
      ->reveal());
    $second_action = $this
      ->prophesize(ActionExpression::class);
    $this->actionSet
      ->addExpressionObject($second_action
      ->reveal());

    // Get the UUID of the first action added.
    $uuid = $this->testActionExpression
      ->reveal()
      ->getUuid();
    $this
      ->assertTrue($this->actionSet
      ->deleteExpression($uuid));

    // Now only the second action remains.
    foreach ($this->actionSet as $action) {
      $this
        ->assertSame($second_action
        ->reveal(), $action);
    }
  }

  /**
   * Tests evaluation order with two actions.
   */
  public function testEvaluationOrder() {

    // The execute method on the second action must be called once.
    $this->testActionExpression
      ->executeWithState(Argument::type(ExecutionStateInterface::class))
      ->shouldBeCalledTimes(1);

    // The execute method on the test action must be called once.
    $this->testFirstActionExpression
      ->executeWithState(Argument::type(ExecutionStateInterface::class))
      ->shouldBeCalledTimes(1);

    // The 'first' action should be called first, because of weight,
    // even though it is added second.
    $this->actionSet
      ->addExpressionObject($this->testActionExpression
      ->reveal())
      ->addExpressionObject($this->testFirstActionExpression
      ->reveal());

    // The $result variable is a test-only variable to hold the return value
    // of test actions, which normally don't return a value. We do this so we
    // can verify the order of execution.
    $this
      ->assertEquals([
      'action_uuid0',
      'action_uuid1',
    ], $this->actionSet
      ->execute());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionSetExpressionTest::$actionSet protected property The action set being tested.
ActionSetExpressionTest::setUp protected function Overrides RulesUnitTestBase::setUp
ActionSetExpressionTest::testActionExecution public function Tests that an action in the set fires.
ActionSetExpressionTest::testDeletingAction public function Tests deleting an action from the container.
ActionSetExpressionTest::testEvaluationOrder public function Tests evaluation order with two actions.
ActionSetExpressionTest::testLookupAction public function Tests that a nested action can be retrieved by UUID.
ActionSetExpressionTest::testNestedActionExecution public function Tests that nested action sets work.
ActionSetExpressionTest::testTwoActionExecution public function Tests that two actions in the set fire both.
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.
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 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.