Skip to content

Commit c5eff68

Browse files
committed
Environment::setupFunctions() creates global functions testException()
1 parent c309c1e commit c5eff68

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

Diff for: src/Framework/Environment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static function setupErrors(): void
143143

144144

145145
/**
146-
* Creates global functions test(), setUp() and tearDown().
146+
* Creates global functions test(), testException(), setUp() and tearDown().
147147
*/
148148
public static function setupFunctions(): void
149149
{

Diff for: src/Framework/functions.php

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Tester\Assert;
56
use Tester\Dumper;
67
use Tester\Environment;
78

@@ -31,6 +32,29 @@ function test(string $description, Closure $closure): void
3132
}
3233

3334

35+
function testException(
36+
string $description,
37+
Closure $function,
38+
string $class,
39+
?string $message = null,
40+
$code = null,
41+
): void
42+
{
43+
try {
44+
Assert::exception($function, $class, $message, $code);
45+
if ($description !== '') {
46+
Environment::print(Dumper::color('lime', '') . " $description");
47+
}
48+
49+
} catch (Throwable $e) {
50+
if ($description !== '') {
51+
Environment::print(Dumper::color('red', '×') . " $description\n\n");
52+
}
53+
throw $e;
54+
}
55+
}
56+
57+
3458
function setUp(?Closure $closure): void
3559
{
3660
static $fn;

Diff for: tests/Framework/DomQuery.css2Xpath.phpt

+12-14
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ test('complex', function () {
8181
});
8282

8383

84-
test('pseudoclass', function () {
85-
Assert::exception(
86-
fn() => DomQuery::css2xpath('a:first-child'),
87-
InvalidArgumentException::class,
88-
);
89-
});
90-
91-
92-
test('adjacent sibling combinator', function () {
93-
Assert::exception(
94-
fn() => DomQuery::css2xpath('div + span'),
95-
InvalidArgumentException::class,
96-
);
97-
});
84+
testException(
85+
'pseudoclass',
86+
fn() => DomQuery::css2xpath('a:first-child'),
87+
InvalidArgumentException::class,
88+
);
89+
90+
91+
testException(
92+
'adjacent sibling combinator',
93+
fn() => DomQuery::css2xpath('div + span'),
94+
InvalidArgumentException::class,
95+
);

Diff for: tests/Framework/Environment.loadData.phpt

+6-16
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,13 @@ test('', function () use ($key, $file) {
2929
});
3030

3131

32-
test('', function () use ($key, $file) {
32+
testException('', function () use ($key, $file) {
3333
$_SERVER['argv'][$key] = "--dataprovider=bar|$file";
34-
35-
Assert::exception(
36-
fn() => Environment::loadData(),
37-
Exception::class,
38-
"Missing dataset 'bar' from data provider '%a%'.",
39-
);
40-
});
34+
Environment::loadData();
35+
}, Exception::class, "Missing dataset 'bar' from data provider '%a%'.");
4136

4237

43-
test('', function () use ($key, $file) {
38+
testException('', function () use ($key, $file) {
4439
unset($_SERVER['argv'][$key]);
45-
46-
Assert::exception(
47-
fn() => Environment::loadData(),
48-
Exception::class,
49-
'Missing annotation @dataProvider.',
50-
);
51-
});
40+
Environment::loadData();
41+
}, Exception::class, 'Missing annotation @dataProvider.');

0 commit comments

Comments
 (0)