From c5eff68e486259b09521d677f68e2d06dc8dc0f1 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 15 Oct 2023 12:45:14 +0200 Subject: [PATCH] Environment::setupFunctions() creates global functions testException() --- src/Framework/Environment.php | 2 +- src/Framework/functions.php | 24 +++++++++++++++++++++ tests/Framework/DomQuery.css2Xpath.phpt | 26 +++++++++++------------ tests/Framework/Environment.loadData.phpt | 22 ++++++------------- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/Framework/Environment.php b/src/Framework/Environment.php index 19bcf602..d845cf5b 100644 --- a/src/Framework/Environment.php +++ b/src/Framework/Environment.php @@ -143,7 +143,7 @@ public static function setupErrors(): void /** - * Creates global functions test(), setUp() and tearDown(). + * Creates global functions test(), testException(), setUp() and tearDown(). */ public static function setupFunctions(): void { diff --git a/src/Framework/functions.php b/src/Framework/functions.php index 9846d1f5..a26f0499 100644 --- a/src/Framework/functions.php +++ b/src/Framework/functions.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Tester\Assert; use Tester\Dumper; use Tester\Environment; @@ -31,6 +32,29 @@ function test(string $description, Closure $closure): void } +function testException( + string $description, + Closure $function, + string $class, + ?string $message = null, + $code = null, +): void +{ + try { + Assert::exception($function, $class, $message, $code); + if ($description !== '') { + Environment::print(Dumper::color('lime', '√') . " $description"); + } + + } catch (Throwable $e) { + if ($description !== '') { + Environment::print(Dumper::color('red', '×') . " $description\n\n"); + } + throw $e; + } +} + + function setUp(?Closure $closure): void { static $fn; diff --git a/tests/Framework/DomQuery.css2Xpath.phpt b/tests/Framework/DomQuery.css2Xpath.phpt index f17bf5b7..a8cb7bd4 100644 --- a/tests/Framework/DomQuery.css2Xpath.phpt +++ b/tests/Framework/DomQuery.css2Xpath.phpt @@ -81,17 +81,15 @@ test('complex', function () { }); -test('pseudoclass', function () { - Assert::exception( - fn() => DomQuery::css2xpath('a:first-child'), - InvalidArgumentException::class, - ); -}); - - -test('adjacent sibling combinator', function () { - Assert::exception( - fn() => DomQuery::css2xpath('div + span'), - InvalidArgumentException::class, - ); -}); +testException( + 'pseudoclass', + fn() => DomQuery::css2xpath('a:first-child'), + InvalidArgumentException::class, +); + + +testException( + 'adjacent sibling combinator', + fn() => DomQuery::css2xpath('div + span'), + InvalidArgumentException::class, +); diff --git a/tests/Framework/Environment.loadData.phpt b/tests/Framework/Environment.loadData.phpt index 1b9e689a..3e72bb55 100644 --- a/tests/Framework/Environment.loadData.phpt +++ b/tests/Framework/Environment.loadData.phpt @@ -29,23 +29,13 @@ test('', function () use ($key, $file) { }); -test('', function () use ($key, $file) { +testException('', function () use ($key, $file) { $_SERVER['argv'][$key] = "--dataprovider=bar|$file"; - - Assert::exception( - fn() => Environment::loadData(), - Exception::class, - "Missing dataset 'bar' from data provider '%a%'.", - ); -}); + Environment::loadData(); +}, Exception::class, "Missing dataset 'bar' from data provider '%a%'."); -test('', function () use ($key, $file) { +testException('', function () use ($key, $file) { unset($_SERVER['argv'][$key]); - - Assert::exception( - fn() => Environment::loadData(), - Exception::class, - 'Missing annotation @dataProvider.', - ); -}); + Environment::loadData(); +}, Exception::class, 'Missing annotation @dataProvider.');