Skip to content

Commit 966324b

Browse files
authored
Code Coverage Improvements (#533)
* 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
1 parent ca5a904 commit 966324b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1928
-40
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ logs
55
.php_cs.cache
66
.phpunit.result.cache
77
coverage.xml
8-
**/.DS_Store
8+
**/.DS_Store
9+
coverage
10+
coverage.txt

phpunit.xml.dist

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<include>
1616
<directory suffix=".php">src</directory>
1717
</include>
18+
<report>
19+
<html outputDirectory="coverage"/>
20+
</report>
1821
</coverage>
1922
<testsuites>
2023
<testsuite name="default">
@@ -23,9 +26,18 @@
2326
<testsuite name="verify">
2427
<directory>test/Verify</directory>
2528
</testsuite>
29+
<testsuite name="handlers">
30+
<directory>test/Client/Credentials/Handler</directory>
31+
</testsuite>
32+
<testsuite name="account">
33+
<directory>test/Account</directory>
34+
</testsuite>
2635
<testsuite name="application">
2736
<directory>test/Application</directory>
2837
</testsuite>
38+
<testsuite name="conversion">
39+
<directory>test/Conversion</directory>
40+
</testsuite>
2941
<testsuite name="verify2">
3042
<directory>test/Verify2</directory>
3143
</testsuite>
@@ -74,6 +86,9 @@
7486
<testsuite name="users">
7587
<directory>test/Users</directory>
7688
</testsuite>
89+
<testsuite name="insights">
90+
<directory>test/Insights</directory>
91+
</testsuite>
7792
</testsuites>
7893
<php>
7994
<ini name='error_reporting' value='E_ALL' />

src/Account/ClientFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Psr\Container\ContainerInterface;
88
use Vonage\Client\APIResource;
99
use Vonage\Client\Credentials\Handler\BasicHandler;
10-
use Vonage\Client\Credentials\Handler\BasicQueryHandler;
1110

1211
class ClientFactory
1312
{

src/Client.php

+11
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,17 @@ public function __get($name)
544544
return $this->factory->get($name);
545545
}
546546

547+
public function getDebug(): mixed
548+
{
549+
return $this->debug;
550+
}
551+
552+
public function setDebug(mixed $debug): Client
553+
{
554+
$this->debug = $debug;
555+
return $this;
556+
}
557+
547558
/**
548559
* @deprecated Use the Verify Client, this shouldn't be here and will be removed.
549560
*/

src/Client/APIExceptionHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function setRfc7807Format(string $format): void
2525
$this->rfc7807Format = $format;
2626
}
2727

