Skip to content

Commit

Permalink
Merge branch 'master' into method-match
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Sep 24, 2021
2 parents c99f8f1 + 4daf7ee commit f0ddd10
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,16 @@ public function match($subject, array $expressions): Expectation
}

/**
* It skips the tests in the callback if the condition is not truthy.
* Apply the callback if the given "condition" is truthy.
*
* @param Closure|bool|string $condition
* @param (callable(): bool)|bool $condition
* @param callable(Expectation<TValue>): mixed $callback
*/
public function when($condition, callable $callback): Expectation
{
$condition = is_callable($condition)
? $condition
: function () use ($condition) {
: static function () use ($condition): mixed {
return $condition;
};

Expand Down
20 changes: 20 additions & 0 deletions src/PendingObjects/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ public function throws(string $exception, string $exceptionMessage = null): Test
return $this;
}

/**
* Asserts that the test throws the given `$exceptionClass` when called if the given condition is true.
*
* @param (callable(): bool)|bool $condition
*/
public function throwsIf($condition, string $exception, string $exceptionMessage = null): TestCall
{
$condition = is_callable($condition)
? $condition
: static function () use ($condition): mixed {
return $condition;
};

if ($condition()) {
return $this->throws($exception, $exceptionMessage);
}

return $this;
}

/**
* Runs the current test multiple times with
* each item of the given `iterable`.
Expand Down
7 changes: 6 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
✓ it catch exceptions
✓ it catch exceptions and messages
✓ it can just define the message
✓ it not catch exceptions if given condition is false
✓ it catch exceptions if given condition is true
✓ it catch exceptions and messages if given condition is true
✓ it can just define the message if given condition is true
✓ it can just define the message if given condition is 1

PASS Tests\Features\Expect\HigherOrder\methods
✓ it can access methods
Expand Down Expand Up @@ -701,5 +706,5 @@
✓ it is a test
✓ it uses correct parent class

Tests: 4 incompleted, 9 skipped, 463 passed
Tests: 4 incompleted, 9 skipped, 459 passed

20 changes: 20 additions & 0 deletions tests/Features/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@
it('can just define the message', function () {
throw new Exception('Something bad happened');
})->throws('Something bad happened');

it('not catch exceptions if given condition is false', function () {
$this->assertTrue(true);
})->throwsIf(false, Exception::class);

it('catch exceptions if given condition is true', function () {
throw new Exception('Something bad happened');
})->throwsIf(function () { return true; }, Exception::class);

it('catch exceptions and messages if given condition is true', function () {
throw new Exception('Something bad happened');
})->throwsIf(true, Exception::class, 'Something bad happened');

it('can just define the message if given condition is true', function () {
throw new Exception('Something bad happened');
})->throwsIf(true, 'Something bad happened');

it('can just define the message if given condition is 1', function () {
throw new Exception('Something bad happened');
})->throwsIf(1, 'Something bad happened');

0 comments on commit f0ddd10

Please sign in to comment.