Skip to content

Commit

Permalink
Environment::setupFunctions() creates global functions testException()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2023
1 parent c309c1e commit c5eff68
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
24 changes: 24 additions & 0 deletions src/Framework/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Tester\Assert;
use Tester\Dumper;
use Tester\Environment;

Expand Down Expand Up @@ -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;
Expand Down
26 changes: 12 additions & 14 deletions tests/Framework/DomQuery.css2Xpath.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
22 changes: 6 additions & 16 deletions tests/Framework/Environment.loadData.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

0 comments on commit c5eff68

Please sign in to comment.