Skip to content

Commit

Permalink
conveying defaults test
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Oct 16, 2023
1 parent 56a9643 commit cbe14bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
8 changes: 7 additions & 1 deletion tests/serverside.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ function apply(string $class): void

$instance = new $class();
testInterfaces($instance, SupportsContextStrata::class, IndicatesServerFault::class);
testAllConveyingCapabilities($instance);

testExplanation($instance);
testInternalContext($instance);
testTagging($instance);

testPublicContext($instance);
testPublicConveying($instance, $instance->getDefaultMessageToConvey());
}

// Here we test the 2 server-side exceptions:
Expand Down
30 changes: 14 additions & 16 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ function testTagging(Throwable $e): void
/**
* @param SupportsPublicConveying&SupportsPublicContext&Throwable $e
*/
function testPublicConveying(Throwable $e): void
function testPublicConveying(Throwable $e, string $defaultMessage): void
{
Assert::type(SupportsPublicConveying::class, $e);
Assert::type(SupportsPublicContext::class, $e);

// first call to convey detailed data
$e->convey(
'An important message.',
'input.email',
Expand All @@ -159,10 +160,12 @@ function testPublicConveying(Throwable $e): void
400,
'crashed...somehow',
);

// second call without parameters (should use defaults)
$e->convey();

$context = $e->publicContext();
Assert::same(2, count($context));

/** @var ErrorContainer $container */
$container = $context[0] ?? null;
Assert::type(ErrorContainer::class, $container);
Expand All @@ -173,10 +176,15 @@ function testPublicConveying(Throwable $e): void
Assert::same(400, $container->status);
Assert::same('crashed...somehow', $container->code);


$emptyContainer = $context[1] ?? null;
Assert::type(ErrorContainer::class, $emptyContainer);
Assert::same($e->getMessage(), $emptyContainer->message);
// here we test that without the parameters to `convey` method, the container receives the expected default message and nothing else
$defaultContainer = $context[1] ?? null;
Assert::type(ErrorContainer::class, $defaultContainer);
Assert::same($defaultMessage, $defaultContainer->message);
Assert::same(null, $defaultContainer->source);
Assert::same(null, $defaultContainer->detail);
Assert::same(null, $defaultContainer->meta);
Assert::same(null, $defaultContainer->status);
Assert::same(null, $defaultContainer->code);
}


Expand All @@ -197,16 +205,6 @@ function testCompatibility(string $class): void
Assert::same($previous, $instance->getPrevious());
}

function testAllConveyingCapabilities(Throwable $e): void
{
testExplanation($e);
testInternalContext($e);
testTagging($e);

testPublicContext($e);
testPublicConveying($e);
}

function testHttpCapabilities(Throwable $e): void
{
// TODO
Expand Down

0 comments on commit cbe14bf

Please sign in to comment.