From 90efcc8a8ad10190895c000677f3e85ddccc6712 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Wed, 7 Apr 2021 10:55:19 -0500 Subject: [PATCH] feat: add shared/global beforeAll and afterAll hooks --- src/Concerns/TestCase.php | 54 +++++++++++++++++++++++++++++ src/Repositories/TestRepository.php | 4 +-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Concerns/TestCase.php b/src/Concerns/TestCase.php index a0ecda8a..1272d49c 100644 --- a/src/Concerns/TestCase.php +++ b/src/Concerns/TestCase.php @@ -51,6 +51,22 @@ trait TestCase */ private $afterEach = null; + /** + * Holds a global/shared beforeAll ("set up before") closure if one has been + * defined. + * + * @var Closure|null + */ + private static $beforeAll = null; + + /** + * Holds a global/shared afterAll ("tear down after") closure if one has + * been defined. + * + * @var Closure|null + */ + private static $afterAll = null; + /** * Creates a new instance of the test case. */ @@ -90,6 +106,36 @@ trait TestCase $this->setDependencies($tests); } + /** + * Add a shared/"global" before all test hook that will execute **before** + * the test defined `beforeAll` hook(s). + */ + public function addBeforeAll(?Closure $hook): void + { + if (!$hook) { + return; + } + + self::$beforeAll = (self::$beforeAll instanceof Closure) + ? ChainableClosure::fromStatic(self::$beforeAll, $hook) + : $hook; + } + + /** + * Add a shared/"global" after all test hook that will execute **before** + * the test defined `afterAll` hook(s). + */ + public function addAfterAll(?Closure $hook): void + { + if (!$hook) { + return; + } + + self::$afterAll = (self::$afterAll instanceof Closure) + ? ChainableClosure::fromStatic(self::$afterAll, $hook) + : $hook; + } + /** * Add a shared/"global" before each test hook that will execute **before** * the test defined `beforeEach` hook. @@ -146,6 +192,10 @@ trait TestCase $beforeAll = TestSuite::getInstance()->beforeAll->get(self::$__filename); + if (self::$beforeAll instanceof Closure) { + $beforeAll = ChainableClosure::fromStatic(self::$beforeAll, $beforeAll); + } + call_user_func(Closure::bind($beforeAll, null, self::class)); } @@ -156,6 +206,10 @@ trait TestCase { $afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename); + if (self::$afterAll instanceof Closure) { + $afterAll = ChainableClosure::fromStatic(self::$afterAll, $afterAll); + } + call_user_func(Closure::bind($afterAll, null, self::class)); parent::tearDownAfterClass(); diff --git a/src/Repositories/TestRepository.php b/src/Repositories/TestRepository.php index e26af691..ab49291e 100644 --- a/src/Repositories/TestRepository.php +++ b/src/Repositories/TestRepository.php @@ -66,10 +66,10 @@ final class TestRepository // IDEA: Consider set the real lines on these. $testCase->factoryProxies->add($filename, 0, 'addGroups', [$groups]); - // $testCase->factoryProxies->add($filename, 0, 'addBeforeAll', [$hooks[0] ?? null]); + $testCase->factoryProxies->add($filename, 0, 'addBeforeAll', [$hooks[0] ?? null]); $testCase->factoryProxies->add($filename, 0, 'addBeforeEach', [$hooks[1] ?? null]); $testCase->factoryProxies->add($filename, 0, 'addAfterEach', [$hooks[2] ?? null]); - // $testCase->factoryProxies->add($filename, 0, 'addAfterAll', [$hooks[3] ?? null]); + $testCase->factoryProxies->add($filename, 0, 'addAfterAll', [$hooks[3] ?? null]); } };