-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathClientTest.php
More file actions
121 lines (93 loc) · 3.63 KB
/
ClientTest.php
File metadata and controls
121 lines (93 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
/*
* This file is part of JoliCode's Slack PHP API project.
*
* (c) JoliCode <coucou@jolicode.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JoliCode\Slack\Tests;
use JoliCode\Slack\Api\Model\ObjsUser;
use JoliCode\Slack\Client;
class ClientTest extends SlackTokenDependentTest
{
public function testItCanIterate(): void
{
$client = $this->createClient();
$users = $client->iterateUsersList([
'limit' => 1000,
]);
self::assertIsIterable($users);
$users = iterator_to_array($users);
self::assertGreaterThan(1000, \count($users));
self::assertInstanceOf(ObjsUser::class, $users[0]);
}
public function testItThrowsExceptionOnUnknownIterate(): void
{
$client = $this->createClient();
self::expectException(\BadMethodCallException::class);
self::expectExceptionMessage('Unknown method JoliCode\Slack\Client::iterateFooBar()');
$client->iterateFooBar();
}
public function testItThrowsExceptionOnUnknownMethod(): void
{
$client = $this->createClient();
self::expectException(\BadMethodCallException::class);
self::expectExceptionMessage('Unknown method JoliCode\Slack\Client::foobar()');
$client->foobar();
}
public function testAllCursorPaginationMethodExists()
{
$client = $this->createClient();
foreach (Client::CURSOR_PAGINATION as $methodName => $getterMethod) {
$getterMethod = 'get' . $getterMethod;
$method = lcfirst(str_replace('iterate', '', $methodName));
if ($_SERVER['CI'] ?? false) {
sleep(10); // @see https://github.com/jolicode/slack-php-api/issues/163
}
$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')
;
}
}