Skip to content

Commit

Permalink
Add coverage for hardcoded error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Jan 30, 2025
1 parent 37c0825 commit 963b16f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Conversion/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function getException(ResponseInterface $response): ClientException\Ex

if ($status === 402) {
$e = new ClientException\Request('This endpoint may need activating on your account. ' .
'"Please email [email protected] for more information', $status);
'Please email [email protected] for more information', $status);
} elseif ($status >= 400 && $status < 500) {
$e = new ClientException\Request($body['error_title'], $status);
} elseif ($status >= 500 && $status < 600) {
Expand Down
38 changes: 33 additions & 5 deletions test/Conversion/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace VonageTest\Conversion;

use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use Prophecy\Argument;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use ReflectionClass;
use Vonage\Client;
use Vonage\Client as VonageClient;
use Vonage\Client\APIResource;
Expand Down Expand Up @@ -140,12 +142,38 @@ public function testVoiceWithoutTimestamp(): void

public function testExceptionHandler(): void
{
$this->expectException(Client\Exception\Request::class);
$reflection = new ReflectionClass($this->conversionClient);
$method = $reflection->getMethod('getException');
$method->setAccessible(true);

$this->vonageClient->send(Argument::that(function (RequestInterface $request) {
return true;
}))->willReturn($this->getResponse('error', 402));
$response = new Response(402);

$this->conversionClient->sms('ABC123', true, '123456');
$return = $method->invoke($this->conversionClient, $response);
$this->assertEquals('This endpoint may need activating on your account. Please email [email protected] for more information', $return->getMessage());
$this->assertInstanceOf(Client\Exception\Request::class, $return);

$serverResponse = new Response(500, [], json_encode([
'error_title' => 'Vonage Server Error',
]));

$return2 = $method->invoke($this->conversionClient, $serverResponse);
$this->assertEquals('Vonage Server Error', $return2->getMessage());
$this->assertInstanceOf(Client\Exception\Server::class, $return2);

$unexpected = new Response(201, [], json_encode([
'error_title' => 'this is not an error',
]));

$return3 = $method->invoke($this->conversionClient, $unexpected);
$this->assertEquals('Unexpected HTTP Status Code (201)', $return3->getMessage());
$this->assertInstanceOf(Client\Exception\Exception::class, $return3);

$notFound = new Response(404, [], json_encode([
'error_title' => 'Not Found',
]));

$return4 = $method->invoke($this->conversionClient, $notFound);
$this->assertEquals('Not Found', $return4->getMessage());
$this->assertInstanceOf(Client\Exception\Request::class, $return4);
}
}

0 comments on commit 963b16f

Please sign in to comment.