Skip to content

Commit

Permalink
Improve exceptions and their tests with mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 22, 2023
1 parent a910d16 commit f6b7f39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private readonly array $contents)
$message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error';

if (is_array($message)) {
$message = implode("\n", $message);
$message = implode(PHP_EOL, $message);
}

parent::__construct($message);
Expand Down
35 changes: 31 additions & 4 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@
$response = new Response(404, ['Content-Type' => 'application/json; charset=utf-8'], json_encode([
'error' => [
'message' => [
'Invalid schema for function \'get_current_weather\': In context=(\'properties\', \'location\'), array schema missing items',
'Invalid schema for function \'get_current_weather\':',
'In context=(\'properties\', \'location\'), array schema missing items',
],
'type' => 'invalid_request_error',
'param' => null,
Expand All @@ -234,8 +235,8 @@

expect(fn () => $this->http->requestObject($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('Invalid schema for function \'get_current_weather\': In context=(\'properties\', \'location\'), array schema missing items')
->and($e->getErrorMessage())->toBe('Invalid schema for function \'get_current_weather\': In context=(\'properties\', \'location\'), array schema missing items')
expect($e->getMessage())->toBe('Invalid schema for function \'get_current_weather\':'.PHP_EOL.'In context=(\'properties\', \'location\'), array schema missing items')
->and($e->getErrorMessage())->toBe('Invalid schema for function \'get_current_weather\':'.PHP_EOL.'In context=(\'properties\', \'location\'), array schema missing items')
->and($e->getErrorCode())->toBeNull()
->and($e->getErrorType())->toBe('invalid_request_error');
});
Expand Down Expand Up @@ -267,6 +268,32 @@
});
});

test('error message may be empty and code is an integer', function () {
$payload = Payload::create('completions', ['model' => 'gpt-4']);

$response = new Response(404, ['Content-Type' => 'application/json; charset=utf-8'], json_encode([
'error' => [
'message' => '',
'type' => 'invalid_request_error',
'param' => null,
'code' => 123,
],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->andReturn($response);

expect(fn () => $this->http->requestObject($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('123')
->and($e->getErrorMessage())->toBe('123')
->and($e->getErrorCode())->toBe(123)
->and($e->getErrorType())->toBe('invalid_request_error');
});
});

test('error message and code may be empty', function () {
$payload = Payload::create('completions', ['model' => 'gpt-4']);

Expand Down Expand Up @@ -323,7 +350,7 @@
->andReturn($response);

$this->http->requestObject($payload);
})->throws(UnserializableResponse::class, 'Syntax error');
})->throws(UnserializableResponse::class, 'Syntax error', 0);

test('request plain text', function () {
$payload = Payload::upload('audio/transcriptions', []);
Expand Down

0 comments on commit f6b7f39

Please sign in to comment.