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

Closures passed to the skip method are now bound to the test case #338

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/PendingObjects/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ public function skip($conditionOrMessage = true, string $message = ''): TestCall
? $conditionOrMessage
: $message;

if ($condition() !== false) {
$this->testCaseFactory
->chains
->add(Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);
}
$this->testCaseFactory
->chains
->addWhen($condition, Backtrace::file(), Backtrace::line(), 'markTestSkipped', [$message]);

return $this;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Support/HigherOrderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Pest\Support;

use Closure;
use ReflectionClass;
use Throwable;

Expand Down Expand Up @@ -50,6 +51,13 @@ final class HigherOrderMessage
*/
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.
*
Expand All @@ -70,6 +78,11 @@ public function __construct(string $filename, int $line, string $methodName, arr
*/
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()) {
/* @phpstan-ignore-next-line */
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments);
Expand All @@ -93,6 +106,18 @@ public function call(object $target)
}
}

/**
* Indicates that this message should only be called when the given condition is true.
*
* @param callable(): bool $condition
*/
public function when(callable $condition): self
lukeraymonddowning marked this conversation as resolved.
Show resolved Hide resolved
{
$this->condition = $condition;

return $this;
}

/**
* Determines whether or not there exists a higher order callable with the message name.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Support/HigherOrderMessageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public function add(string $filename, int $line, string $methodName, array $argu
$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.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Features/Skip.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

beforeEach(function () {
$this->shouldSkip = true;
});

it('do not skips')
->skip(false)
->assertTrue(true);
Expand Down Expand Up @@ -31,3 +35,12 @@
it('skips when skip after assertion')
->assertTrue(true)
->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();