28+
public function getRfc7807Format(): string
29+
{
30+
return $this->rfc7807Format;
31+
}
32+
2833
/**
2934
* @throws Exception\Exception
3035
*

src/Client/Credentials/Handler/SignatureBodyFormHandler.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Vonage\Client\Credentials\Handler;
44

5+
use GuzzleHttp\Psr7\Utils;
56
use Psr\Http\Message\RequestInterface;
67
use Vonage\Client\Credentials\CredentialsInterface;
78
use Vonage\Client\Credentials\SignatureSecret;
@@ -17,6 +18,7 @@ public function __invoke(RequestInterface $request, CredentialsInterface $creden
1718
$body = $request->getBody();
1819
$body->rewind();
1920
$content = $body->getContents();
21+
2022
$params = [];
2123
parse_str($content, $params);
2224
$params['api_key'] = $credentialsArray['api_key'];
@@ -28,9 +30,8 @@ public function __invoke(RequestInterface $request, CredentialsInterface $creden
2830
);
2931

3032
$params = $signature->getSignedParams();
31-
$body->rewind();
32-
$body->write(http_build_query($params, '', '&'));
3333

34-
return $request;
34+
$newBody = Utils::streamFor(http_build_query($params, '', '&'));
35+
return $request->withBody($newBody);
3536
}
3637
}

src/Conversion/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function getException(ResponseInterface $response): ClientException\Ex
9797

9898
if ($status === 402) {
9999
$e = new ClientException\Request('This endpoint may need activating on your account. ' .
100-
'"Please email [email protected] for more information', $status);
100+
'Please email [email protected] for more information', $status);
101101
} elseif ($status >= 400 && $status < 500) {
102102
$e = new ClientException\Request($body['error_title'], $status);
103103
} elseif ($status >= 500 && $status < 600) {

test/Account/ClientFactoryTest.php

+16-23
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,30 @@
44

55
namespace VonageTest\Account;
66

7-
use VonageTest\VonageTestCase;
8-
use Vonage\Account\ClientFactory;
7+
use PHPUnit\Framework\TestCase;
98
use Vonage\Client;
109
use Vonage\Client\APIResource;
1110
use Vonage\Client\Factory\MapFactory;
11+
use Vonage\Account\ClientFactory;
1212

13-
class ClientFactoryTest extends VonageTestCase
13+
class ClientFactoryTest extends TestCase
1414
{
15-
/**
16-
* @var MapFactory
17-
*/
18-
protected $mapFactory;
19-
20-
protected $vonageClient;
21-
22-
public function setUp(): void
15+
public function testInvokeCreatesClientWithConfiguredApiResource(): void
2316
{
24-
$this->vonageClient = $this->prophesize(Client::class);
25-
$this->vonageClient->getRestUrl()->willReturn('https://rest.nexmo.com');
26-
$this->vonageClient->getApiUrl()->willReturn('https://api.nexmo.com');
17+
$mockServices = [
18+
'account' => ClientFactory::class,
19+
APIResource::class => APIResource::class,
20+
];
2721

28-
/** @noinspection PhpParamsInspection */
29-
$this->mapFactory = new MapFactory([APIResource::class => APIResource::class], $this->vonageClient->reveal());
30-
}
31-
32-
public function testURIsAreCorrect(): void
33-
{
22+
$mockClient = $this->createMock(Client::class);
23+
$container = new MapFactory($mockServices, $mockClient);
3424
$factory = new ClientFactory();
35-
$client = $factory($this->mapFactory);
3625

37-
$this->assertSame('/account', $client->getAPIResource()->getBaseUri());
38-
$this->assertSame('https://rest.nexmo.com', $client->getAPIResource()->getBaseUrl());
26+
$result = $factory($container);
27+
$this->assertInstanceOf(\Vonage\Account\Client::class, $result);
28+
$this->assertEquals('/account', $result->getAPIResource()->getBaseUri());
29+
$this->assertInstanceOf(Client\Credentials\Handler\BasicHandler::class, $result->getAPIResource()
30+
->getAuthHandlers()[0]);
31+
$this->assertFalse($result->getAPIResource()->isHAL());
3932
}
4033
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VonageTest\Application;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Vonage\Client;
9+
use Vonage\Client\APIResource;
10+
use Vonage\Client\Factory\MapFactory;
11+
use Vonage\Application\ClientFactory;
12+
13+
class ClientFactoryTest extends TestCase
14+
{
15+
public function testInvokeCreatesClientWithConfiguredApiResource(): void
16+
{
17+
$mockServices = [
18+
'account' => ClientFactory::class,
19+
APIResource::class => APIResource::class,
20+
];
21+
22+
$mockClient = $this->createMock(Client::class);
23+
$container = new MapFactory($mockServices, $mockClient);
24+
$factory = new ClientFactory();
25+
26+
$result = $factory($container);
27+
$this->assertInstanceOf(\Vonage\Application\Client::class, $result);
28+
$this->assertEquals('/v2/applications', $result->getAPIResource()->getBaseUri());
29+
$this->assertInstanceOf(Client\Credentials\Handler\BasicHandler::class, $result->getAPIResource()
30+
->getAuthHandlers()[0]);
31+
$this->assertEquals('applications', $result->getAPIResource()->getCollectionName());
32+
}
33+
}

