Skip to content

Unittest closure actions #283

Closed
Closed
@thekid

Description

@thekid

Scope of Change

A new abstract base class will be added to enable inline test actions with a simple before-test verification.

Rationale

This will remove the necessity to introduce classes for really simple verify-and-it-not-ok-skip scenarios.

Functionality

Example:

class Verification extends \lang\Object implements \unittest\TestAction {
  public function verify() {
     ...
  }

  public function beforeTest(TestCase $t) {
    if (!$this->verify()) {
      throw new PrerequisitesNotMetError('Skip, verification failed');
    }
  }
}

class MyTest extends \unittest\TestCase {
  #[@test, @action(new Verification())]
  function verify_something_works() {
    $this->assertEquals(...);
  }
}

New functionality

class MyTest extends \unittest\TestCase {
  #[@test, @action(new \unittest\actions\VerifyThat(function() {
  #   return ...
  #}))]
  function verify_something_works() {
    $this->assertEquals(...);
  }
}

The argument passed to the VerifyThat classes' constructor can be one of the following:

  • A closure, as seen above - runs in the test class' context and has full access to $this
  • An instance method, e.g. 'method' - runs in the test class' context and has full access to $this
  • A class method, e.g. 'self::method' - runs in the test class' context and has full access to self
  • A foreign class' static method, e.g. 'com.example.Coin::values'.

The callable will be invoked without arguments and if it returns FALSE, a PrerequisitesNotMetError will be thrown an the corresponding test(s) will be skipped.

Security considerations

Speed impact

Dependencies

The function keyword needs to be supported inside the Class Parser. Currently this raises:

Caused by Exception lang.IllegalStateException (Parse error: Unexpected T_FUNCTION)
  at lang.reflect.ClassParser::valueOf(array[32], 17, (0x13)'unittest.TestCase�5', array[0]) [line 159 of ClassParser.class.php]
  ... 17 more

See xp-framework/core#7

Related documents

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions