Skip to content

Commit

Permalink
Added tests for Client builders
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Dec 6, 2021
1 parent dd72804 commit 1f191e8
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
### Added
* [#12](https://github.com/shlinkio/shlink-php-client/issues/12) Created `ShlinkClientBuilder` and `SingletonShlinkClientBuilder`, which can be used to create client instances at runtime.

### Changed
* *Nothing*

### Deprecated
* *Nothing*

### Removed
* *Nothing*

### Fixed
* *Nothing*

## [0.1.0] - 2021-12-04
### Added
* First release
Expand Down
21 changes: 21 additions & 0 deletions test/Builder/ClientBuilderMethodsProviderTrait.php
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];
}
}
}
54 changes: 54 additions & 0 deletions test/Builder/ShlinkClientBuilderTest.php
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);
}
}
57 changes: 57 additions & 0 deletions test/Builder/SingletonShlinkClientBuilderTest.php
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);
}
}

0 comments on commit 1f191e8

Please sign in to comment.