Skip to content

Commit

Permalink
Test all client factory configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Jan 30, 2025
1 parent ca5a904 commit 918e266
Show file tree
Hide file tree
Showing 33 changed files with 1,347 additions and 24 deletions.
12 changes: 12 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<html outputDirectory="coverage"/>
</report>
</coverage>
<testsuites>
<testsuite name="default">
Expand All @@ -23,9 +26,15 @@
<testsuite name="verify">
<directory>test/Verify</directory>
</testsuite>
<testsuite name="account">
<directory>test/Account</directory>
</testsuite>
<testsuite name="application">
<directory>test/Application</directory>
</testsuite>
<testsuite name="conversion">
<directory>test/Conversion</directory>
</testsuite>
<testsuite name="verify2">
<directory>test/Verify2</directory>
</testsuite>
Expand Down Expand Up @@ -74,6 +83,9 @@
<testsuite name="users">
<directory>test/Users</directory>
</testsuite>
<testsuite name="insights">
<directory>test/Insights</directory>
</testsuite>
</testsuites>
<php>
<ini name='error_reporting' value='E_ALL' />
Expand Down
1 change: 0 additions & 1 deletion src/Account/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Psr\Container\ContainerInterface;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\BasicHandler;
use Vonage\Client\Credentials\Handler\BasicQueryHandler;

class ClientFactory
{
Expand Down
11 changes: 11 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,17 @@ public function __get($name)
return $this->factory->get($name);
}

public function getDebug(): mixed
{
return $this->debug;
}

public function setDebug(mixed $debug): Client
{
$this->debug = $debug;
return $this;
}

/**
* @deprecated Use the Verify Client, this shouldn't be here and will be removed.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Client/APIExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function setRfc7807Format(string $format): void
$this->rfc7807Format = $format;
}

public function getRfc7807Format(): string
{
return $this->rfc7807Format;
}

/**
* @throws Exception\Exception
*
Expand Down
39 changes: 16 additions & 23 deletions test/Account/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@

namespace VonageTest\Account;

use VonageTest\VonageTestCase;
use Vonage\Account\ClientFactory;
use PHPUnit\Framework\TestCase;
use Vonage\Client;
use Vonage\Client\APIResource;
use Vonage\Client\Factory\MapFactory;
use Vonage\Account\ClientFactory;

class ClientFactoryTest extends VonageTestCase
class ClientFactoryTest extends TestCase
{
/**
* @var MapFactory
*/
protected $mapFactory;

protected $vonageClient;

public function setUp(): void
public function testInvokeCreatesClientWithConfiguredApiResource(): void
{
$this->vonageClient = $this->prophesize(Client::class);
$this->vonageClient->getRestUrl()->willReturn('https://rest.nexmo.com');
$this->vonageClient->getApiUrl()->willReturn('https://api.nexmo.com');
$mockServices = [
'account' => ClientFactory::class,
APIResource::class => APIResource::class,
];

/** @noinspection PhpParamsInspection */
$this->mapFactory = new MapFactory([APIResource::class => APIResource::class], $this->vonageClient->reveal());
}

public function testURIsAreCorrect(): void
{
$mockClient = $this->createMock(Client::class);
$container = new MapFactory($mockServices, $mockClient);
$factory = new ClientFactory();
$client = $factory($this->mapFactory);

$this->assertSame('/account', $client->getAPIResource()->getBaseUri());
$this->assertSame('https://rest.nexmo.com', $client->getAPIResource()->getBaseUrl());
$result = $factory($container);
$this->assertInstanceOf(\Vonage\Account\Client::class, $result);
$this->assertEquals('/account', $result->getAPIResource()->getBaseUri());
$this->assertInstanceOf(Client\Credentials\Handler\BasicHandler::class, $result->getAPIResource()
->getAuthHandlers()[0]);
$this->assertFalse($result->getAPIResource()->isHAL());
}
}
33 changes: 33 additions & 0 deletions test/Application/ClientFactoryTest.php
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());
}
}
97 changes: 97 additions & 0 deletions test/Client/CallbackTest.php
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');
}
}
89 changes: 89 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace VonageTest;

