diff --git a/examples/emojis-async.php b/examples/emojis-async.php new file mode 100644 index 0000000000..63401816be --- /dev/null +++ b/examples/emojis-async.php @@ -0,0 +1,17 @@ +emojis()->subscribe(function (EmojiInterface $emoji) { + resource_pretty_print($emoji); +}, 'display_throwable'); + +$loop->run(); diff --git a/src/AsyncClient.php b/src/AsyncClient.php index d488f98878..e09a2a4282 100644 --- a/src/AsyncClient.php +++ b/src/AsyncClient.php @@ -61,6 +61,11 @@ public function whoami(): PromiseInterface return $this->client->handle(new Command\UserCommand()); } + public function emojis(): Observable + { + return unwrapObservableFromPromise($this->client->handle(new Command\EmojisCommand())); + } + public function myOrganizations(): Observable { return unwrapObservableFromPromise($this->client->handle(new Command\MyOrganizationsCommand())); diff --git a/src/AsyncClientInterface.php b/src/AsyncClientInterface.php index caa82502c3..12b6b1c9b2 100644 --- a/src/AsyncClientInterface.php +++ b/src/AsyncClientInterface.php @@ -15,5 +15,7 @@ public function user(string $user): PromiseInterface; public function whoami(): PromiseInterface; + public function emojis(): Observable; + public function myOrganizations(): Observable; } diff --git a/src/CommandBus/Command/EmojisCommand.php b/src/CommandBus/Command/EmojisCommand.php new file mode 100644 index 0000000000..c5198612c4 --- /dev/null +++ b/src/CommandBus/Command/EmojisCommand.php @@ -0,0 +1,12 @@ +service = $service; + $this->hydrator = $hydrator; + } + + /** + * @param EmojisCommand $command + * @return PromiseInterface + */ + public function handle(EmojisCommand $command): PromiseInterface + { + return resolve( + $this->service->iterate('emojis') + ->flatMap(function ($emojis) { + $structuredEmojis = []; + + foreach ($emojis as $name => $image) { + $structuredEmojis[] = [ + 'name' => $name, + 'image' => $image, + ]; + } + + return Observable::fromArray($structuredEmojis); + })->map(function ($emoji) { + return $this->hydrator->hydrate(EmojiInterface::HYDRATE_CLASS, $emoji); + }) + ); + } +} diff --git a/src/Resource/Async/Emoji.php b/src/Resource/Async/Emoji.php new file mode 100644 index 0000000000..ea9d7b4039 --- /dev/null +++ b/src/Resource/Async/Emoji.php @@ -0,0 +1,13 @@ +name; + } + + /** + * @return string + */ + public function image() : string + { + return $this->image; + } +} diff --git a/src/Resource/EmojiInterface.php b/src/Resource/EmojiInterface.php new file mode 100644 index 0000000000..b7d4fe6559 --- /dev/null +++ b/src/Resource/EmojiInterface.php @@ -0,0 +1,20 @@ +wait($this->handleCommand( + new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this) + )->then(function (EmojiInterface $emoji) { + return $emoji->refresh(); + })); + } +} diff --git a/src/Resource/Sync/EmptyEmoji.php b/src/Resource/Sync/EmptyEmoji.php new file mode 100644 index 0000000000..61ec3de07e --- /dev/null +++ b/src/Resource/Sync/EmptyEmoji.php @@ -0,0 +1,9 @@ + 'bar', + 'bar' => 'foo', + ]; + $service = $this->prophesize(IteratePagesService::class); + $service->iterate('emojis')->shouldBeCalled()->willReturn(Observable::fromArray([$emojis])); + + $hydrator = $this->prophesize(Hydrator::class); + $hydrator->hydrate( + EmojiInterface::HYDRATE_CLASS, + [ + 'name' => 'foo', + 'image' => 'bar', + ] + )->shouldBeCalled()->willReturn($this->prophesize(EmojiInterface::class)->reveal()); + $hydrator->hydrate( + EmojiInterface::HYDRATE_CLASS, + [ + 'name' => 'bar', + 'image' => 'foo', + ] + )->shouldBeCalled()->willReturn($this->prophesize(EmojiInterface::class)->reveal()); + + $handler = new EmojisHandler($service->reveal(), $hydrator->reveal()); + $emojiResources = $this->await(Promise::fromObservable(unwrapObservableFromPromise($handler->handle(new EmojisCommand()))->toArray())); + + self::assertCount(2, $emojiResources); + } +} diff --git a/tests/Resource/Async/EmojiTest.php b/tests/Resource/Async/EmojiTest.php new file mode 100644 index 0000000000..ea57c34dfa --- /dev/null +++ b/tests/Resource/Async/EmojiTest.php @@ -0,0 +1,23 @@ +