Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracts PSR-17/18 discovery integration and unit tests functionality #3

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require-dev": {
"doctrine/coding-standard": "^9.0",
"laminas/laminas-diactoros": "^2.8",
"php-http/curl-client": "^2.2",
"php-http/mock-client": "^1.5",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16.1",
Expand Down
71 changes: 70 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions src/Postmark/ClientBehaviour/Discovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Postmark\ClientBehaviour;

use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Postmark\Exception\DiscoveryFailure;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Throwable;

/** @internal Postmark */
trait Discovery
{
private static function resolveHttpClient(?ClientInterface $client): ClientInterface
{
if ($client) {
return $client;
}

try {
return Psr18ClientDiscovery::find();
} catch (Throwable $error) {
throw DiscoveryFailure::clientDiscoveryFailed($error);
}
}

private static function resolveRequestFactory(): RequestFactoryInterface
{
try {
return Psr17FactoryDiscovery::findRequestFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::requestFactoryDiscoveryFailed($error);
}
}

private static function resolveStreamFactory(): StreamFactoryInterface
{
try {
return Psr17FactoryDiscovery::findStreamFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::streamFactoryDiscoveryFailed($error);
}
}

private static function resolveUriFactory(): UriFactoryInterface
{
try {
return Psr17FactoryDiscovery::findUriFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::uriFactoryDiscoveryFailed($error);
}
}
}
47 changes: 3 additions & 44 deletions src/Postmark/PostmarkClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
namespace Postmark;

use Fig\Http\Message\RequestMethodInterface;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Postmark\Exception\DiscoveryFailure;
use Postmark\ClientBehaviour\Discovery;
use Postmark\Exception\PostmarkException;
use Postmark\Exception\RequestFailure;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Throwable;

use function array_filter;
use function http_build_query;
Expand All @@ -34,6 +31,8 @@
/** @internal Postmark */
abstract class PostmarkClientBase
{
use Discovery;

private ClientInterface $client;
private RequestFactoryInterface $requestFactory;
private UriFactoryInterface $uriFactory;
Expand All @@ -55,46 +54,6 @@ protected function __construct(
/** @return non-empty-string */
abstract protected function authorizationHeaderName(): string;

private static function resolveHttpClient(?ClientInterface $client): ClientInterface
{
if ($client) {
return $client;
}

try {
return Psr18ClientDiscovery::find();
} catch (Throwable $error) {
throw DiscoveryFailure::clientDiscoveryFailed($error);
}
}

private static function resolveRequestFactory(): RequestFactoryInterface
{
try {
return Psr17FactoryDiscovery::findRequestFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::requestFactoryDiscoveryFailed($error);
}
}

private static function resolveStreamFactory(): StreamFactoryInterface
{
try {
return Psr17FactoryDiscovery::findStreamFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::streamFactoryDiscoveryFailed($error);
}
}

private static function resolveUriFactory(): UriFactoryInterface
{
try {
return Psr17FactoryDiscovery::findUriFactory();
} catch (Throwable $error) {
throw DiscoveryFailure::uriFactoryDiscoveryFailed($error);
}
}

public function baseUri(): UriInterface
{
return $this->uriFactory->createUri($this->baseUri);
Expand Down
93 changes: 93 additions & 0 deletions tests/Unit/ClientBehaviour/DiscoveryIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Postmark\Tests\Unit\ClientBehaviour;

use Http\Client\Curl\Client;
use Http\Discovery\ClassDiscovery;
use PHPUnit\Framework\TestCase;
use Postmark\Exception\DiscoveryFailure;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

class DiscoveryIntegrationTest extends TestCase
{
private DiscoveryStub $stub;
/** @var array<array-key, mixed> */
private static array $strategyBackup;

protected function setUp(): void
{
parent::setUp();
$this->stub = new DiscoveryStub();

self::$strategyBackup = ClassDiscovery::getStrategies();
}

protected function tearDown(): void
{
parent::tearDown();
ClassDiscovery::setStrategies(self::$strategyBackup);
}

public function testAClientCanBeDiscovered(): void
{
self::assertInstanceOf(ClientInterface::class, $this->stub->client(null));
}

public function testStreamFactoryCanBeDiscovered(): void
{
self::assertInstanceOf(StreamFactoryInterface::class, $this->stub->stream());
}

public function testUriFactoryCanBeDiscovered(): void
{
self::assertInstanceOf(UriFactoryInterface::class, $this->stub->uri());
}

public function testRequestFactoryCanBeDiscovered(): void
{
self::assertInstanceOf(RequestFactoryInterface::class, $this->stub->request());
}

public function testTheSameClientIsReturnedWhenGivenAsAnArgument(): void
{
$client = new Client();
self::assertSame($client, $this->stub->client($client));
}

public function testExceptionThrownWhenClientCannotBeDiscovered(): void
{
ClassDiscovery::setStrategies([]);
$this->expectException(DiscoveryFailure::class);
$this->expectExceptionMessage('A PSR-18 HTTP Client could not be discovered');
$this->stub->client(null);
}

public function testExceptionThrownWhenStreamFactoryCannotBeDiscovered(): void
{
ClassDiscovery::setStrategies([]);
$this->expectException(DiscoveryFailure::class);
$this->expectExceptionMessage('A PSR-17 Stream Factory implementation');
$this->stub->stream();
}

public function testExceptionThrownWhenUriFactoryCannotBeDiscovered(): void
{
ClassDiscovery::setStrategies([]);
$this->expectException(DiscoveryFailure::class);
$this->expectExceptionMessage('A PSR-17 URI Factory implementation');
$this->stub->uri();
}

public function testExceptionThrownWhenRequestFactoryCannotBeDiscovered(): void
{
ClassDiscovery::setStrategies([]);
$this->expectException(DiscoveryFailure::class);
$this->expectExceptionMessage('A PSR-17 Request Factory implementation');
$this->stub->request();
}
}
36 changes: 36 additions & 0 deletions tests/Unit/ClientBehaviour/DiscoveryStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Postmark\Tests\Unit\ClientBehaviour;

use Postmark\ClientBehaviour\Discovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

final class DiscoveryStub
{
use Discovery;

public function client(? ClientInterface $client): ClientInterface
{
return self::resolveHttpClient($client);
}

public function stream(): StreamFactoryInterface
{
return self::resolveStreamFactory();
}

public function request(): RequestFactoryInterface
{
return self::resolveRequestFactory();
}

public function uri(): UriFactoryInterface
{
return self::resolveUriFactory();
}
}