Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Schema/JSONDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class JSONDocument {
private bool $hasError = false;
/** @var null|callable(string, null|array<string, mixed>):void */
/** @var null|callable(string, int, null|array<string, mixed>):void */
private $errorCallback = null;

public function __construct(
Expand Down Expand Up @@ -55,6 +55,7 @@ public function set(string $key, mixed $value):void {
* properties may be set once an error is set.
*
* @param string $message The message to be set
* @param int $statusCode The HTTP response status code associated with the error
* @param null|array<string, mixed> $context An optional array containing error context to be returned
* @param string $property Optional property name for the error message
* @param string $contextProperty Optional property name for the error context array
Expand All @@ -63,6 +64,7 @@ public function set(string $key, mixed $value):void {
*/
public function error(
string $message,
int $statusCode = 400,
?array $context = null,
string $property = "error",
string $contextProperty = "errorContext"
Expand All @@ -80,7 +82,7 @@ public function error(

$this->hasError = true;
if($this->errorCallback !== null) {
call_user_func($this->errorCallback, $message, $context);
call_user_func($this->errorCallback, $message, $statusCode, $context);
}
}

Expand Down
32 changes: 25 additions & 7 deletions test/phpunit/Schema/JSONDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public function testError_disallowsSetAfterError():void {

public function testSetErrorCallback():void {
$calls = [];
$callback = function(string $message, ?array $context = null)use(&$calls):void {
array_push($calls, [$message, $context]);
$callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void {
array_push($calls, [$message, $statusCode, $context]);
};

$sut = new JSONDocument();
Expand All @@ -176,20 +176,38 @@ public function testSetErrorCallback():void {
$sut->error("Test");
self::assertCount(1, $calls);
self::assertSame("Test", $calls[0][0]);
self::assertNull($calls[0][1]);
self::assertSame(400, $calls[0][1]);
self::assertNull($calls[0][2]);
}

public function testSetErrorCallback_context():void {
$calls = [];
$callback = function(string $message, ?array $context = null)use(&$calls):void {
array_push($calls, [$message, $context]);
$callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void {
array_push($calls, [$message, $statusCode, $context]);
};

$sut = new JSONDocument();
$sut->setErrorCallback($callback);

$sut->error("Test", ["example" => "message"]);
$sut->error("Test", context: ["example" => "message"]);
self::assertCount(1, $calls);
self::assertSame(["example" => "message"], $calls[0][1]);
self::assertSame(400, $calls[0][1]);
self::assertSame(["example" => "message"], $calls[0][2]);
}

public function testSetErrorCallback_statusCode():void {
$calls = [];
$callback = function(string $message, int $statusCode, ?array $context = null)use(&$calls):void {
array_push($calls, [$message, $statusCode, $context]);
};

$sut = new JSONDocument();
$sut->setErrorCallback($callback);

$sut->error("Test", 422);
self::assertCount(1, $calls);
self::assertSame("Test", $calls[0][0]);
self::assertSame(422, $calls[0][1]);
self::assertNull($calls[0][2]);
}
}
Loading