diff --git a/docs/2-usage.md b/docs/2-usage.md index fce301b6..fde7ce74 100644 --- a/docs/2-usage.md +++ b/docs/2-usage.md @@ -77,9 +77,6 @@ do { } while (!empty($cursor)); ``` ->Note: the virtual methods are not yet documented with PHPDoc so your IDE will -not autocomplete them. - ## Concrete examples Here are some real-life examples of interacting with the SDK: diff --git a/src/Client.php b/src/Client.php index 35aa79cd..f31fca22 100644 --- a/src/Client.php +++ b/src/Client.php @@ -14,19 +14,31 @@ namespace JoliCode\Slack; use JoliCode\Slack\Api\Client as ApiClient; +use JoliCode\Slack\Api\Model\ObjsConversation; +use JoliCode\Slack\Api\Model\ObjsMessage; +use JoliCode\Slack\Api\Model\ObjsSubteam; +use JoliCode\Slack\Api\Model\ObjsUser; +/** + * @method iterable iterateConversationsHistory(array $arguments = []) + * @method iterable iterateConversationsList(array $arguments = []) + * @method iterable iterateConversationsMembers(array $arguments = []) + * @method iterable iterateConversationsReplies(array $arguments = []) + * @method iterable iterateFilesInfo(array $arguments = []) + * @method iterable iterateUsergroupsList(array $arguments = []) + * @method iterable iterateReactionsList(array $arguments = []) + * @method iterable iterateStarsList(array $arguments = []) + * @method iterable iterateUsersList(array $arguments = []) + */ class Client extends ApiClient { public const CURSOR_PAGINATION = [ - 'channelsList' => 'channels', 'conversationsHistory' => 'messages', 'conversationsList' => 'channels', 'conversationsMembers' => 'members', 'conversationsReplies' => 'messages', 'filesInfo' => 'comments', - 'groupsList' => 'groups', - 'imList' => 'ims', - 'mpimList' => 'groups', + 'usergroupsList' => 'usergroups', 'reactionsList' => 'items', 'starsList' => 'items', 'usersList' => 'members', diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 1a62f7c8..9c9406ae 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -14,6 +14,7 @@ namespace JoliCode\Slack\Tests; use JoliCode\Slack\Api\Model\ObjsUser; +use JoliCode\Slack\Client; class ClientTest extends SlackTokenDependentTest { @@ -52,4 +53,65 @@ public function testItThrowsExceptionOnUnknownMethod(): void $client->foobar(); } + + public function testAllCursorPaginationMethodExists() + { + $client = $this->createClient(); + + foreach (Client::CURSOR_PAGINATION as $methodName => $getterMethod) { + $getterMethod = 'get' . $getterMethod; + $method = lcfirst(str_replace('iterate', '', $methodName)); + + $responseFromMethod = $client->{$method}($this->argumentsForCursorPaginationRequest($method, $client)); + + self::assertTrue( + method_exists($responseFromMethod, $getterMethod), + \sprintf('Expected that response from %s would contain method %s', $method, $getterMethod) + ); + } + } + + private function argumentsForCursorPaginationRequest(string $method, Client $client): array + { + if (\in_array($method, ['conversationsMembers', 'conversationsHistory'])) { + return ['channel' => $_SERVER['SLACK_TEST_CHANNEL']]; + } + + if ('conversationsReplies' === $method) { + return [ + 'channel' => $_SERVER['SLACK_TEST_CHANNEL'], + 'ts' => $this->findLastThreadTsInChannel($client), + ]; + } + + if ('filesInfo' === $method) { + return ['file' => $this->findLastFileIdInChannel($client)]; + } + + return []; + } + + private function findLastThreadTsInChannel(Client $client): string + { + $messages = $client->conversationsHistory([ + 'channel' => $_SERVER['SLACK_TEST_CHANNEL'], + 'limit' => 100, + ])->getMessages(); + + foreach ($messages as $message) { + if (\is_string($message->getThreadTs()) && $message->getThreadTs() === $message->getTs()) { + return $message->getThreadTs(); + } + } + + throw new \RuntimeException('Unable to find thread in your test channel'); + } + + private function findLastFileIdInChannel(Client $client): string + { + return $client->filesList(['channel' => $_SERVER['SLACK_TEST_CHANNEL']]) + ->getFiles()[0] + ?->getId() ?? throw new \RuntimeException('Unable to find file in your test channel') + ; + } }