-
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.
Add unit test for http client factory
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 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
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); | ||
} | ||
} |