Skip to content

Commit c71ea4d

Browse files
committed
Created interface for tests
1 parent a7b266b commit c71ea4d

9 files changed

+133
-34
lines changed

src/AsyncClient.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@
99
use ApiClients\Foundation\Oauth1\Options as Oauth1Options;
1010
use ApiClients\Foundation\Options;
1111
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
12-
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand;
1312
use ApiClients\Foundation\Transport\Options as TransportOptions;
1413
use ApiClients\Tools\CommandBus\CommandBus;
14+
use ApiClients\Tools\CommandBus\CommandBusInterface;
1515
use ApiClients\Tools\Psr7\Oauth1\Definition;
1616
use GuzzleHttp\Psr7\Request;
17-
use Psr\Http\Message\RequestInterface;
1817
use Psr\Http\Message\ResponseInterface;
1918
use React\EventLoop\LoopInterface;
2019
use React\Promise\PromiseInterface;
21-
use Rx\Extra\Operator\CutOperator;
2220
use Rx\Observable;
23-
use Rx\React\Promise;
2421
use function React\Promise\resolve;
2522

26-
final class AsyncClient
23+
final class AsyncClient implements AsyncClientInterface
2724
{
2825
/**
2926
* @var string
@@ -111,9 +108,9 @@ public function withOutAccessToken(): AsyncClient
111108
);
112109
}
113110

114-
public function getCommandBus(): CommandBus
111+
public function getCommandBus(): CommandBusInterface
115112
{
116-
return $this->client->getFromContainer(CommandBus::class);
113+
return $this->client->getFromContainer(CommandBusInterface::class);
117114
}
118115

119116
public function user(string $user): PromiseInterface

src/AsyncClientInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Twitter;
4+
5+
use ApiClients\Tools\CommandBus\CommandBusInterface;
6+
use ApiClients\Tools\Psr7\Oauth1\Definition;
7+
use React\Promise\PromiseInterface;
8+
use Rx\Observable;
9+
use function React\Promise\resolve;
10+
11+
interface AsyncClientInterface
12+
{
13+
public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient;
14+
public function withOutAccessToken(): AsyncClient;
15+
public function getCommandBus(): CommandBusInterface;
16+
public function user(string $user): PromiseInterface;
17+
public function stream(): AsyncStreamingClient;
18+
}

src/AsyncStreamingClient.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@
33
namespace ApiClients\Client\Twitter;
44

55
use ApiClients\Foundation\Client;
6-
use ApiClients\Foundation\Factory;
76
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
8-
use ApiClients\Foundation\Oauth1\Middleware\Oauth1Middleware;
9-
use ApiClients\Foundation\Oauth1\Options as Oauth1Options;
10-
use ApiClients\Foundation\Options;
11-
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
127
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand;
13-
use ApiClients\Foundation\Transport\Options as TransportOptions;
148
use ApiClients\Tools\Psr7\Oauth1\Definition;
159
use GuzzleHttp\Psr7\Request;
1610
use Psr\Http\Message\RequestInterface;
17-
use Psr\Http\Message\ResponseInterface;
1811
use React\EventLoop\LoopInterface;
19-
use React\Promise\PromiseInterface;
2012
use Rx\Extra\Operator\CutOperator;
2113
use Rx\Observable;
2214
use Rx\React\Promise;
2315
use function React\Promise\resolve;
2416

25-
final class AsyncStreamingClient
17+
final class AsyncStreamingClient implements AsyncStreamingClientInterface
2618
{
2719
const STREAM_DELIMITER = "\r\n";
2820

src/AsyncStreamingClientInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Twitter;
4+
5+
use Rx\Observable;
6+
7+
interface AsyncStreamingClientInterface
8+
{
9+
public function sample(): Observable;
10+
public function filtered(array $filter = []): Observable;
11+
}

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use function Clue\React\Block\await;
99
use React\EventLoop\LoopInterface;
1010

11-
final class Client
11+
final class Client implements ClientInterface
1212
{
1313
/**
1414
* @var LoopInterface

src/ClientInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Twitter;
4+
5+
use ApiClients\Client\Twitter\Resource\TweetInterface;
6+
use ApiClients\Client\Twitter\Resource\UserInterface;
7+
use React\EventLoop\Factory as LoopFactory;
8+
use function Clue\React\Block\await;
9+
use React\EventLoop\LoopInterface;
10+
11+
interface ClientInterface
12+
{
13+
public function withAccessToken(string $accessToken, string $accessTokenSecret): Client;
14+
public function withOutAccessToken(): Client;
15+
public function stream(): StreamingClient;
16+
public function tweet(string $tweet): TweetInterface;
17+
public function user(string $tweet): UserInterface;
18+
}

src/StreamingClient.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand;
66
use ApiClients\Foundation\Resource\ResourceInterface;
7-
use ApiClients\Tools\CommandBus\CommandBus;
7+
use ApiClients\Tools\CommandBus\CommandBusInterface;
88
use React\EventLoop\LoopInterface;
99
use ReflectionObject;
1010
use Rx\ObservableInterface;
1111
use Rx\React\Promise;
1212

13-
final class StreamingClient
13+
final class StreamingClient implements StreamingClientInterface
1414
{
1515
/**
1616
* @var LoopInterface
1717
*/
1818
protected $loop;
1919

2020
/**
21-
* @var CommandBus
21+
* @var CommandBusInterface
2222
*/
2323
protected $commandBus;
2424

2525
/**
26-
* @var AsyncStreamingClient
26+
* @var AsyncStreamingClientInterface
2727
*/
2828
protected $client;
2929

@@ -35,11 +35,14 @@ final class StreamingClient
3535
/**
3636
* StreamingClient constructor.
3737
* @param LoopInterface $loop
38-
* @param CommandBus $commandBus
39-
* @param AsyncStreamingClient $client
38+
* @param CommandBusInterface $commandBus
39+
* @param AsyncStreamingClientInterface $client
4040
*/
41-
public function __construct(LoopInterface $loop, CommandBus $commandBus, AsyncStreamingClient $client)
42-
{
41+
public function __construct(
42+
LoopInterface $loop,
43+
CommandBusInterface $commandBus,
44+
AsyncStreamingClientInterface $client
45+
) {
4346
$this->loop = $loop;
4447
$this->commandBus = $commandBus;
4548
$this->client = $client;

src/StreamingClientInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Twitter;
4+
5+
use Rx\Observable;
6+
7+
interface StreamingClientInterface
8+
{
9+
public function sample(callable $listener);
10+
public function filtered(callable $listener, array $filter = []);
11+
}

tests/StreamingClientTest.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,67 @@
22

33
namespace ApiClients\Tests\Client\Twitter;
44

5-
use ApiClients\Client\Twitter\Client;
5+
use ApiClients\Client\Twitter\AsyncStreamingClient;
6+
use ApiClients\Client\Twitter\AsyncStreamingClientInterface;
7+
use ApiClients\Client\Twitter\StreamingClient;
8+
use ApiClients\Foundation\Client;
9+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand;
10+
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand;
11+
use ApiClients\Tools\CommandBus\CommandBusInterface;
612
use ApiClients\Tools\TestUtilities\TestCase;
13+
use DI\ContainerBuilder;
14+
use Prophecy\Argument;
15+
use React\EventLoop\Factory;
16+
use React\EventLoop\LoopInterface;
17+
use Rx\Observable;
18+
use Rx\ObservableInterface;
719

8-
class ClientTest extends TestCase
20+
class StreamingClientTest extends TestCase
921
{
10-
public function testImmutability()
22+
public function streamingClientProvider()
1123
{
12-
$client = new Client('foo', 'bar');
13-
$newClient = $client->withAccessToken('beer', 'baz');
14-
$this->assertNotSame($client, $newClient);
15-
$anotherNewClient = $newClient->withOutAccessToken();
16-
$this->assertNotSame($client, $anotherNewClient);
17-
$this->assertNotSame($newClient, $anotherNewClient);
24+
yield [
25+
'sample',
26+
[],
27+
];
28+
29+
yield [
30+
'filtered',
31+
[
32+
[
33+
'follow' => 'php_api_clients',
34+
],
35+
],
36+
];
37+
}
38+
39+
/**
40+
* @dataProvider streamingClientProvider
41+
*/
42+
public function testStreamingClient(string $method, array $args)
43+
{
44+
$listener = function () {};
45+
$loop = Factory::create();
46+
$commandBus = $this->prophesize(CommandBusInterface::class);
47+
$commandBus->handle(Argument::type(BuildSyncFromAsyncCommand::class))->willReturn(\React\Promise\reject());
48+
/*$container = ContainerBuilder::buildDevContainer();
49+
$container->set(LoopInterface::class, Factory::create());
50+
$container->set(CommandBusInterface::class, $commandBus->reveal());
51+
$client = new Client($container);*/
52+
$observableSecond = $this->prophesize(Observable::class);
53+
$observableSecond->flatMap($listener, Argument::type('callable'));
54+
$observableFirst = $this->prophesize(Observable::class);
55+
$observableFirst->flatMap(Argument::type('callable'))->willReturn($observableSecond->reveal());
56+
$asyncStreamingClient = $this->prophesize(AsyncStreamingClientInterface::class);
57+
$asyncStreamingClient->$method(...$args)->willReturn($observableFirst->reveal());
58+
$streamingClient = new StreamingClient(
59+
$loop,
60+
$commandBus->reveal(),
61+
$asyncStreamingClient->reveal()
62+
);
63+
$streamingClient->$method(
64+
$listener,
65+
...$args
66+
);
1867
}
1968
}

0 commit comments

Comments
 (0)