-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix HTTP Client alias so that it's a service with a factory meaning it can then be delegated
- Loading branch information
Showing
4 changed files
with
96 additions
and
3 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
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,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Primo\Http; | ||
|
||
use Http\Discovery\Psr18ClientDiscovery; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
|
||
class PrismicHttpClientFactory | ||
{ | ||
public function __invoke(ContainerInterface $container) : ClientInterface | ||
{ | ||
if ($container->has(ClientInterface::class)) { | ||
return $container->get(ClientInterface::class); | ||
} | ||
|
||
return Psr18ClientDiscovery::find(); | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PrimoTest\Unit\Http; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Primo\Http\PrismicHttpClientFactory; | ||
use PrimoTest\Unit\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
|
||
class PrismicHttpClientFactoryTest extends TestCase | ||
{ | ||
/** @var MockObject|ClientInterface */ | ||
private $container; | ||
|
||
protected function setUp() : void | ||
{ | ||
parent::setUp(); | ||
$this->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); | ||
} | ||
} |