use Lcobucci\JWT\Token\Plain;
use Psr\Http\Client\ClientInterface;
use RuntimeException;
use Vonage\Client\Credentials\CredentialsInterface;
use VonageTest\VonageTestCase;
use Vonage\Client;
use Vonage\Client\Credentials\Basic;
Expand All @@ -22,4 +25,90 @@ public function testCallingVideoWithoutPackageGeneratesRuntimeError(): void
$client = new Client(new Basic('abcd', '1234'));
$video = $client->video();
}

public function testConstructorWithValidClient()
{
$credentials = $this->createMock(Basic::class);
$httpClient = $this->createMock(ClientInterface::class);
$options = ['debug' => true];

$client = new Client($credentials, $options, $httpClient);

$this->assertInstanceOf(Client::class, $client);
$this->assertTrue($client->getDebug());
}

public function testConstructorWithoutHttpClientUsesDefault()
{
$credentials = $this->createMock(Basic::class);
$options = ['debug' => true];

$client = new Client($credentials, $options);

$this->assertInstanceOf(Client::class, $client);
$this->assertInstanceOf(ClientInterface::class, $client->getHttpClient());
}

public function testConstructorThrowsExceptionOnInvalidCredentials()
{
$invalidCredentials = $this->createMock(CredentialsInterface::class);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('unknown credentials type');

new Client($invalidCredentials);
}

public function testConstructorWithCustomOptions()
{
$credentials = $this->createMock(Basic::class);
$options = [
'base_rest_url' => 'https://example-rest.com',
'base_api_url' => 'https://example-api.com',
'debug' => true
];

$client = new Client($credentials, $options);

$this->assertEquals('https://example-rest.com', $client->getRestUrl());
$this->assertEquals('https://example-api.com', $client->getApiUrl());
$this->assertTrue($client->getDebug());
}

public function testConstructorHandlesDeprecationsOption()
{
$credentials = $this->createMock(Basic::class);
$options = ['show_deprecations' => true];

$client = new Client($credentials, $options);

// No specific assertion for error handler setup, but ensuring no exceptions occurred.
$this->assertInstanceOf(Client::class, $client);
}

public function testConstructorHandlesVideoClientFactory()
{
$credentials = $this->createMock(Basic::class);

if (class_exists('Vonage\Video\ClientFactory')) {
$this->markTestSkipped('Vonage Video ClientFactory class is available.');
}

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Please install @vonage/video to use the Video API');

$client = new Client($credentials);
$client->video();
}

public function testWillGenerateJwt()
{
$keyPath = __DIR__ . '/Client/Credentials/test.key';
$keyContents = file_get_contents($keyPath);
$credentials = new Client\Credentials\Keypair($keyContents, 'abc123');
$client = new Client($credentials);
$jwt = $client->generateJwt();

$this->assertInstanceOf(Plain::class, $jwt);
}
}
34 changes: 34 additions & 0 deletions test/Conversation/ClientFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace VonageTest\Conversation;

use PHPUnit\Framework\TestCase;
use Vonage\Client;
use Vonage\Client\APIResource;
use Vonage\Client\Factory\MapFactory;
use Vonage\Conversation\ClientFactory;

class ClientFactoryTest extends TestCase
{
public function testInvokeCreatesClientWithConfiguredApiResource(): void
{
$mockServices = [
'conversation' => 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\Conversation\Client::class, $result);
$this->assertEquals('https://api.nexmo.com/v1/conversations', $result->getAPIResource()->getBaseUrl());
$this->assertInstanceOf(Client\Credentials\Handler\KeypairHandler::class, $result->getAPIResource()
->getAuthHandlers()[0]);
$this->assertFalse($result->getAPIResource()->errorsOn200());
$this->assertTrue($result->getAPIResource()->isHAL());
}
}
Loading

0 comments on commit 918e266

Please sign in to comment.