Skip to content

Commit 7f33981

Browse files
Add Writing and Reading tests for common features that broke (#60)
1 parent 96ed8ad commit 7f33981

File tree

5 files changed

+263
-64
lines changed

5 files changed

+263
-64
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<php>
1111
<ini name="error_reporting" value="-1" />
1212
<!--<server name="SLACK_TOKEN" value="edit_me" />-->
13+
<!--<server name="SLACK_TEST_CHANNEL" value="edit_me" />-->
1314
</php>
1415

1516
<testsuites>

tests/FunctionalTest.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

tests/ReadingTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/*
4+
* This file is part of JoliCode's Slack PHP API project.
5+
*
6+
* (c) JoliCode <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace JoliCode\Slack\Tests;
13+
14+
use JoliCode\Slack\Api\Model\ApiTestGetResponse200;
15+
use JoliCode\Slack\Api\Model\ChannelsHistoryGetResponse200;
16+
use JoliCode\Slack\Api\Model\ConversationsListGetResponse200;
17+
use JoliCode\Slack\Api\Model\ImListGetResponse200;
18+
use JoliCode\Slack\Api\Model\ObjsFile;
19+
use JoliCode\Slack\Api\Model\SearchMessagesGetResponse200;
20+
use JoliCode\Slack\Api\Model\UsersListGetResponse200;
21+
use JoliCode\Slack\ClientFactory;
22+
use JoliCode\Slack\Exception\SlackErrorResponse;
23+
use PHPUnit\Framework\TestCase;
24+
25+
class ReadingTest extends TestCase
26+
{
27+
public function testItWorksOnTestSuccess()
28+
{
29+
$client = ClientFactory::create('');
30+
$response = $client->apiTest();
31+
32+
self::assertInstanceOf(ApiTestGetResponse200::class, $response);
33+
self::assertTrue($response->getOk());
34+
}
35+
36+
public function testItThrowsExceptionOnTestError()
37+
{
38+
$client = ClientFactory::create('');
39+
40+
self::expectException(SlackErrorResponse::class);
41+
self::expectExceptionMessage('Slack returned error code "yolo"');
42+
43+
$client->apiTest([
44+
'error' => 'yolo',
45+
]);
46+
}
47+
48+
public function testItWorksOnUserListWithCorrectToken()
49+
{
50+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
51+
52+
$response = $client->usersList();
53+
54+
self::assertInstanceOf(UsersListGetResponse200::class, $response);
55+
self::assertTrue($response->getOk());
56+
57+
self::assertGreaterThan(2, \count($response->getMembers()));
58+
}
59+
60+
public function testItThrowsExceptionOnUserListWithoutToken()
61+
{
62+
$client = ClientFactory::create('');
63+
64+
self::expectException(SlackErrorResponse::class);
65+
self::expectExceptionMessage('Slack returned error code "not_authed"');
66+
67+
$client->usersList();
68+
}
69+
70+
public function testItCanReadAChannelHistory()
71+
{
72+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
73+
74+
$results = $client->channelsHistory([
75+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
76+
]);
77+
78+
self::assertInstanceOf(ChannelsHistoryGetResponse200::class, $results);
79+
80+
foreach ($results->getMessages() as $message) {
81+
if ($message->getFiles()) {
82+
self::assertInstanceOf(ObjsFile::class, $message->getFiles()[0]);
83+
}
84+
}
85+
}
86+
87+
public function testItCanGetTheImList()
88+
{
89+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
90+
91+
$results = $client->imList();
92+
93+
self::assertInstanceOf(ImListGetResponse200::class, $results);
94+
self::assertNotEmpty($results->getIms());
95+
}
96+
97+
public function testItCanReadConversationsAndHydrateThem()
98+
{
99+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
100+
101+
/** @var ConversationsListGetResponse200 $response */
102+
$response = $client->conversationsList([
103+
'limit' => 2,
104+
]);
105+
106+
$this->assertTrue($response->getOk());
107+
$this->assertInstanceOf(ConversationsListGetResponse200::class, $response);
108+
$this->assertNotEmpty($response->getChannels());
109+
110+
foreach ($response->getChannels() as $channel) {
111+
$this->assertNotInstanceOf(\stdClass::class, $channel);
112+
}
113+
}
114+
115+
public function testItCanSearchMessages()
116+
{
117+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
118+
119+
/** @var SearchMessagesGetResponse200 $results */
120+
$results = $client->searchMessages([
121+
'query' => 'Message with attachment',
122+
'count' => '1',
123+
]);
124+
125+
self::assertInstanceOf(SearchMessagesGetResponse200::class, $results);
126+
127+
self::markTestIncomplete('The response is not tested as it is not specified!');
128+
}
129+
}

