mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 02:37:22 +01:00
feat: add shared/global before each hook
This commit is contained in:
@ -55,6 +55,15 @@ trait TestCase
|
|||||||
$this->setGroups($groups);
|
$this->setGroups($groups);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a shared/"global" before each test hook that will execute **before**
|
||||||
|
* the test defined `beforeEach` hook.
|
||||||
|
*/
|
||||||
|
public function addBeforeEach(?Closure $hook): void
|
||||||
|
{
|
||||||
|
$this->beforeEach = $hook;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add dependencies to the test case and map them to instances of ExecutionOrderDependency.
|
* Add dependencies to the test case and map them to instances of ExecutionOrderDependency.
|
||||||
*/
|
*/
|
||||||
@ -121,6 +130,10 @@ trait TestCase
|
|||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
if ($this->beforeEach instanceof Closure) {
|
||||||
|
$this->__callClosure($this->beforeEach, func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
$beforeEach = TestSuite::getInstance()->beforeEach->get(self::$__filename);
|
$beforeEach = TestSuite::getInstance()->beforeEach->get(self::$__filename);
|
||||||
|
|
||||||
$this->__callClosure($beforeEach, func_get_args());
|
$this->__callClosure($beforeEach, func_get_args());
|
||||||
|
|||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\PendingObjects;
|
namespace Pest\PendingObjects;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Pest\Exceptions\InvalidUsesPath;
|
use Pest\Exceptions\InvalidUsesPath;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
|
|
||||||
@ -12,6 +13,13 @@ use Pest\TestSuite;
|
|||||||
*/
|
*/
|
||||||
final class UsesCall
|
final class UsesCall
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Contains a global before each hook closure to be executed.
|
||||||
|
*
|
||||||
|
* @var Closure
|
||||||
|
*/
|
||||||
|
private $beforeEach;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the class and traits.
|
* Holds the class and traits.
|
||||||
*
|
*
|
||||||
@ -97,11 +105,26 @@ final class UsesCall
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the global beforeEach test hook
|
||||||
|
*/
|
||||||
|
public function beforeEach(Closure $hook): UsesCall
|
||||||
|
{
|
||||||
|
$this->beforeEach = $hook;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch the creation of uses.
|
* Dispatch the creation of uses.
|
||||||
*/
|
*/
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
TestSuite::getInstance()->tests->use($this->classAndTraits, $this->groups, $this->targets);
|
TestSuite::getInstance()->tests->use(
|
||||||
|
$this->classAndTraits,
|
||||||
|
$this->groups,
|
||||||
|
$this->targets,
|
||||||
|
$this->beforeEach,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Repositories;
|
namespace Pest\Repositories;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Pest\Exceptions\ShouldNotHappen;
|
use Pest\Exceptions\ShouldNotHappen;
|
||||||
use Pest\Exceptions\TestAlreadyExist;
|
use Pest\Exceptions\TestAlreadyExist;
|
||||||
use Pest\Exceptions\TestCaseAlreadyInUse;
|
use Pest\Exceptions\TestCaseAlreadyInUse;
|
||||||
@ -46,8 +47,9 @@ final class TestRepository
|
|||||||
};
|
};
|
||||||
|
|
||||||
foreach ($this->uses as $path => $uses) {
|
foreach ($this->uses as $path => $uses) {
|
||||||
[$classOrTraits, $groups] = $uses;
|
[$classOrTraits, $groups, $beforeEach] = $uses;
|
||||||
$setClassName = function (TestCaseFactory $testCase, string $key) use ($path, $classOrTraits, $groups, $startsWith): void {
|
|
||||||
|
$setClassName = function (TestCaseFactory $testCase, string $key) use ($path, $classOrTraits, $groups, $startsWith, $beforeEach): void {
|
||||||
[$filename] = explode('@', $key);
|
[$filename] = explode('@', $key);
|
||||||
|
|
||||||
if ((!is_dir($path) && $filename === $path) || (is_dir($path) && $startsWith($filename, $path))) {
|
if ((!is_dir($path) && $filename === $path) || (is_dir($path) && $startsWith($filename, $path))) {
|
||||||
@ -62,10 +64,9 @@ final class TestRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$testCase
|
// IDEA: Consider set the real lines on these.
|
||||||
->factoryProxies
|
$testCase->factoryProxies->add($filename, 0, 'addBeforeEach', [$beforeEach]);
|
||||||
// Consider set the real line here.
|
$testCase->factoryProxies->add($filename, 0, 'addGroups', [$groups]);
|
||||||
->add($filename, 0, 'addGroups', [$groups]);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,7 +82,7 @@ final class TestRepository
|
|||||||
$state = count($onlyState) > 0 ? $onlyState : $this->state;
|
$state = count($onlyState) > 0 ? $onlyState : $this->state;
|
||||||
|
|
||||||
foreach ($state as $testFactory) {
|
foreach ($state as $testFactory) {
|
||||||
/* @var TestCaseFactory $testFactory */
|
/** @var TestCaseFactory $testFactory */
|
||||||
$tests = $testFactory->build($testSuite);
|
$tests = $testFactory->build($testSuite);
|
||||||
foreach ($tests as $test) {
|
foreach ($tests as $test) {
|
||||||
$each($test);
|
$each($test);
|
||||||
@ -95,8 +96,9 @@ final class TestRepository
|
|||||||
* @param array<int, string> $classOrTraits
|
* @param array<int, string> $classOrTraits
|
||||||
* @param array<int, string> $groups
|
* @param array<int, string> $groups
|
||||||
* @param array<int, string> $paths
|
* @param array<int, string> $paths
|
||||||
|
* @param Closure|null $beforeEach
|
||||||
*/
|
*/
|
||||||
public function use(array $classOrTraits, array $groups, array $paths): void
|
public function use(array $classOrTraits, array $groups, array $paths, ?Closure $beforeEach): void
|
||||||
{
|
{
|
||||||
foreach ($classOrTraits as $classOrTrait) {
|
foreach ($classOrTraits as $classOrTrait) {
|
||||||
if (!class_exists($classOrTrait) && !trait_exists($classOrTrait)) {
|
if (!class_exists($classOrTrait) && !trait_exists($classOrTrait)) {
|
||||||
@ -109,9 +111,10 @@ final class TestRepository
|
|||||||
$this->uses[$path] = [
|
$this->uses[$path] = [
|
||||||
array_merge($this->uses[$path][0], $classOrTraits),
|
array_merge($this->uses[$path][0], $classOrTraits),
|
||||||
array_merge($this->uses[$path][1], $groups),
|
array_merge($this->uses[$path][1], $groups),
|
||||||
|
$this->uses[$path][2] ?? $beforeEach,
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
$this->uses[$path] = [$classOrTraits, $groups];
|
$this->uses[$path] = [$classOrTraits, $groups, $beforeEach];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user