Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest closure actions #283

Closed
thekid opened this issue Mar 17, 2014 · 2 comments
Closed

Unittest closure actions #283

thekid opened this issue Mar 17, 2014 · 2 comments

Comments

@thekid
Copy link
Member

thekid commented Mar 17, 2014

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

@thekid
Copy link
Member Author

thekid commented Mar 17, 2014

Initial implementation exists in the rfc-283 branch

thekid added a commit to xp-framework/core that referenced this issue Aug 14, 2014
@thekid thekid closed this as completed Aug 14, 2014
@thekid
Copy link
Member Author

thekid commented Aug 14, 2014

Part of XP 6, see #172

@thekid thekid added this to the 6.0.0-RELEASE milestone Aug 14, 2014
thekid added a commit to xp-framework/unittest that referenced this issue Aug 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant