forked from shlinkio/shlink-php-sdk
-
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.
- Loading branch information
Showing
4 changed files
with
148 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
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\Builder; | ||
|
||
use ReflectionClass; | ||
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilderInterface; | ||
|
||
trait ClientBuilderMethodsProviderTrait | ||
{ | ||
public function provideMethods(): iterable | ||
{ | ||
$ref = new ReflectionClass(ShlinkClientBuilderInterface::class); | ||
|
||
foreach ($ref->getMethods() as $method) { | ||
$name = $method->getName(); | ||
yield $name => [$name]; | ||
} | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilder; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfig; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
|
||
class ShlinkClientBuilderTest extends TestCase | ||
{ | ||
use ClientBuilderMethodsProviderTrait; | ||
use ProphecyTrait; | ||
|
||
private ShlinkClientBuilder $builder; | ||
private ObjectProphecy $client; | ||
private ObjectProphecy $requestFactory; | ||
private ObjectProphecy $streamFactory; | ||
private ShlinkConfigInterface $config; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = $this->prophesize(ClientInterface::class); | ||
$this->requestFactory = $this->prophesize(RequestFactoryInterface::class); | ||
$this->streamFactory = $this->prophesize(StreamFactoryInterface::class); | ||
|
||
$this->builder = new ShlinkClientBuilder( | ||
$this->client->reveal(), | ||
$this->requestFactory->reveal(), | ||
$this->streamFactory->reveal(), | ||
); | ||
$this->config = ShlinkConfig::fromBaseUrlAndApiKey('foo', 'bar'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideMethods | ||
*/ | ||
public function buildClientReturnsAlwaysNewInstances(string $method): void | ||
{ | ||
$instance1 = $this->builder->{$method}($this->config); | ||
$instance2 = $this->builder->{$method}($this->config); | ||
|
||
self::assertEquals($instance1, $instance2); | ||
self::assertNotSame($instance1, $instance2); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilderInterface; | ||
use Shlinkio\Shlink\SDK\Builder\SingletonShlinkClientBuilder; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfig; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
use Shlinkio\Shlink\SDK\ShlinkClient; | ||
|
||
class SingletonShlinkClientBuilderTest extends TestCase | ||
{ | ||
use ClientBuilderMethodsProviderTrait; | ||
use ProphecyTrait; | ||
|
||
private SingletonShlinkClientBuilder $builder; | ||
private ObjectProphecy $wrapped; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->wrapped = $this->prophesize(ShlinkClientBuilderInterface::class); | ||
$this->builder = new SingletonShlinkClientBuilder($this->wrapped->reveal()); | ||
$this->config = ShlinkConfig::fromBaseUrlAndApiKey('foo', 'bar'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideMethods | ||
*/ | ||
public function buildClientReturnsAlwaysNewInstances(string $method): void | ||
{ | ||
$call = $this->wrapped->__call($method, [Argument::type(ShlinkConfigInterface::class)])->willReturn( | ||
$this->prophesize(ShlinkClient::class)->reveal(), | ||
); | ||
|
||
$configOne = ShlinkConfig::fromBaseUrlAndApiKey('foo', 'bar'); | ||
$instance1 = $this->builder->{$method}($configOne); | ||
$instance2 = $this->builder->{$method}($configOne); | ||
self::assertSame($instance1, $instance2); | ||
|
||
$configTwo = ShlinkConfig::fromBaseUrlAndApiKey('bar', 'foo'); | ||
$instance1 = $this->builder->{$method}($configTwo); | ||
$instance2 = $this->builder->{$method}($configTwo); | ||
$instance3 = $this->builder->{$method}($configTwo); | ||
self::assertSame($instance1, $instance2); | ||
self::assertSame($instance1, $instance3); | ||
self::assertSame($instance2, $instance3); | ||
|
||
$call->shouldHaveBeenCalledTimes(2); | ||
} | ||
} |