Skip to content

Commit a9be377

Browse files
committed
Refactor and fix tests when no env vars given
1 parent 27cfdec commit a9be377

File tree

6 files changed

+96
-78
lines changed

6 files changed

+96
-78
lines changed

tests/ApiTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of JoliCode's Slack PHP API project.
7+
*
8+
* (c) JoliCode <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace JoliCode\Slack\Tests;
15+
16+
use JoliCode\Slack\Api\Model\ApiTestGetResponse200;
17+
use JoliCode\Slack\ClientFactory;
18+
use JoliCode\Slack\Exception\SlackErrorResponse;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class ApiTest extends TestCase
22+
{
23+
public function testItWorksOnTestSuccess()
24+
{
25+
$client = ClientFactory::create('');
26+
$response = $client->apiTest();
27+
28+
self::assertInstanceOf(ApiTestGetResponse200::class, $response);
29+
self::assertTrue($response->getOk());
30+
}
31+
32+
public function testItThrowsExceptionOnTestError()
33+
{
34+
$client = ClientFactory::create('');
35+
36+
self::expectException(SlackErrorResponse::class);
37+
self::expectExceptionMessage('Slack returned error code "yolo"');
38+
39+
$client->apiTest([
40+
'error' => 'yolo',
41+
]);
42+
}
43+
}

tests/ClientTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,12 @@
1414
namespace JoliCode\Slack\Tests;
1515

1616
use JoliCode\Slack\Api\Model\ObjsUser;
17-
use JoliCode\Slack\ClientFactory;
18-
use PHPUnit\Framework\TestCase;
1917

