Merge pull request #338 from pestphp/skip-closure-support

Closures passed to the `skip` method are now bound to the test case
This commit is contained in:
Luke Downing
2021-07-08 18:05:27 +01:00
committed by GitHub
4 changed files with 51 additions and 5 deletions

View File

@ -144,11 +144,9 @@ final class TestCall
? $conditionOrMessage ? $conditionOrMessage
: $message; : $message;
if ($condition() !== false) { $this->testCaseFactory
$this->testCaseFactory ->chains
->chains ->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
}
return $this; return $this;
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest\Support; namespace Pest\Support;
use Closure;
use ReflectionClass; use ReflectionClass;
use Throwable; use Throwable;
@ -50,6 +51,13 @@ final class HigherOrderMessage
*/ */
public $arguments; public $arguments;
/**
* An optional condition that will determine if the message will be executed.
*
* @var callable(): bool|null
*/
public $condition = null;
/** /**
* Creates a new higher order message. * Creates a new higher order message.
* *
@ -70,6 +78,11 @@ final class HigherOrderMessage
*/ */
public function call(object $target) public function call(object $target)
{ {
/* @phpstan-ignore-next-line */
if (is_callable($this->condition) && call_user_func(Closure::bind($this->condition, $target)) === false) {
return $target;
}
if ($this->hasHigherOrderCallable()) { if ($this->hasHigherOrderCallable()) {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments); return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments);
@ -93,6 +106,18 @@ final class HigherOrderMessage
} }
} }
/**
* Indicates that this message should only be called when the given condition is true.
*
* @param callable(): bool $condition
*/
public function when(callable $condition): self
{
$this->condition = $condition;
return $this;
}
/** /**
* Determines whether or not there exists a higher order callable with the message name. * Determines whether or not there exists a higher order callable with the message name.
* *

View File

@ -24,6 +24,16 @@ final class HigherOrderMessageCollection
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments); $this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments);
} }
/**
* Adds a new higher order message to the collection if the callable condition is does not return false.
*
* @param array<int, mixed> $arguments
*/
public function addWhen(callable $condition, string $filename, int $line, string $methodName, array $arguments): void
{
$this->messages[] = (new HigherOrderMessage($filename, $line, $methodName, $arguments))->when($condition);
}
/** /**
* Proxy all the messages starting from the target. * Proxy all the messages starting from the target.
*/ */

View File

@ -1,5 +1,9 @@
<?php <?php
beforeEach(function () {
$this->shouldSkip = true;
});
it('do not skips') it('do not skips')
->skip(false) ->skip(false)
->assertTrue(true); ->assertTrue(true);
@ -31,3 +35,12 @@ it('skips with condition and message')
it('skips when skip after assertion') it('skips when skip after assertion')
->assertTrue(true) ->assertTrue(true)
->skip(); ->skip();
it('can use something in the test case as a condition')
->skip(function () { return $this->shouldSkip; }, 'This test was skipped')
->assertTrue(false);
it('can user higher order callables and skip')
->skip(function () { return $this->shouldSkip; })
->expect(function () { return $this->shouldSkip; })
->toBeFalse();