Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/AsyncClient.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);

namespace ApiClients\Client\Travis;

use ApiClients\Client\Travis\CommandBus\Command;
use ApiClients\Client\Travis\Resource\HookInterface;
use ApiClients\Foundation\Client;
use ApiClients\Foundation\ClientInterface;
use ApiClients\Foundation\Factory;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
Expand All @@ -16,26 +16,26 @@
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
use function React\Promise\resolve;

class AsyncClient
final class AsyncClient
{
/**
* @var Client
* @var ClientInterface
*/
protected $client;

/**
* @param LoopInterface $loop
* @param string $token
* @param array $options
* @param Client|null $client
* @param ClientInterface|null $client
*/
public function __construct(
LoopInterface $loop,
string $token = '',
array $options = [],
Client $client = null
ClientInterface $client = null
) {
if (!($client instanceof Client)) {
if (!($client instanceof ClientInterface)) {
$this->options = ApiSettings::getOptions($token, 'Async', $options);
$client = Factory::create($loop, $this->options);
}
Expand Down
100 changes: 100 additions & 0 deletions tests/AsyncClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Travis;

use ApiClients\Client\Travis\AsyncClient;
use ApiClients\Client\Travis\CommandBus\Command;
use ApiClients\Foundation\ClientInterface;
use ApiClients\Tools\TestUtilities\TestCase;
use Prophecy\Argument;
use React\EventLoop\Factory;
use function Clue\React\Block\await;
use function React\Promise\resolve;
use Rx\React\Promise;

final class AsyncClientTest extends TestCase
{
public function testRepository()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::which('getRepository', 'php-api-clients/travis')
)->shouldBeCalled()->willReturn(resolve($expected));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await($asyncClient->repository('php-api-clients/travis'), $loop);
self::assertSame($expected, $result);
}

public function testUser()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::type(Command\UserCommand::class)
)->shouldBeCalled()->willReturn(resolve($expected));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await($asyncClient->user(), $loop);
self::assertSame($expected, $result);
}

public function testSshKey()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::which('getId', 123)
)->shouldBeCalled()->willReturn(resolve($expected));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await($asyncClient->sshKey(123), $loop);
self::assertSame($expected, $result);
}

public function testHooks()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::type(Command\HooksCommand::class)
)->shouldBeCalled()->willReturn(resolve(Promise::toObservable(resolve($expected))));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await(Promise::fromObservable($asyncClient->hooks()), $loop);
self::assertSame($expected, $result);
}

public function testAccounts()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::type(Command\AccountsCommand::class)
)->shouldBeCalled()->willReturn(resolve(Promise::toObservable(resolve($expected))));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await(Promise::fromObservable($asyncClient->accounts()), $loop);
self::assertSame($expected, $result);
}

public function testBroadcasts()
{
$expected = 'foo.bar';
$loop = Factory::create();
$client = $this->prophesize(ClientInterface::class);
$client->handle(
Argument::type(Command\BroadcastsCommand::class)
)->shouldBeCalled()->willReturn(resolve(Promise::toObservable(resolve($expected))));

$asyncClient = new AsyncClient($loop, 'token', [], $client->reveal());
$result = await(Promise::fromObservable($asyncClient->broadcasts()), $loop);
self::assertSame($expected, $result);
}
}