From 32b7441a46dc6613b9e891312fa064db8b8b9713 Mon Sep 17 00:00:00 2001 From: George Steel Date: Wed, 10 Jun 2020 10:48:33 +0100 Subject: [PATCH 1/2] Fix HTTP Client alias so that it's a service with a factory meaning it can then be delegated --- CHANGELOG.md | 22 ++++++++++++++++++++++ src/ConfigProvider.php | 4 +--- src/Http/PrismicHttpClientFactory.php | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/Http/PrismicHttpClientFactory.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 03677dd..c2a87db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 0.2.1 - 2020-06-10 + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- [#3](https://github.com/netglue/primo/pull/3) Fixes delegation of the Prismic specific http client by creating a factory for the client that can actually be delegated by consumers. Aliases don't trigger delegation in Laminas Service Manager which is a pain in the ass and something that is very likely we'll want to do. + ## 0.2.0 - 2020-06-10 ### Added diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 95d3f4d..01fdcfe 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -6,7 +6,6 @@ use Mezzio; use Primo\Content\Document; use Prismic; -use Psr; final class ConfigProvider { @@ -54,6 +53,7 @@ private function dependencies() : array { return [ 'factories' => [ + Http\PrismicHttpClient::class => Http\PrismicHttpClientFactory::class, Middleware\DocumentResolver::class => Middleware\Container\DocumentResolverFactory::class, Middleware\InjectRequestCookies::class => Middleware\Container\InjectRequestCookiesFactory::class, Middleware\PreviewHandler::class => Middleware\Container\PreviewHandlerFactory::class, @@ -69,8 +69,6 @@ private function dependencies() : array LinkResolver::class => Container\LinkResolverFactory::class, ], 'aliases' => [ - // Aliases our marker interface to the regular ClientInterface - Http\PrismicHttpClient::class => Psr\Http\Client\ClientInterface::class, Prismic\ResultSet\ResultSetFactory::class => Prismic\ResultSet\StandardResultSetFactory::class, // To Opt-In to Hydrating Result Sets, alias the Prismic ResultSetFactory to the Hydrating Result Set FQCN //Prismic\ResultSet\ResultSetFactory::class => ResultSet\HydratingResultSetFactory::class, diff --git a/src/Http/PrismicHttpClientFactory.php b/src/Http/PrismicHttpClientFactory.php new file mode 100644 index 0000000..f8abb75 --- /dev/null +++ b/src/Http/PrismicHttpClientFactory.php @@ -0,0 +1,20 @@ +has(ClientInterface::class)) { + return $container->get(ClientInterface::class); + } + + return Psr18ClientDiscovery::find(); + } +} From d8c432dbafd6e595ac81ab2542e59926f1a2a0f3 Mon Sep 17 00:00:00 2001 From: George Steel Date: Wed, 10 Jun 2020 11:01:16 +0100 Subject: [PATCH 2/2] Add unit test for http client factory --- .../Http/PrismicHttpClientFactoryTest.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test/Unit/Http/PrismicHttpClientFactoryTest.php diff --git a/test/Unit/Http/PrismicHttpClientFactoryTest.php b/test/Unit/Http/PrismicHttpClientFactoryTest.php new file mode 100644 index 0000000..5ab3103 --- /dev/null +++ b/test/Unit/Http/PrismicHttpClientFactoryTest.php @@ -0,0 +1,53 @@ +container = $this->createMock(ContainerInterface::class); + } + + private function clientInContainer(bool $value) : void + { + $this->container->expects($this->once()) + ->method('has') + ->with(ClientInterface::class) + ->willReturn($value); + } + + public function testThatClientInContainerWillBeReturnedWhenAvailable() : void + { + $client = $this->createMock(ClientInterface::class); + $this->clientInContainer(true); + + $this->container->expects($this->once()) + ->method('get') + ->with(ClientInterface::class) + ->willReturn($client); + + $factory = new PrismicHttpClientFactory(); + $this->assertSame($client, $factory($this->container)); + } + + public function testThatClientDiscoveryWillBeUsedWhenNoClientIsInTheContainer() : void + { + $this->clientInContainer(false); + $this->container->expects($this->never())->method('get'); + $factory = new PrismicHttpClientFactory(); + $factory($this->container); + $this->addToAssertionCount(1); + } +}