20-
class ClientTest extends TestCase
18+
class ClientTest extends SlackTokenDependentTest
2119
{
22-
protected function setUp(): void
23-
{
24-
if (!\array_key_exists('SLACK_TOKEN', $_SERVER)) {
25-
$this->markTestSkipped('SLACK_TOKEN env var not present, skip the test.');
26-
}
27-
}
28-
2920
public function testItCanIterate()
3021
{
31-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
22+
$client = $this->createClient();
3223

3324
$users = $client->iterateUsersList([
3425
'limit' => 1000,
@@ -44,7 +35,7 @@ public function testItCanIterate()
4435

4536
public function testItThrowsExceptionOnUnknownIterate()
4637
{
47-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
38+
$client = $this->createClient();
4839

4940
self::expectException(\BadMethodCallException::class);
5041
self::expectExceptionMessage('Unknown method JoliCode\Slack\Client::iterateFooBar()');
@@ -54,7 +45,7 @@ public function testItThrowsExceptionOnUnknownIterate()
5445

5546
public function testItThrowsExceptionOnUnknownMethod()
5647
{
57-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
48+
$client = $this->createClient();
5849

5950
self::expectException(\BadMethodCallException::class);
6051
self::expectExceptionMessage('Unknown method JoliCode\Slack\Client::foobar()');

tests/ReadingTest.php

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,18 @@
1313

1414
namespace JoliCode\Slack\Tests;
1515

16-
use JoliCode\Slack\Api\Model\ApiTestGetResponse200;
1716
use JoliCode\Slack\Api\Model\ConversationsHistoryGetResponse200;
1817
use JoliCode\Slack\Api\Model\ConversationsListGetResponse200;
1918
use JoliCode\Slack\Api\Model\ObjsFile;
2019
use JoliCode\Slack\Api\Model\SearchMessagesGetResponse200;
2120
use JoliCode\Slack\Api\Model\UsersListGetResponse200;
22-
use JoliCode\Slack\ClientFactory;
2321
use JoliCode\Slack\Exception\SlackErrorResponse;
24-
use PHPUnit\Framework\TestCase;
2522

26-
class ReadingTest extends TestCase
23+
class ReadingTest extends SlackTokenDependentTest
2724
{
28-
protected function setUp(): void
29-
{
30-
if (!\array_key_exists('SLACK_TOKEN', $_SERVER)) {
31-
$this->markTestSkipped('SLACK_TOKEN env var not present, skip the test.');
32-
}
33-
}
34-
35-
public function testItWorksOnTestSuccess()
36-
{
37-
$client = ClientFactory::create('');
38-
$response = $client->apiTest();
39-
40-
self::assertInstanceOf(ApiTestGetResponse200::class, $response);
41-
self::assertTrue($response->getOk());
42-
}
43-
44-
public function testItThrowsExceptionOnTestError()
45-
{
46-
$client = ClientFactory::create('');
47-
48-
self::expectException(SlackErrorResponse::class);
49-
self::expectExceptionMessage('Slack returned error code "yolo"');
50-
51-
$client->apiTest([
52-
'error' => 'yolo',
53-
]);
54-
}
55-
5625
public function testItWorksOnUserListWithCorrectToken()
5726
{
58-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
27+
$client = $this->createClient();
5928

6029
$response = $client->usersList();
6130

@@ -67,7 +36,7 @@ public function testItWorksOnUserListWithCorrectToken()
6736

6837
public function testItThrowsExceptionOnUserListWithoutToken()
6938
{
70-
$client = ClientFactory::create('');
39+
$client = $this->createClient('');
7140

7241
self::expectException(SlackErrorResponse::class);
7342
self::expectExceptionMessage('Slack returned error code "not_authed"');
@@ -77,7 +46,7 @@ public function testItThrowsExceptionOnUserListWithoutToken()
7746

7847
public function testItCanReadAConversationHistory()
7948
{
80-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
49+
$client = $this->createClient();
8150

8251
$results = $client->conversationsHistory([
8352
'channel' => $_SERVER['SLACK_TEST_CHANNEL'],
@@ -105,7 +74,7 @@ public function testItCanReadAConversationHistory()
10574

10675
public function testItCanGetTheImList()
10776
{
108-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
77+
$client = $this->createClient();
10978

11079
$results = $client->conversationsList(['types' => 'im']);
11180

@@ -115,7 +84,7 @@ public function testItCanGetTheImList()
11584

11685
public function testItCanReadConversationsAndHydrateThem()
11786
{
118-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
87+
$client = $this->createClient();
11988

12089
/** @var ConversationsListGetResponse200 $response */
12190
$response = $client->conversationsList([
@@ -133,7 +102,7 @@ public function testItCanReadConversationsAndHydrateThem()
133102

134103
public function testItCanSearchMessages()
135104
{
136-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
105+
$client = $this->createClient();
137106

138107
/** @var SearchMessagesGetResponse200 $results */
139108
$results = $client->searchMessages([

tests/SlackTokenDependentTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of JoliCode's Slack PHP API project.
7+
*
8+
* (c) JoliCode <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace JoliCode\Slack\Tests;
15+
16+
use JoliCode\Slack\Client;
17+
use JoliCode\Slack\ClientFactory;
18+
use PHPUnit\Framework\TestCase;
19+
20+
abstract class SlackTokenDependentTest extends TestCase
21+
{
22+
protected function setUp(): void
23+
{
24+
if (!($_SERVER['SLACK_TOKEN'] ?? null)) {
25+
$this->markTestSkipped('SLACK_TOKEN env var not present, skip the test.');
26+
}
27+
}
28+
29+
protected function createClient(?string $token = null): Client
30+
{
31+
return ClientFactory::create(null === $token ? $_SERVER['SLACK_TOKEN'] : $token);
32+
}
33+
}

tests/UserInfoTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,12 @@
1414
namespace JoliCode\Slack\Tests;
1515

1616
use JoliCode\Slack\Api\Model\UsersListGetResponse200;
17-
use JoliCode\Slack\ClientFactory;
18-
use PHPUnit\Framework\TestCase;
1917

20-
class UserInfoTest extends TestCase
18+
class UserInfoTest extends SlackTokenDependentTest
2119
{
22-
protected function setUp(): void
23-
{
24-
if (!\array_key_exists('SLACK_TOKEN', $_SERVER)) {
25-
$this->markTestSkipped('SLACK_TOKEN env var not present, skip the test.');
26-
}
27-
}
28-
2920
public function testItCanFetchUserInfo()
3021
{
31-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
22+
$client = $this->createClient();
3223

3324
$response = $client->usersList(['limit' => 2]);
3425

tests/WritingTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,13 @@
1515

1616
use JoliCode\Slack\Api\Model\ChatPostMessagePostResponse200;
1717
use JoliCode\Slack\Api\Model\FilesUploadPostResponse200;
18-
use JoliCode\Slack\ClientFactory;
1918
use Nyholm\Psr7\Stream;
20-
use PHPUnit\Framework\TestCase;
2119

22-
class WritingTest extends TestCase
20+
class WritingTest extends SlackTokenDependentTest
2321
{
24-
protected function setUp(): void
25-
{
26-
if (!\array_key_exists('SLACK_TOKEN', $_SERVER)) {
27-
$this->markTestSkipped('SLACK_TOKEN env var not present, skip the test.');
28-
}
29-
}
30-
3122
public function testItCanPostAttachment()
3223
{
33-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
24+
$client = $this->createClient();
3425

3526
$response = $client->chatPostMessage([
3627
'username' => 'User A',
@@ -53,7 +44,7 @@ public function testItCanPostAttachment()
5344

5445
public function testItCanPostMessageWithBlock()
5546
{
56-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
47+
$client = $this->createClient();
5748

5849
$response = $client->chatPostMessage([
5950
'username' => 'User C',
@@ -88,7 +79,7 @@ public function testItCanPostMessageWithBlock()
8879

8980
public function testItCanPostAMessageAndThenAThreadResponse()
9081
{
91-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
82+
$client = $this->createClient();
9283

9384
/** @var ChatPostMessagePostResponse200 $response */
9485
$response = $client->chatPostMessage([
@@ -109,7 +100,7 @@ public function testItCanPostAMessageAndThenAThreadResponse()
109100

110101
public function testItCanUploadFile()
111102
{
112-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
103+
$client = $this->createClient();
113104

114105
/** @var FilesUploadPostResponse200 $response */
115106
$response = $client->filesUpload([
@@ -133,7 +124,7 @@ public function testItCanUploadFile()
133124

134125
public function testScheduleMessage()
135126
{
136-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
127+
$client = $this->createClient();
137128
$futureTs = (new \DateTime('+1 hour'))->getTimestamp();
138129

139130
$response = $client->chatScheduleMessage([
@@ -147,7 +138,7 @@ public function testScheduleMessage()
147138

148139
public function testItCanMarkConversation()
149140
{
150-
$client = ClientFactory::create($_SERVER['SLACK_TOKEN']);
141+
$client = $this->createClient();
151142

152143
$response = $client->chatPostMessage([
153144
'username' => 'User A',

0 commit comments

Comments
 (0)