test/Client/CallbackTest.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Vonage\Client\Callback;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Vonage\Client\Callback\Callback;
9+
use RuntimeException;
10+
use InvalidArgumentException;
11+
12+
class CallbackTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
// Mocking the $_GET and $_POST superglobals for testing purposes
19+
$_GET = [
20+
'key1' => 'value1',
21+
'key2' => 'value2',
22+
];
23+
24+
$_POST = [
25+
'key3' => 'value3',
26+
'key4' => 'value4',
27+
];
28+
}
29+
30+
public function testConstructorWithMissingKeys(): void
31+
{
32+
$this->expectException(RuntimeException::class);
33+
$this->expectExceptionMessage('missing expected callback keys: key1, key2');
34+
35+
$callback = new class (['key3' => 'value3']) extends Callback {
36+
protected array $expected = ['key1', 'key2'];
37+
};
38+
}
39+
40+
public function testConstructorWithAllKeys(): void
41+
{
42+
$callback = new class ([
43+
'key1' => 'value1',
44+
'key2' => 'value2',
45+
]) extends Callback {
46+
protected array $expected = ['key1', 'key2'];
47+
};
48+
49+
$this->assertSame([
50+
'key1' => 'value1',
51+
'key2' => 'value2',
52+
], $callback->getData());
53+
}
54+
55+
public function testFromEnvPost(): void
56+
{
57+
$callback = Callback::fromEnv(Callback::ENV_POST);
58+
59+
$this->assertInstanceOf(Callback::class, $callback);
60+
$this->assertSame([
61+
'key3' => 'value3',
62+
'key4' => 'value4',
63+
], $callback->getData());
64+
}
65+
66+
public function testFromEnvGet(): void
67+
{
68+
$callback = Callback::fromEnv(Callback::ENV_GET);
69+
70+
$this->assertInstanceOf(Callback::class, $callback);
71+
$this->assertSame([
72+
'key1' => 'value1',
73+
'key2' => 'value2',
74+
], $callback->getData());
75+
}
76+
77+
public function testFromEnvAll(): void
78+
{
79+
$callback = Callback::fromEnv(Callback::ENV_ALL);
80+
81+
$this->assertInstanceOf(Callback::class, $callback);
82+
$this->assertSame([
83+
'key1' => 'value1',
84+
'key2' => 'value2',
85+
'key3' => 'value3',
86+
'key4' => 'value4',
87+
], $callback->getData());
88+
}
89+
90+
public function testFromEnvInvalidSource(): void
91+
{
92+
$this->expectException(InvalidArgumentException::class);
93+
$this->expectExceptionMessage('invalid source: invalid');
94+
95+
Callback::fromEnv('invalid');
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VonageTest\Client\Credentials\Handler;
6+
7+
use GuzzleHttp\Psr7\Request;
8+
use GuzzleHttp\Psr7\Utils;
9+
use Vonage\Client\Credentials\SignatureSecret;
10+
use VonageTest\VonageTestCase;
11+
use Vonage\Client\Credentials\Handler\SignatureBodyFormHandler;
12+
13+
class SignatureBodyFormHandlerTest extends VonageTestCase
14+
{
15+
public function testSignatureBodyFormHandler()
16+
{
17+
$initialBody = http_build_query(['param1' => 'value1', 'param2' => 'value2']);
18+
19+
$request = new Request(
20+
'POST',
21+
'/test',
22+
['Content-Type' => 'application/x-www-form-urlencoded'],
23+
Utils::streamFor($initialBody)
24+
);
25+
26+
$credentials = new SignatureSecret('secret', 'sha256');
27+
28+
$handler = new SignatureBodyFormHandler();
29+
$authRequest = $handler($request, $credentials);
30+
$authRequest->getBody()->rewind();
31+
$newBody = $authRequest->getBody()->getContents();
32+
33+
parse_str($newBody, $params);
34+
35+
$this->assertArrayHasKey('api_key', $params);
36+
$this->assertArrayHasKey('sig', $params);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VonageTest\Client\Credentials\Handler;
6+
7+
use GuzzleHttp\Psr7\Request;
8+
use Vonage\Client\Credentials\Handler\SignatureBodyHandler;
9+
use Vonage\Client\Credentials\SignatureSecret;
10+
use VonageTest\VonageTestCase;
11+
12+
class SignatureBodyHandlerTest extends VonageTestCase
13+
{
14+
public function testSignatureBodyHandler(): void
15+
{
16+
$request = new Request(
17+
'POST',
18+
'/test',
19+
['Content-Type' => 'application/json'],
20+
json_encode(['foo' => 'bar'])
21+
);
22+
23+
$credentials = new SignatureSecret('secret', 'sha256');
24+
25+
$handler = new SignatureBodyHandler();
26+
$authRequest = $handler($request, $credentials);
27+
$authRequest->getBody()->rewind();
28+
$newBody = $authRequest->getBody()->getContents();
29+
$newBodyArray = json_decode($newBody, true);
30+
31+
$this->assertIsArray($newBodyArray);
32+
$this->assertArrayHasKey('foo', $newBodyArray);
33+
$this->assertEquals('bar', $newBodyArray['foo']);
34+
$this->assertArrayHasKey('sig', $newBodyArray);
35+
}
36+
}

0 commit comments

Comments
 (0)