Skip to content

Commit 9535f16

Browse files
authored
fix(pagination): Update client cursor pagination (#178)
1 parent 4a0f32a commit 9535f16

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

docs/2-usage.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ do {
7777
} while (!empty($cursor));
7878
```
7979

80-
>Note: the virtual methods are not yet documented with PHPDoc so your IDE will
81-
not autocomplete them.
82-
8380
## Concrete examples
8481

8582
Here are some real-life examples of interacting with the SDK:

src/Client.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,31 @@
1414
namespace JoliCode\Slack;
1515

1616
use JoliCode\Slack\Api\Client as ApiClient;
17+
use JoliCode\Slack\Api\Model\ObjsConversation;
18+
use JoliCode\Slack\Api\Model\ObjsMessage;
19+
use JoliCode\Slack\Api\Model\ObjsSubteam;
20+
use JoliCode\Slack\Api\Model\ObjsUser;
1721

22+
/**
23+
* @method iterable<ObjsMessage> iterateConversationsHistory(array $arguments = [])
24+
* @method iterable<ObjsConversation> iterateConversationsList(array $arguments = [])
25+
* @method iterable<string> iterateConversationsMembers(array $arguments = [])
26+
* @method iterable<mixed> iterateConversationsReplies(array $arguments = [])
27+
* @method iterable<mixed> iterateFilesInfo(array $arguments = [])
28+
* @method iterable<ObjsSubteam> iterateUsergroupsList(array $arguments = [])
29+
* @method iterable<mixed> iterateReactionsList(array $arguments = [])
30+
* @method iterable<mixed> iterateStarsList(array $arguments = [])
31+
* @method iterable<ObjsUser> iterateUsersList(array $arguments = [])
32+
*/
1833
class Client extends ApiClient
1934
{
2035
public const CURSOR_PAGINATION = [
21-
'channelsList' => 'channels',
2236
'conversationsHistory' => 'messages',
2337
'conversationsList' => 'channels',
2438
'conversationsMembers' => 'members',
2539
'conversationsReplies' => 'messages',
2640
'filesInfo' => 'comments',
27-
'groupsList' => 'groups',
28-
'imList' => 'ims',
29-
'mpimList' => 'groups',
41+
'usergroupsList' => 'usergroups',
3042
'reactionsList' => 'items',
3143
'starsList' => 'items',
3244
'usersList' => 'members',

tests/ClientTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace JoliCode\Slack\Tests;
1515

1616
use JoliCode\Slack\Api\Model\ObjsUser;
17+
use JoliCode\Slack\Client;
1718

1819
class ClientTest extends SlackTokenDependentTest
1920
{
@@ -52,4 +53,65 @@ public function testItThrowsExceptionOnUnknownMethod(): void
5253

5354
$client->foobar();
5455
}
56+
57+
public function testAllCursorPaginationMethodExists()
58+
{
59+
$client = $this->createClient();
60+
61+
foreach (Client::CURSOR_PAGINATION as $methodName => $getterMethod) {
62+
$getterMethod = 'get' . $getterMethod;
63+
$method = lcfirst(str_replace('iterate', '', $methodName));
64+
65+
$responseFromMethod = $client->{$method}($this->argumentsForCursorPaginationRequest($method, $client));
66+
67+
self::assertTrue(
68+
method_exists($responseFromMethod, $getterMethod),
69+
\sprintf('Expected that response from %s would contain method %s', $method, $getterMethod)
70+
);
71+
}
72+
}
73+
74+
private function argumentsForCursorPaginationRequest(string $method, Client $client): array
75+
{
76+
if (\in_array($method, ['conversationsMembers', 'conversationsHistory'])) {
77+
return ['channel' => $_SERVER['SLACK_TEST_CHANNEL']];
78+
}
79+
80+
if ('conversationsReplies' === $method) {
81+
return [
82+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
83+
'ts' => $this->findLastThreadTsInChannel($client),
84+
];
85+
}
86+
87+
if ('filesInfo' === $method) {
88+
return ['file' => $this->findLastFileIdInChannel($client)];
89+
}
90+
91+
return [];
92+
}
93+
94+
private function findLastThreadTsInChannel(Client $client): string
95+
{
96+
$messages = $client->conversationsHistory([
97+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
98+
'limit' => 100,
99+
])->getMessages();
100+
101+
foreach ($messages as $message) {
102+
if (\is_string($message->getThreadTs()) && $message->getThreadTs() === $message->getTs()) {
103+
return $message->getThreadTs();
104+
}
105+
}
106+
107+
throw new \RuntimeException('Unable to find thread in your test channel');
108+
}
109+
110+
private function findLastFileIdInChannel(Client $client): string
111+
{
112+
return $client->filesList(['channel' => $_SERVER['SLACK_TEST_CHANNEL']])
113+
->getFiles()[0]
114+
?->getId() ?? throw new \RuntimeException('Unable to find file in your test channel')
115+
;
116+
}
55117
}

0 commit comments

Comments
 (0)