From 3324455e0a6badc6ac0c4e59b057c4fb578299a8 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sun, 4 Dec 2022 16:32:45 +0000 Subject: [PATCH] feat: only registers globals if necessary --- bin/pest | 2 + src/Functions.php | 284 +++++++++++++++++++++++----------------------- 2 files changed, 145 insertions(+), 141 deletions(-) diff --git a/bin/pest b/bin/pest index ac1e98b6..8a6b8596 100755 --- a/bin/pest +++ b/bin/pest @@ -11,6 +11,8 @@ use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; (static function () { + $_SERVER['__PEST__'] = true; + // Ensures Collision's Printer is registered. $_SERVER['COLLISION_PRINTER'] = 'DefaultPrinter'; diff --git a/src/Functions.php b/src/Functions.php index ea53c22c..7adfccc3 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -14,149 +14,151 @@ use Pest\Support\HigherOrderTapProxy; use Pest\TestSuite; use PHPUnit\Framework\TestCase; -if (! function_exists('expect')) { - /** - * Creates a new expectation. - * - * @template TValue - * - * @param TValue|null $value - * @return Expectation - */ - function expect(mixed $value = null): Expectation - { - return new Expectation($value); - } -} - -if (! function_exists('beforeAll')) { - /** - * Runs the given closure before all tests in the current file. - */ - function beforeAll(Closure $closure): void - { - TestSuite::getInstance()->beforeAll->set($closure); - } -} - -if (! function_exists('beforeEach')) { - /** - * Runs the given closure before each test in the current file. - * - * @return BeforeEachCall|TestCase|mixed - */ - function beforeEach(Closure $closure = null): BeforeEachCall - { - $filename = Backtrace::file(); - - return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure); - } -} - -if (! function_exists('dataset')) { - /** - * Registers the given dataset. - * - * @param Closure|iterable $dataset - */ - function dataset(string $name, Closure|iterable $dataset): void - { - $scope = DatasetInfo::scope(Backtrace::datasetsFile()); - DatasetsRepository::set($name, $dataset, $scope); - } -} - -if (! function_exists('uses')) { - /** - * The uses function binds the given - * arguments to test closures. - * - * @param class-string ...$classAndTraits - */ - function uses(string ...$classAndTraits): UsesCall - { - $filename = Backtrace::file(); - - return new UsesCall($filename, array_values($classAndTraits)); - } -} - -if (! function_exists('test')) { - /** - * Adds the given closure as a test. The first argument - * is the test description; the second argument is - * a closure that contains the test expectations. - * - * @return TestCall|TestCase|mixed - */ - function test(string $description = null, Closure $closure = null) - { - if ($description === null && TestSuite::getInstance()->test !== null) { - return new HigherOrderTapProxy(TestSuite::getInstance()->test); +if (array_key_exists('__PEST__', $_SERVER)) { + if (! function_exists('expect')) { + /** + * Creates a new expectation. + * + * @template TValue + * + * @param TValue|null $value + * @return Expectation + */ + function expect(mixed $value = null): Expectation + { + return new Expectation($value); } + } - $filename = Backtrace::testFile(); + if (! function_exists('beforeAll')) { + /** + * Runs the given closure before all tests in the current file. + */ + function beforeAll(Closure $closure): void + { + TestSuite::getInstance()->beforeAll->set($closure); + } + } - return new TestCall(TestSuite::getInstance(), $filename, $description, $closure); - } -} - -if (! function_exists('it')) { - /** - * Adds the given closure as a test. The first argument - * is the test description; the second argument is - * a closure that contains the test expectations. - * - * @return TestCall|TestCase|mixed - */ - function it(string $description, Closure $closure = null): TestCall - { - $description = sprintf('it %s', $description); - - /** @var TestCall $test */ - $test = test($description, $closure); - - return $test; - } -} - -if (! function_exists('todo')) { - /** - * Adds the given todo test. Internally, this test - * is marked as incomplete. Yet, Collision, Pest's - * printer, will display it as a "todo" test. - * - * @return TestCall|TestCase|mixed - */ - function todo(string $description): TestCall - { - /* @phpstan-ignore-next-line */ - return test($description, fn () => self::markTestSkipped( - '__TODO__', - )); - } -} - -if (! function_exists('afterEach')) { - /** - * Runs the given closure after each test in the current file. - * - * @return AfterEachCall|TestCase|mixed - */ - function afterEach(Closure $closure = null): AfterEachCall - { - $filename = Backtrace::file(); - - return new AfterEachCall(TestSuite::getInstance(), $filename, $closure); - } -} - -if (! function_exists('afterAll')) { - /** - * Runs the given closure after all tests in the current file. - */ - function afterAll(Closure $closure): void - { - TestSuite::getInstance()->afterAll->set($closure); + if (! function_exists('beforeEach')) { + /** + * Runs the given closure before each test in the current file. + * + * @return BeforeEachCall|TestCase|mixed + */ + function beforeEach(Closure $closure = null): BeforeEachCall + { + $filename = Backtrace::file(); + + return new BeforeEachCall(TestSuite::getInstance(), $filename, $closure); + } + } + + if (! function_exists('dataset')) { + /** + * Registers the given dataset. + * + * @param Closure|iterable $dataset + */ + function dataset(string $name, Closure|iterable $dataset): void + { + $scope = DatasetInfo::scope(Backtrace::datasetsFile()); + DatasetsRepository::set($name, $dataset, $scope); + } + } + + if (! function_exists('uses')) { + /** + * The uses function binds the given + * arguments to test closures. + * + * @param class-string ...$classAndTraits + */ + function uses(string ...$classAndTraits): UsesCall + { + $filename = Backtrace::file(); + + return new UsesCall($filename, array_values($classAndTraits)); + } + } + + if (! function_exists('test')) { + /** + * Adds the given closure as a test. The first argument + * is the test description; the second argument is + * a closure that contains the test expectations. + * + * @return TestCall|TestCase|mixed + */ + function test(string $description = null, Closure $closure = null) + { + if ($description === null && TestSuite::getInstance()->test !== null) { + return new HigherOrderTapProxy(TestSuite::getInstance()->test); + } + + $filename = Backtrace::testFile(); + + return new TestCall(TestSuite::getInstance(), $filename, $description, $closure); + } + } + + if (! function_exists('it')) { + /** + * Adds the given closure as a test. The first argument + * is the test description; the second argument is + * a closure that contains the test expectations. + * + * @return TestCall|TestCase|mixed + */ + function it(string $description, Closure $closure = null): TestCall + { + $description = sprintf('it %s', $description); + + /** @var TestCall $test */ + $test = test($description, $closure); + + return $test; + } + } + + if (! function_exists('todo')) { + /** + * Adds the given todo test. Internally, this test + * is marked as incomplete. Yet, Collision, Pest's + * printer, will display it as a "todo" test. + * + * @return TestCall|TestCase|mixed + */ + function todo(string $description): TestCall + { + /* @phpstan-ignore-next-line */ + return test($description, fn () => self::markTestSkipped( + '__TODO__', + )); + } + } + + if (! function_exists('afterEach')) { + /** + * Runs the given closure after each test in the current file. + * + * @return AfterEachCall|TestCase|mixed + */ + function afterEach(Closure $closure = null): AfterEachCall + { + $filename = Backtrace::file(); + + return new AfterEachCall(TestSuite::getInstance(), $filename, $closure); + } + } + + if (! function_exists('afterAll')) { + /** + * Runs the given closure after all tests in the current file. + */ + function afterAll(Closure $closure): void + { + TestSuite::getInstance()->afterAll->set($closure); + } } }