-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Test all client factory configurations * Ignore coverage output * Add coverage for hardcoded error handler * Fixed the Signature Body and wrote test for it * Added exception and credentials testing * More tests
- Loading branch information
Showing
53 changed files
with
1,928 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ logs | |
.php_cs.cache | ||
.phpunit.result.cache | ||
coverage.xml | ||
**/.DS_Store | ||
**/.DS_Store | ||
coverage | ||
coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VonageTest\Application; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Vonage\Client; | ||
use Vonage\Client\APIResource; | ||
use Vonage\Client\Factory\MapFactory; | ||
use Vonage\Application\ClientFactory; | ||
|
||
class ClientFactoryTest extends TestCase | ||
{ | ||
public function testInvokeCreatesClientWithConfiguredApiResource(): void | ||
{ | ||
$mockServices = [ | ||
'account' => ClientFactory::class, | ||
APIResource::class => APIResource::class, | ||
]; | ||
|
||
$mockClient = $this->createMock(Client::class); | ||
$container = new MapFactory($mockServices, $mockClient); | ||
$factory = new ClientFactory(); | ||
|
||
$result = $factory($container); | ||
$this->assertInstanceOf(\Vonage\Application\Client::class, $result); | ||
$this->assertEquals('/v2/applications', $result->getAPIResource()->getBaseUri()); | ||
$this->assertInstanceOf(Client\Credentials\Handler\BasicHandler::class, $result->getAPIResource() | ||
->getAuthHandlers()[0]); | ||
$this->assertEquals('applications', $result->getAPIResource()->getCollectionName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Vonage\Client\Callback; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Vonage\Client\Callback\Callback; | ||
use RuntimeException; | ||
use InvalidArgumentException; | ||
|
||
class CallbackTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
// Mocking the $_GET and $_POST superglobals for testing purposes | ||
$_GET = [ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
]; | ||
|
||
$_POST = [ | ||
'key3' => 'value3', | ||
'key4' => 'value4', | ||
]; | ||
} | ||
|
||
public function testConstructorWithMissingKeys(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('missing expected callback keys: key1, key2'); | ||
|
||
$callback = new class (['key3' => 'value3']) extends Callback { | ||
protected array $expected = ['key1', 'key2']; | ||
}; | ||
} | ||
|
||
public function testConstructorWithAllKeys(): void | ||
{ | ||
$callback = new class ([ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
]) extends Callback { | ||
protected array $expected = ['key1', 'key2']; | ||
}; | ||
|
||
$this->assertSame([ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
], $callback->getData()); | ||
} | ||
|
||
public function testFromEnvPost(): void | ||
{ | ||
$callback = Callback::fromEnv(Callback::ENV_POST); | ||
|
||
$this->assertInstanceOf(Callback::class, $callback); | ||
$this->assertSame([ | ||
'key3' => 'value3', | ||
'key4' => 'value4', | ||
], $callback->getData()); | ||
} | ||
|
||
public function testFromEnvGet(): void | ||
{ | ||
$callback = Callback::fromEnv(Callback::ENV_GET); | ||
|
||
$this->assertInstanceOf(Callback::class, $callback); | ||
$this->assertSame([ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
], $callback->getData()); | ||
} | ||
|
||
public function testFromEnvAll(): void | ||
{ | ||
$callback = Callback::fromEnv(Callback::ENV_ALL); | ||
|
||
$this->assertInstanceOf(Callback::class, $callback); | ||
$this->assertSame([ | ||
'key1' => 'value1', | ||
'key2' => 'value2', | ||
'key3' => 'value3', | ||
'key4' => 'value4', | ||
], $callback->getData()); | ||
} | ||
|
||
public function testFromEnvInvalidSource(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('invalid source: invalid'); | ||
|
||
Callback::fromEnv('invalid'); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/Client/Credentials/Handler/SignatureBodyFormHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VonageTest\Client\Credentials\Handler; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Utils; | ||
use Vonage\Client\Credentials\SignatureSecret; | ||
use VonageTest\VonageTestCase; | ||
use Vonage\Client\Credentials\Handler\SignatureBodyFormHandler; | ||
|
||
class SignatureBodyFormHandlerTest extends VonageTestCase | ||
{ | ||
public function testSignatureBodyFormHandler() | ||
{ | ||
$initialBody = http_build_query(['param1' => 'value1', 'param2' => 'value2']); | ||
|
||
$request = new Request( | ||
'POST', | ||
'/test', | ||
['Content-Type' => 'application/x-www-form-urlencoded'], | ||
Utils::streamFor($initialBody) | ||
); | ||
|
||
$credentials = new SignatureSecret('secret', 'sha256'); | ||
|
||
$handler = new SignatureBodyFormHandler(); | ||
$authRequest = $handler($request, $credentials); | ||
$authRequest->getBody()->rewind(); | ||
$newBody = $authRequest->getBody()->getContents(); | ||
|
||
parse_str($newBody, $params); | ||
|
||
$this->assertArrayHasKey('api_key', $params); | ||
$this->assertArrayHasKey('sig', $params); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
test/Client/Credentials/Handler/SignatureBodyHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.