mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Closures passed to the skip method are now bound to the test case to allow for more complex logic.
This commit is contained in:
@ -143,11 +143,9 @@ final class TestCall
|
|||||||
? $conditionOrMessage
|
? $conditionOrMessage
|
||||||
: $message;
|
: $message;
|
||||||
|
|
||||||
if ($condition() !== false) {
|
|
||||||
$this->testCaseFactory
|
$this->testCaseFactory
|
||||||
->chains
|
->chains
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -81,7 +81,6 @@ final class Coverage implements AddsOutput, HandlesArguments
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($input->getOption(self::MIN_OPTION) !== null) {
|
if ($input->getOption(self::MIN_OPTION) !== null) {
|
||||||
/* @phpstan-ignore-next-line */
|
|
||||||
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
$this->coverageMin = (float) $input->getOption(self::MIN_OPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Support;
|
namespace Pest\Support;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use const PHP_MAJOR_VERSION;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -50,6 +52,13 @@ final class HigherOrderMessage
|
|||||||
*/
|
*/
|
||||||
public $arguments;
|
public $arguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An optional condition that will determine if the message will be executed.
|
||||||
|
*
|
||||||
|
* @var callable|null
|
||||||
|
*/
|
||||||
|
public $condition = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new higher order message.
|
* Creates a new higher order message.
|
||||||
*
|
*
|
||||||
@ -70,6 +79,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;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Reflection::call($target, $this->methodName, $this->arguments);
|
return Reflection::call($target, $this->methodName, $this->arguments);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
@ -88,9 +102,16 @@ final class HigherOrderMessage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function when(callable $condition): self
|
||||||
|
{
|
||||||
|
$this->condition = $condition;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
private static function getUndefinedMethodMessage(object $target, string $methodName): string
|
||||||
{
|
{
|
||||||
if (\PHP_MAJOR_VERSION >= 8) {
|
if (PHP_MAJOR_VERSION >= 8) {
|
||||||
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
|
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -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,7 @@ 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user