Skip to content

Commit cddfd74

Browse files
committed
Environment::setupFunctions() creates global functions test(), setUp() & tearDown()
1 parent 9882813 commit cddfd74

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

β€Žsrc/Framework/Environment.phpβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ public static function setupErrors(): void
142142
}
143143

144144

145+
/**
146+
* Creates global functions test(), setUp() and tearDown().
147+
*/
148+
public static function setupFunctions(): void
149+
{
150+
require __DIR__ . '/functions.php';
151+
}
152+
153+
145154
/**
146155
* @internal
147156
*/

β€Žsrc/Framework/functions.phpβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Dumper;
6+
use Tester\Environment;
7+
8+
9+
function test(string $description, Closure $closure): void
10+
{
11+
if ($fn = (new ReflectionFunction('setUp'))->getStaticVariables()['fn']) {
12+
$fn();
13+
}
14+
15+
try {
16+
$closure();
17+
if ($description !== '') {
18+
Environment::print(Dumper::color('lime', '√') . " $description");
19+
}
20+
21+
} catch (Throwable $e) {
22+
if ($description !== '') {
23+
Environment::print(Dumper::color('red', 'Γ—') . " $description\n\n");
24+
}
25+
throw $e;
26+
}
27+
28+
if ($fn = (new ReflectionFunction('tearDown'))->getStaticVariables()['fn']) {
29+
$fn();
30+
}
31+
}
32+
33+
34+
function setUp(?Closure $closure): void
35+
{
36+
static $fn;
37+
$fn = $closure;
38+
}
39+
40+
41+
function tearDown(?Closure $closure): void
42+
{
43+
static $fn;
44+
$fn = $closure;
45+
}

β€Žtests/bootstrap.phpβ€Ž

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
require __DIR__ . '/../src/Runner/PhpInterpreter.php';
1010

1111

12-
function test(string $description, Closure $function): void
13-
{
14-
$function();
15-
}
12+
Tester\Environment::setupFunctions();
1613

1714

1815
function createInterpreter(): PhpInterpreter

0 commit comments

Comments
Β (0)