|
14 | 14 | namespace JoliCode\Slack\Tests; |
15 | 15 |
|
16 | 16 | use JoliCode\Slack\Api\Model\ObjsUser; |
| 17 | +use JoliCode\Slack\Client; |
17 | 18 |
|
18 | 19 | class ClientTest extends SlackTokenDependentTest |
19 | 20 | { |
@@ -52,4 +53,65 @@ public function testItThrowsExceptionOnUnknownMethod(): void |
52 | 53 |
|
53 | 54 | $client->foobar(); |
54 | 55 | } |
| 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 | + } |
55 | 117 | } |
0 commit comments