refactor: PHP 8 features

This commit is contained in:
Nuno Maduro
2021-10-24 18:29:59 +01:00
parent e8c2fe6e35
commit 2b687a7269
43 changed files with 283 additions and 635 deletions

View File

@ -13,61 +13,42 @@ use PHPUnit\Framework\ExecutionOrderDependency;
use Throwable;
/**
* To avoid inheritance conflicts, all the fields related to Pest only will be prefixed by double underscore.
*
* @internal
*/
trait Testable
{
/**
* The test case description. Contains the first
* argument of global functions like `it` and `test`.
*
* @var string
* The Test Case description.
*/
private $__description;
private string $__description;
/**
* Holds the test closure function.
*
* @var Closure
* The Test Case "test" closure.
*/
private $__test;
private Closure $__test;
/**
* Holds a global/shared beforeEach ("set up") closure if one has been
* defined.
*
* @var Closure|null
* The Test Case "setUp" closure.
*/
private $__beforeEach = null;
private ?Closure $__beforeEach = null;
/**
* Holds a global/shared afterEach ("tear down") closure if one has been
* defined.
*
* @var Closure|null
* The Test Case "tearDown" closure.
*/
private $__afterEach = null;
private ?Closure $__afterEach = null;
/**
* Holds a global/shared beforeAll ("set up before") closure if one has been
* defined.
*
* @var Closure|null
* The Test Case "setUpBeforeClass" closure.
*/
private static $__beforeAll = null;
private static ?Closure $__beforeAll = null;
/**
* Holds a global/shared afterAll ("tear down after") closure if one has
* been defined.
*
* @var Closure|null
* The test "tearDownAfterClass" closure.
*/
private static $__afterAll = null;
private static ?Closure $__afterAll = null;
/**
* Creates a new instance of the test case.
* Creates a new Test Case instance.
*/
public function __construct(Closure $test, string $description, array $data)
{
@ -82,7 +63,7 @@ trait Testable
}
/**
* Adds the groups to the current test case.
* Adds groups to the Test Case.
*/
public function addGroups(array $groups): void
{
@ -92,14 +73,14 @@ trait Testable
}
/**
* Add dependencies to the test case and map them to instances of ExecutionOrderDependency.
* Adds dependencies to the Test Case.
*/
public function addDependencies(array $tests): void
{
$className = get_class($this);
$className = $this::class;
$tests = array_map(function (string $test) use ($className): ExecutionOrderDependency {
if (strpos($test, '::') === false) {
$tests = array_map(static function (string $test) use ($className): ExecutionOrderDependency {
if (!str_contains($test, '::')) {
$test = "{$className}::{$test}";
}
@ -110,8 +91,7 @@ trait Testable
}
/**
* Add a shared/"global" before all test hook that will execute **before**
* the test defined `beforeAll` hook(s).
* Adds a new "setUpBeforeClass" to the Test Case.
*/
public function __addBeforeAll(?Closure $hook): void
{
@ -125,8 +105,7 @@ trait Testable
}
/**
* Add a shared/"global" after all test hook that will execute **before**
* the test defined `afterAll` hook(s).
* Adds a new "tearDownAfterClass" to the Test Case.
*/
public function __addAfterAll(?Closure $hook): void
{
@ -140,8 +119,7 @@ trait Testable
}
/**
* Add a shared/"global" before each test hook that will execute **before**
* the test defined `beforeEach` hook.
* Adds a new "setUp" to the Test Case.
*/
public function __addBeforeEach(?Closure $hook): void
{
@ -149,8 +127,7 @@ trait Testable
}
/**
* Add a shared/"global" after each test hook that will execute **before**
* the test defined `afterEach` hook.
* Adds a new "tearDown" to the Test Case.
*/
public function __addAfterEach(?Closure $hook): void
{
@ -158,7 +135,7 @@ trait Testable
}
/**
* Add a shared/global hook and compose them if more than one is passed.
* Adds a new "hook" to the Test Case.
*/
private function __addHook(string $property, ?Closure $hook): void
{
@ -172,9 +149,7 @@ trait Testable
}
/**
* Returns the test case name. Note that, in Pest
* we ignore withDataset argument as the description
* already contains the dataset description.
* Gets the Test Case name.
*/
public function getName(bool $withDataSet = true): string
{
@ -183,13 +158,16 @@ trait Testable
: $this->__description;
}
public static function __getFileName(): string
/**
* Gets the Test Case filename.
*/
public static function __getFilename(): string
{
return self::$__filename;
}
/**
* This method is called before the first test of this test class is run.
* This method is called before the first test of this Test Case is run.
*/
public static function setUpBeforeClass(): void
{
@ -205,7 +183,7 @@ trait Testable
}
/**
* This method is called after the last test of this test class is run.
* This method is called after the last test of this Test Case is run.
*/
public static function tearDownAfterClass(): void
{
@ -221,7 +199,7 @@ trait Testable
}
/**
* Gets executed before the test.
* Gets executed before the Test Case.
*/
protected function setUp(): void
{
@ -239,7 +217,7 @@ trait Testable
}
/**
* Gets executed after the test.
* Gets executed after the Test Case.
*/
protected function tearDown(): void
{
@ -257,7 +235,7 @@ trait Testable
}
/**
* Returns the test case as string.
* Gets the Test Case filename and description.
*/
public function toString(): string
{
@ -269,13 +247,11 @@ trait Testable
}
/**
* Runs the test.
*
* @return mixed
* Executes the Test Case current test.
*
* @throws Throwable
*/
public function __test()
public function __test(): mixed
{
return $this->__callClosure($this->__test, $this->__resolveTestArguments(func_get_args()));
}
@ -287,23 +263,20 @@ trait Testable
*/
private function __resolveTestArguments(array $arguments): array
{
return array_map(function ($data) {
return $data instanceof Closure ? $this->__callClosure($data, []) : $data;
}, $arguments);
return array_map(fn ($data) => $data instanceof Closure ? $this->__callClosure($data, []) : $data, $arguments);
}
/**
* @return mixed
*
* @throws Throwable
*/
private function __callClosure(Closure $closure, array $arguments)
private function __callClosure(Closure $closure, array $arguments): mixed
{
return ExceptionTrace::ensure(function () use ($closure, $arguments) {
return call_user_func_array(Closure::bind($closure, $this, get_class($this)), $arguments);
});
return ExceptionTrace::ensure(fn () => call_user_func_array(Closure::bind($closure, $this, $this::class), $arguments));
}
/**
* Gets the Test Case name that should be used by printers.
*/
public function getPrintableTestCaseName(): string
{
return ltrim(self::class, 'P\\');