Skip to content

Commit

Permalink
Add unit test for http client factory
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteel committed Jun 10, 2020
1 parent 32b7441 commit d8c432d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/Unit/Http/PrismicHttpClientFactoryTest.php
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);
}
}

0 comments on commit d8c432d

Please sign in to comment.