Merge branch 'master' into performs_no_expectations

# Conflicts:
#	src/PendingCalls/TestCall.php
This commit is contained in:
Luke Downing
2022-03-22 13:20:31 +00:00
33 changed files with 456 additions and 47 deletions

View File

@ -5,6 +5,10 @@ declare(strict_types=1);
namespace Pest\PendingCalls;
use Closure;
use InvalidArgumentException;
use Pest\Factories\Covers\CoversClass;
use Pest\Factories\Covers\CoversFunction;
use Pest\Factories\Covers\CoversNothing;
use Pest\Factories\TestCaseMethodFactory;
use Pest\Support\Backtrace;
use Pest\Support\HigherOrderCallables;
@ -45,9 +49,11 @@ final class TestCall
/**
* Asserts that the test throws the given `$exceptionClass` when called.
*/
public function throws(string $exception, string $exceptionMessage = null): TestCall
public function throws(string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall
{
if (class_exists($exception)) {
if (is_int($exception)) {
$exceptionCode = $exception;
} elseif (class_exists($exception)) {
$this->testCaseMethod
->proxies
->add(Backtrace::file(), Backtrace::line(), 'expectException', [$exception]);
@ -61,6 +67,12 @@ final class TestCall
->add(Backtrace::file(), Backtrace::line(), 'expectExceptionMessage', [$exceptionMessage]);
}
if (is_int($exceptionCode)) {
$this->testCaseMethod
->proxies
->add(Backtrace::file(), Backtrace::line(), 'expectExceptionCode', [$exceptionCode]);
}
return $this;
}
@ -69,7 +81,7 @@ final class TestCall
*
* @param (callable(): bool)|bool $condition
*/
public function throwsIf(callable|bool $condition, string $exception, string $exceptionMessage = null): TestCall
public function throwsIf(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): TestCall
{
$condition = is_callable($condition)
? $condition
@ -78,7 +90,7 @@ final class TestCall
};
if ($condition()) {
return $this->throws($exception, $exceptionMessage);
return $this->throws($exception, $exceptionMessage, $exceptionCode);
}
return $this;
@ -160,6 +172,63 @@ final class TestCall
return $this;
}
/**
* Sets the covered classes or methods.
*/
public function covers(string ...$classesOrFunctions): TestCall
{
foreach ($classesOrFunctions as $classOrFunction) {
$isClass = class_exists($classOrFunction);
$isMethod = function_exists($classOrFunction);
if (!$isClass && !$isMethod) {
throw new InvalidArgumentException(sprintf('No class or method named "%s" has been found.', $classOrFunction));
}
if ($isClass) {
$this->coversClass($classOrFunction);
} else {
$this->coversFunction($classOrFunction);
}
}
return $this;
}
/**
* Sets the covered classes.
*/
public function coversClass(string ...$classes): TestCall
{
foreach ($classes as $class) {
$this->testCaseMethod->covers[] = new CoversClass($class);
}
return $this;
}
/**
* Sets the covered functions.
*/
public function coversFunction(string ...$functions): TestCall
{
foreach ($functions as $function) {
$this->testCaseMethod->covers[] = new CoversFunction($function);
}
return $this;
}
/**
* Sets that the current test covers nothing.
*/
public function coversNothing(): TestCall
{
$this->testCaseMethod->covers = [new CoversNothing()];
return $this;
}
/**
* Informs the test runner that no expectations happen in this test,
* and its purpose is simply to check whether the given code can