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
3 changes: 0 additions & 3 deletions docs/2-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 16 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjsMessage> iterateConversationsHistory(array $arguments = [])
* @method iterable<ObjsConversation> iterateConversationsList(array $arguments = [])
* @method iterable<string> iterateConversationsMembers(array $arguments = [])
* @method iterable<mixed> iterateConversationsReplies(array $arguments = [])
* @method iterable<mixed> iterateFilesInfo(array $arguments = [])
* @method iterable<ObjsSubteam> iterateUsergroupsList(array $arguments = [])
* @method iterable<mixed> iterateReactionsList(array $arguments = [])
* @method iterable<mixed> iterateStarsList(array $arguments = [])
* @method iterable<ObjsUser> 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',
Expand Down
62 changes: 62 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace JoliCode\Slack\Tests;

use JoliCode\Slack\Api\Model\ObjsUser;
use JoliCode\Slack\Client;

class ClientTest extends SlackTokenDependentTest
{
Expand Down Expand Up @@ -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')
;
}
}