Merge pull request #300 from pestphp/feat-function_exists

Wrap functions in function_exists
This commit is contained in:
Nuno Maduro
2021-05-23 21:47:25 +01:00
committed by GitHub

View File

@ -12,56 +12,65 @@ use Pest\Support\HigherOrderTapProxy;
use Pest\TestSuite; use Pest\TestSuite;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** if (!function_exists('beforeAll')) {
/**
* Runs the given closure before all tests in the current file. * Runs the given closure before all tests in the current file.
*/ */
function beforeAll(Closure $closure): void function beforeAll(Closure $closure): void
{ {
TestSuite::getInstance()->beforeAll->set($closure); TestSuite::getInstance()->beforeAll->set($closure);
}
} }
/** if (!function_exists('beforeEach')) {
/**
* Runs the given closure before each test in the current file. * Runs the given closure before each test in the current file.
* *
* @return BeforeEachCall|TestCase|mixed * @return BeforeEachCall|TestCase|mixed
*/ */
function beforeEach(Closure $closure = null): BeforeEachCall function beforeEach(Closure $closure = null): BeforeEachCall
{ {
$filename = Backtrace::file(); $filename = Backtrace::file();
return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure); return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure);
}
} }
/** if (!function_exists('dataset')) {
/**
* Registers the given dataset. * Registers the given dataset.
* *
* @param Closure|iterable<int|string, mixed> $dataset * @param Closure|iterable<int|string, mixed> $dataset
*/ */
function dataset(string $name, $dataset): void function dataset(string $name, $dataset): void
{ {
Datasets::set($name, $dataset); Datasets::set($name, $dataset);
}
} }
/** if (!function_exists('uses')) {
/**
* The uses function binds the given * The uses function binds the given
* arguments to test closures. * arguments to test closures.
*/ */
function uses(string ...$classAndTraits): UsesCall function uses(string ...$classAndTraits): UsesCall
{ {
$filename = Backtrace::file(); $filename = Backtrace::file();
return new UsesCall($filename, $classAndTraits); return new UsesCall($filename, $classAndTraits);
}
} }
/** if (!function_exists('test')) {
/**
* Adds the given closure as a test. The first argument * Adds the given closure as a test. The first argument
* is the test description; the second argument is * is the test description; the second argument is
* a closure that contains the test expectations. * a closure that contains the test expectations.
* *
* @return TestCall|TestCase|mixed * @return TestCall|TestCase|mixed
*/ */
function test(string $description = null, Closure $closure = null) function test(string $description = null, Closure $closure = null)
{ {
if ($description === null && TestSuite::getInstance()->test !== null) { if ($description === null && TestSuite::getInstance()->test !== null) {
return new HigherOrderTapProxy(TestSuite::getInstance()->test); return new HigherOrderTapProxy(TestSuite::getInstance()->test);
} }
@ -69,38 +78,45 @@ function test(string $description = null, Closure $closure = null)
$filename = Backtrace::testFile(); $filename = Backtrace::testFile();
return new TestCall(TestSuite::getInstance(), $filename, $description, $closure); return new TestCall(TestSuite::getInstance(), $filename, $description, $closure);
}
} }
/** if (!function_exists('it')) {
/**
* Adds the given closure as a test. The first argument * Adds the given closure as a test. The first argument
* is the test description; the second argument is * is the test description; the second argument is
* a closure that contains the test expectations. * a closure that contains the test expectations.
* *
* @return TestCall|TestCase|mixed * @return TestCall|TestCase|mixed
*/ */
function it(string $description, Closure $closure = null): TestCall function it(string $description, Closure $closure = null): TestCall
{ {
$description = sprintf('it %s', $description); $description = sprintf('it %s', $description);
return test($description, $closure); return test($description, $closure);
}
} }
/** if (!function_exists('afterEach')) {
/**
* Runs the given closure after each test in the current file. * Runs the given closure after each test in the current file.
* *
* @return AfterEachCall|TestCase|mixed * @return AfterEachCall|TestCase|mixed
*/ */
function afterEach(Closure $closure = null): AfterEachCall function afterEach(Closure $closure = null): AfterEachCall
{ {
$filename = Backtrace::file(); $filename = Backtrace::file();
return new AfterEachCall(TestSuite::getInstance(), $filename, $closure); return new AfterEachCall(TestSuite::getInstance(), $filename, $closure);
}
} }
/** if (!function_exists('afterAll')) {
/**
* Runs the given closure after all tests in the current file. * Runs the given closure after all tests in the current file.
*/ */
function afterAll(Closure $closure): void function afterAll(Closure $closure): void
{ {
TestSuite::getInstance()->afterAll->set($closure); TestSuite::getInstance()->afterAll->set($closure);
}
} }