You are here

class RulesConditionalWhileTestCase in Conditional Rules 8

Same name and namespace in other branches
  1. 7 tests/rules_conditional.test \RulesConditionalWhileTestCase

While tests.

Hierarchy

Expanded class hierarchy of RulesConditionalWhileTestCase

File

tests/rules_conditional.test, line 480
SimpleTest testing suites.

View source
class RulesConditionalWhileTestCase extends RulesConditionalBaseTestCase {

  /**
   * Returns test info.
   */
  public static function getInfo() {
    return array(
      'name' => 'While loop',
      'description' => 'Test the while loop plugin.',
      'group' => 'Conditional Rules',
    );
  }

  /**
   * Sets up test case.
   */
  protected function setUp() {
    parent::setUp('rules_conditional_test');
  }

  /**
   * Tests evaluation.
   */
  public function testEvaluate() {

    // Cap iteration limit to prevent long-running tests.
    variable_set('rules_conditional_max_iterations', 100);
    $comp = $this
      ->createTestComponent();
    $this
      ->assert($this
      ->doTestEvaluate($comp), 'While loop correctly evaluates.');

    // Constrain iteration limit and test again.
    variable_set('rules_conditional_max_iterations', 5);
    $this
      ->assert($this
      ->doTestEvaluate($comp, 5), 'While loop correctly evaluates with limited number of iterations.');
  }

  /**
   * Tests exporting.
   */
  public function testExport() {
    $comp = $this
      ->createTestComponent();
    $comp->name = 'while_test';
    $export = $this
      ->getFileContents('while_test_export.txt');
    $this
      ->assertEqual($export, $comp
      ->export(), 'While loop is correctly exported.');
  }

  /**
   * Tests importing.
   */
  public function testImport() {

    // Cap iteration limit to prevent long-running tests.
    variable_set('rules_conditional_max_iterations', 100);
    $export = $this
      ->getFileContents('while_test_export.txt');
    $comp = entity_import('rules_config', $export);
    $this
      ->assert($this
      ->doTestEvaluate($comp), 'Imported while loop correctly evaluates.');
  }

  /**
   * Creates an action set to test a conditional.
   */
  protected function createTestComponent() {
    return rules_action_set(array())
      ->action('variable_add', array(
      'type' => 'integer',
      'value' => 0,
      'variable_added:var' => 'count',
      'variable_added:label' => 'Count',
    ))
      ->action(rules_conditional_while('data_is', array(
      'data:select' => 'count',
      'op' => '>',
      'value' => 10,
    ))
      ->negate()
      ->action('data_set', array(
      'data:select' => 'count',
      'value:select' => 'count',
      'value:process' => array(
        'num_offset' => array(
          'value' => 1,
        ),
      ),
    )))
      ->action('rules_conditional_test_throw', array(
      'message:select' => 'count',
    ));
  }

  /**
   * Tests evaluating a conditional.
   */
  public function doTestEvaluate($comp, $expectedCount = 11) {
    return $this
      ->assertExecution($expectedCount, $comp);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesConditionalBaseTestCase::assertExecution protected function Asserts a test message from the execution of a rule configuration.
RulesConditionalBaseTestCase::getFileContents protected function Retrieves a file in the test directory.
RulesConditionalWhileTestCase::createTestComponent protected function Creates an action set to test a conditional.
RulesConditionalWhileTestCase::doTestEvaluate public function Tests evaluating a conditional.
RulesConditionalWhileTestCase::getInfo public static function Returns test info.
RulesConditionalWhileTestCase::setUp protected function Sets up test case.
RulesConditionalWhileTestCase::testEvaluate public function Tests evaluation.
RulesConditionalWhileTestCase::testExport public function Tests exporting.
RulesConditionalWhileTestCase::testImport public function Tests importing.