feat: add shared/global beforeAll and afterAll hooks

This commit is contained in:
jordanbrauer
2021-04-07 10:55:19 -05:00
parent 7d35ee9998
commit 90efcc8a8a
2 changed files with 56 additions and 2 deletions

View File

@ -51,6 +51,22 @@ trait TestCase
*/ */
private $afterEach = null; 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. * Creates a new instance of the test case.
*/ */
@ -90,6 +106,36 @@ trait TestCase
$this->setDependencies($tests); $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** * Add a shared/"global" before each test hook that will execute **before**
* the test defined `beforeEach` hook. * the test defined `beforeEach` hook.
@ -146,6 +192,10 @@ trait TestCase
$beforeAll = TestSuite::getInstance()->beforeAll->get(self::$__filename); $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)); call_user_func(Closure::bind($beforeAll, null, self::class));
} }
@ -156,6 +206,10 @@ trait TestCase
{ {
$afterAll = TestSuite::getInstance()->afterAll->get(self::$__filename); $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)); call_user_func(Closure::bind($afterAll, null, self::class));
parent::tearDownAfterClass(); parent::tearDownAfterClass();

View File

@ -66,10 +66,10 @@ final class TestRepository
// IDEA: Consider set the real lines on these. // IDEA: Consider set the real lines on these.
$testCase->factoryProxies->add($filename, 0, 'addGroups', [$groups]); $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, 'addBeforeEach', [$hooks[1] ?? null]);
$testCase->factoryProxies->add($filename, 0, 'addAfterEach', [$hooks[2] ?? 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]);
} }
}; };