tests/UserInfoTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of JoliCode's Slack PHP API project.
5+
*
6+
* (c) JoliCode <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace JoliCode\Slack\Tests;
13+
14+
use JoliCode\Slack\Api\Model\UsersListGetResponse200;
15+
use JoliCode\Slack\ClientFactory;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class UserInfoTest extends TestCase
19+
{
20+
public function testItCanFetchUserInfo()
21+
{
22+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
23+
24+
$response = $client->usersList(['limit' => 2]);
25+
26+
/* @var UsersListGetResponse200 $response */
27+
$this->assertInstanceOf(UsersListGetResponse200::class, $response);
28+
$this->assertTrue($response->getOk());
29+
$this->assertNotEmpty($response->getMembers());
30+
31+
foreach ($response->getMembers() as $member) {
32+
$this->assertNotInstanceOf(\stdClass::class, $member);
33+
}
34+
}
35+
}

tests/WritingTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of JoliCode's Slack PHP API project.
5+
*
6+
* (c) JoliCode <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace JoliCode\Slack\Tests;
13+
14+
use JoliCode\Slack\Api\Model\ApiTestGetResponse200;
15+
use JoliCode\Slack\Api\Model\ChatPostMessagePostResponse200;
16+
use JoliCode\Slack\ClientFactory;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class WritingTest extends TestCase
20+
{
21+
public function testItCanPostAttachment()
22+
{
23+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
24+
25+
$response = $client->chatPostMessage([
26+
'username' => 'User A',
27+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
28+
'text' => 'Message with attachment',
29+
'attachments' => json_encode([[
30+
'fallback' => 'JoliCode',
31+
'image_url' => 'https://jolicode.com/images/valeurs_huma.png',
32+
]]),
33+
]);
34+
35+
self::assertInstanceOf(ChatPostMessagePostResponse200::class, $response);
36+
self::assertContains($response->getMessage()->getAttachments()[0]->getImageUrl(), 'https://jolicode.com/images/valeurs_huma.png');
37+
}
38+
39+
public function testItCanPostMessageWithBlock()
40+
{
41+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
42+
43+
$response = $client->chatPostMessage([
44+
'username' => 'User C',
45+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
46+
'text' => 'Message with blocks',
47+
'blocks' => json_encode([
48+
[
49+
'type' => 'section',
50+
'text' => [
51+
'type' => 'mrkdwn',
52+
'text' => ':tada: Blocks are working!',
53+
],
54+
],
55+
[
56+
'type' => 'divider',
57+
],
58+
[
59+
'type' => 'context',
60+
'elements' => [
61+
[
62+
'type' => 'mrkdwn',
63+
'text' => ':woman-running: Best test ever',
64+
],
65+
],
66+
],
67+
]),
68+
]);
69+
70+
self::assertInstanceOf(ChatPostMessagePostResponse200::class, $response);
71+
self::assertNotEmpty($response->getMessage()->getBlocks());
72+
}
73+
74+
public function testItCanPostAMessageAndThenAThreadResponse()
75+
{
76+
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
77+
$response = $client->apiTest();
78+
79+
self::assertInstanceOf(ApiTestGetResponse200::class, $response);
80+
self::assertTrue($response->getOk());
81+
82+
/** @var ChatPostMessagePostResponse200 $response */
83+
$response = $client->chatPostMessage([
84+
'username' => 'User A',
85+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
86+
'text' => 'First message',
87+
]);
88+
89+
$response2 = $client->chatPostMessage([
90+
'username' => 'User B',
91+
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
92+
'text' => 'First response in a Thread',
93+
'thread_ts' => $response->getMessage()->getTs(),
94+
]);
95+
96+
$this->assertTrue($response2->getOk());
97+
}
98+
}

0 commit comments

Comments
 (0)