Skip to content

Commit

Permalink
Added exception and credentials testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Jan 31, 2025
1 parent b76ac90 commit 07a5557
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 14 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<testsuite name="verify">
<directory>test/Verify</directory>
</testsuite>
<testsuite name="handlers">
<directory>test/Client/Credentials/Handler</directory>
</testsuite>
<testsuite name="account">
<directory>test/Account</directory>
</testsuite>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
36 changes: 36 additions & 0 deletions test/Client/Credentials/Handler/SignatureBodyHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Request;
use Vonage\Client\Credentials\Handler\SignatureBodyHandler;
use Vonage\Client\Credentials\SignatureSecret;
use VonageTest\VonageTestCase;

class SignatureBodyHandlerTest extends VonageTestCase
{
public function testSignatureBodyHandler(): void
{
$request = new Request(
'POST',
'/test',
['Content-Type' => '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);
}
}
30 changes: 30 additions & 0 deletions test/Client/Credentials/Handler/SignatureQueryHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Request;
use Vonage\Client\Credentials\Handler\SignatureQueryHandler;
use Vonage\Client\Credentials\SignatureSecret;
use VonageTest\VonageTestCase;
class SignatureQueryHandlerTest extends VonageTestCase
{
public function testSignatureQueryHandler(): void
{
$request = new Request(
'POST',
'/test',
['Content-Type' => '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());
}
}
37 changes: 37 additions & 0 deletions test/Client/Credentials/Handler/TokenBodyHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Request;
use Vonage\Client\Credentials\Basic;
use Vonage\Client\Credentials\Handler\TokenBodyHandler;
use VonageTest\VonageTestCase;

class TokenBodyHandlerTest extends VonageTestCase
{
public function testTokenBodyHandler(): void
{
$request = new Request(
'POST',
'/test',
['Content-Type' => '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']);
}
}
31 changes: 31 additions & 0 deletions test/Client/Credentials/Handler/TokenQueryHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Credentials\Handler;

use GuzzleHttp\Psr7\Request;
use Vonage\Client\Credentials\Basic;
use Vonage\Client\Credentials\Handler\TokenQueryHandler;
use VonageTest\VonageTestCase;

class TokenQueryHandlerTest extends VonageTestCase
{
public function testTokenBodyHandler(): void
{
$request = new Request(
'POST',
'/test',
['Content-Type' => '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());
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/ConflictTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\Conflict;

class ConflictTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/CredentialsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\Credentials;

class CredentialsTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/NotFoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\NotFound;

class NotFoundTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\Request;

class RequestTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/ServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\Request;

class ServerTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
33 changes: 33 additions & 0 deletions test/Client/Exception/TransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VonageTest\Client\Exception;

use PHPUnit\Framework\TestCase;
use Vonage\Client\Exception\Request;

class TransportTest extends TestCase
{
public function testExceptionCanBeThrown(): void
{
$this->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);
}
}
Loading

0 comments on commit 07a5557

Please sign in to comment.