From 07a55570e360ca3159e3f43d3c0f720c724b9ad2 Mon Sep 17 00:00:00 2001 From: Jim Seconde Date: Fri, 31 Jan 2025 11:47:25 +0000 Subject: [PATCH] Added exception and credentials testing --- phpunit.xml.dist | 3 ++ .../Handler/SignatureBodyFormHandlerTest.php | 4 +- .../Handler/SignatureBodyHandlerTest.php | 36 ++++++++++++++++++ .../Handler/SignatureQueryHandlerTest.php | 30 +++++++++++++++ .../Handler/TokenBodyHandlerTest.php | 37 +++++++++++++++++++ .../Handler/TokenQueryHandlerTest.php | 31 ++++++++++++++++ test/Client/Exception/ConflictTest.php | 33 +++++++++++++++++ test/Client/Exception/CredentialsTest.php | 33 +++++++++++++++++ test/Client/Exception/NotFoundTest.php | 33 +++++++++++++++++ test/Client/Exception/RequestTest.php | 33 +++++++++++++++++ test/Client/Exception/ServerTest.php | 33 +++++++++++++++++ test/Client/Exception/TransportTest.php | 33 +++++++++++++++++ .../{Validation.php => ValidationTest.php} | 11 +----- test/SMS/ExceptionErrorHandlerTest.php | 2 +- test/Verify/VerificationTest.php | 2 +- 15 files changed, 340 insertions(+), 14 deletions(-) create mode 100644 test/Client/Credentials/Handler/SignatureBodyHandlerTest.php create mode 100644 test/Client/Credentials/Handler/SignatureQueryHandlerTest.php create mode 100644 test/Client/Credentials/Handler/TokenBodyHandlerTest.php create mode 100644 test/Client/Credentials/Handler/TokenQueryHandlerTest.php create mode 100644 test/Client/Exception/ConflictTest.php create mode 100644 test/Client/Exception/CredentialsTest.php create mode 100644 test/Client/Exception/NotFoundTest.php create mode 100644 test/Client/Exception/RequestTest.php create mode 100644 test/Client/Exception/ServerTest.php create mode 100644 test/Client/Exception/TransportTest.php rename test/Client/Exception/{Validation.php => ValidationTest.php} (79%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bc3bb2f9..2faff0e3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -26,6 +26,9 @@ test/Verify + + test/Client/Credentials/Handler + test/Account diff --git a/test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php b/test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php index 8b97c299..ca23c0fd 100644 --- a/test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php +++ b/test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php @@ -27,14 +27,12 @@ public function testSignatureBodyFormHandler() $handler = new SignatureBodyFormHandler(); $authRequest = $handler($request, $credentials); - - // Check the modified body $authRequest->getBody()->rewind(); $newBody = $authRequest->getBody()->getContents(); parse_str($newBody, $params); $this->assertArrayHasKey('api_key', $params); - $this->assertArrayHasKey('sig', $params); // assuming Signature adds 'sig' + $this->assertArrayHasKey('sig', $params); } } \ No newline at end of file diff --git a/test/Client/Credentials/Handler/SignatureBodyHandlerTest.php b/test/Client/Credentials/Handler/SignatureBodyHandlerTest.php new file mode 100644 index 00000000..69dee603 --- /dev/null +++ b/test/Client/Credentials/Handler/SignatureBodyHandlerTest.php @@ -0,0 +1,36 @@ + 'application/json'], + json_encode(['foo' => 'bar']) + ); + + $credentials = new SignatureSecret('secret', 'sha256'); + + $handler = new SignatureBodyHandler(); + $authRequest = $handler($request, $credentials); + $authRequest->getBody()->rewind(); + $newBody = $authRequest->getBody()->getContents(); + $newBodyArray = json_decode($newBody, true); + + $this->assertIsArray($newBodyArray); + $this->assertArrayHasKey('foo', $newBodyArray); + $this->assertEquals('bar', $newBodyArray['foo']); + $this->assertArrayHasKey('sig', $newBodyArray); + } +} \ No newline at end of file diff --git a/test/Client/Credentials/Handler/SignatureQueryHandlerTest.php b/test/Client/Credentials/Handler/SignatureQueryHandlerTest.php new file mode 100644 index 00000000..4340c651 --- /dev/null +++ b/test/Client/Credentials/Handler/SignatureQueryHandlerTest.php @@ -0,0 +1,30 @@ + 'application/json'], + json_encode(['foo' => 'bar']) + ); + + $credentials = new SignatureSecret('secret', 'sha256'); + + $handler = new SignatureQueryHandler(); + $authRequest = $handler($request, $credentials); + + $this->assertStringContainsString('sig', $authRequest->getUri()->getQuery()); + $this->assertStringContainsString('timestamp', $authRequest->getUri()->getQuery()); + } +} \ No newline at end of file diff --git a/test/Client/Credentials/Handler/TokenBodyHandlerTest.php b/test/Client/Credentials/Handler/TokenBodyHandlerTest.php new file mode 100644 index 00000000..2709300d --- /dev/null +++ b/test/Client/Credentials/Handler/TokenBodyHandlerTest.php @@ -0,0 +1,37 @@ + 'application/json'], + json_encode(['foo' => 'bar']) + ); + + $credentials = new Basic('secret', 'sha256'); + + $handler = new TokenBodyHandler(); + $authRequest = $handler($request, $credentials); + $authRequest->getBody()->rewind(); + $newBody = $authRequest->getBody()->getContents(); + $newBodyArray = json_decode($newBody, true); + + $this->assertIsArray($newBodyArray); + $this->assertArrayHasKey('foo', $newBodyArray); + $this->assertArrayHasKey('api_key', $newBodyArray); + $this->assertArrayHasKey('api_secret', $newBodyArray); + $this->assertEquals('bar', $newBodyArray['foo']); + } +} \ No newline at end of file diff --git a/test/Client/Credentials/Handler/TokenQueryHandlerTest.php b/test/Client/Credentials/Handler/TokenQueryHandlerTest.php new file mode 100644 index 00000000..681ab3bf --- /dev/null +++ b/test/Client/Credentials/Handler/TokenQueryHandlerTest.php @@ -0,0 +1,31 @@ + 'application/json'], + json_encode(['foo' => 'bar']) + ); + + $credentials = new Basic('secret', 'sha256'); + + $handler = new TokenQueryHandler(); + $authRequest = $handler($request, $credentials); + + $this->assertStringContainsString('api_key', $authRequest->getUri()->getQuery()); + $this->assertStringContainsString('api_secret', $authRequest->getUri()->getQuery()); + } +} \ No newline at end of file diff --git a/test/Client/Exception/ConflictTest.php b/test/Client/Exception/ConflictTest.php new file mode 100644 index 00000000..eb803730 --- /dev/null +++ b/test/Client/Exception/ConflictTest.php @@ -0,0 +1,33 @@ +expectException(Conflict::class); + + throw new Conflict('Conflict occurred'); + } + + public function testExceptionMessage(): void + { + $message = 'Conflict occurred'; + $exception = new Conflict($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new Conflict('Conflict occurred'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/CredentialsTest.php b/test/Client/Exception/CredentialsTest.php new file mode 100644 index 00000000..c19adb4c --- /dev/null +++ b/test/Client/Exception/CredentialsTest.php @@ -0,0 +1,33 @@ +expectException(Credentials::class); + + throw new Credentials('You are not authorised to perform this request'); + } + + public function testExceptionMessage(): void + { + $message = 'You are not authorised to perform this request'; + $exception = new Credentials($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new Credentials('You are not authorised to perform this request'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/NotFoundTest.php b/test/Client/Exception/NotFoundTest.php new file mode 100644 index 00000000..6262f7dd --- /dev/null +++ b/test/Client/Exception/NotFoundTest.php @@ -0,0 +1,33 @@ +expectException(NotFound::class); + + throw new NotFound('You are not authorised to perform this request'); + } + + public function testExceptionMessage(): void + { + $message = 'You are not authorised to perform this request'; + $exception = new NotFound($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new NotFound('You are not authorised to perform this request'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/RequestTest.php b/test/Client/Exception/RequestTest.php new file mode 100644 index 00000000..1e013553 --- /dev/null +++ b/test/Client/Exception/RequestTest.php @@ -0,0 +1,33 @@ +expectException(Request::class); + + throw new Request('You are not authorised to perform this request'); + } + + public function testExceptionMessage(): void + { + $message = 'You are not authorised to perform this request'; + $exception = new Request($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new Request('You are not authorised to perform this request'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/ServerTest.php b/test/Client/Exception/ServerTest.php new file mode 100644 index 00000000..4ba05f9a --- /dev/null +++ b/test/Client/Exception/ServerTest.php @@ -0,0 +1,33 @@ +expectException(Request::class); + + throw new Request('No results found'); + } + + public function testExceptionMessage(): void + { + $message = 'No results found'; + $exception = new Request($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new Request('No results found'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/TransportTest.php b/test/Client/Exception/TransportTest.php new file mode 100644 index 00000000..30489c30 --- /dev/null +++ b/test/Client/Exception/TransportTest.php @@ -0,0 +1,33 @@ +expectException(Request::class); + + throw new Request('Response was empty'); + } + + public function testExceptionMessage(): void + { + $message = 'Response was empty'; + $exception = new Request($message); + + $this->assertSame($message, $exception->getMessage()); + } + + public function testExceptionIsInstanceOfBaseException(): void + { + $exception = new Request('Response was empty'); + + $this->assertInstanceOf(\Exception::class, $exception); + } +} diff --git a/test/Client/Exception/Validation.php b/test/Client/Exception/ValidationTest.php similarity index 79% rename from test/Client/Exception/Validation.php rename to test/Client/Exception/ValidationTest.php index 2325bf18..8f2bedb2 100644 --- a/test/Client/Exception/Validation.php +++ b/test/Client/Exception/ValidationTest.php @@ -6,8 +6,9 @@ use PHPUnit\Framework\TestCase; use Vonage\Client\Exception\Exception; +use Vonage\Client\Exception\Validation; -class Validation extends TestCase +class ValidationTest extends TestCase { public function testValidationException() { @@ -20,17 +21,9 @@ public function testValidationException() ]; $exception = new Validation($message, $code, $previous, $errors); - - // Assert the exception message $this->assertEquals($message, $exception->getMessage()); - - // Assert the exception code $this->assertEquals($code, $exception->getCode()); - - // Assert the previous exception $this->assertSame($previous, $exception->getPrevious()); - - // Assert the validation errors $this->assertEquals($errors, $exception->getValidationErrors()); } } diff --git a/test/SMS/ExceptionErrorHandlerTest.php b/test/SMS/ExceptionErrorHandlerTest.php index f5b2bffb..0e8f242f 100644 --- a/test/SMS/ExceptionErrorHandlerTest.php +++ b/test/SMS/ExceptionErrorHandlerTest.php @@ -9,7 +9,7 @@ use Laminas\Diactoros\ResponseFactory; use VonageTest\VonageTestCase; use Vonage\Client\Exception\Request as RequestException; -use Vonage\Client\Exception\Server as ServerException; +use Vonage\Client\Exception\Request as ServerException; use Vonage\Client\Exception\ThrottleException; use Vonage\SMS\ExceptionErrorHandler; diff --git a/test/Verify/VerificationTest.php b/test/Verify/VerificationTest.php index 37b74f5d..cf548161 100644 --- a/test/Verify/VerificationTest.php +++ b/test/Verify/VerificationTest.php @@ -11,7 +11,7 @@ use Psr\Http\Client\ClientExceptionInterface; use Vonage\Client\Exception\Exception as ClientException; use Vonage\Client\Exception\Request as RequestException; -use Vonage\Client\Exception\Server as ServerException; +use Vonage\Client\Exception\Request as ServerException; use Vonage\Verify\Check; use Vonage\Verify\Client as VerifyClient; use Vonage\Verify\Verification;