Skip to content

Commit 5cefd43

Browse files
authored
Merge pull request #118 from pyrech/fix-tests
Refactor and fix tests when no env vars given
2 parents 27cfdec + 84f6000 commit 5cefd43

File tree

10 files changed

+122
-96
lines changed

10 files changed

+122
-96
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ jobs:
2020
with:
2121
args: --config=.php_cs --diff --dry-run
2222

23+
check-spec:
24+
name: Check SDK is in sync with spec
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v2
29+
30+
- name: Install dependencies
31+
run: composer update --prefer-dist --no-interaction
32+
33+
- name: Generate SDK in ci_generated directory
34+
run: CI_TEST=1 vendor/bin/jane-openapi generate --config-file=.jane-openapi.php
35+
36+
- name: Fix CS on ci_generated directory
37+
run: vendor/bin/php-cs-fixer fix --diff --dry-run ci_generated
38+
39+
- name: Check ci_generated and generated directories are equal
40+
run: diff ci_generated generated
41+
2342
tests:
2443
name: Test PHP ${{ matrix.php-version }} ${{ matrix.name }}
2544
runs-on: ubuntu-latest
@@ -43,19 +62,8 @@ jobs:
4362
php-version: ${{ matrix.php-version }}
4463
extensions: mbstring, xml
4564

46-
- name: Get composer cache directory
47-
id: composer-cache
48-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
49-
50-
- name: Cache composer dependencies
51-
uses: actions/cache@v2
52-
with:
53-
path: ${{ steps.composer-cache.outputs.dir }}
54-
key: ${{ runner.os }}-composer-${{ github.sha }}
55-
restore-keys: ${{ runner.os }}-composer-
56-
5765
- name: Install dependencies
58-
run: composer update --prefer-dist --no-interaction ${{ matrix.composer-flags }}
66+
run: "composer update --prefer-dist --no-interaction ${{ matrix.composer-flags }}"
5967

6068
- name: Run tests
6169
run: php vendor/bin/simple-phpunit

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ composer.phar
1010
phpunit.xml
1111

1212
# tests
13-
generated_ci
13+
ci_generated
1414
.phpunit.result.cache

.jane-openapi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$directory = __DIR__ . '/generated/';
44

55
if (!empty($_SERVER['CI_TEST'])) {
6-
$directory = __DIR__ . '/generated_ci/';
6+
$directory = __DIR__ . '/ci_generated/';
77
}
88

99
return [

.php_cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ return PhpCsFixer\Config::create()
3434
'semicolon_after_instruction' => true,
3535
'combine_consecutive_unsets' => true,
3636
))
37-
->setFinder(
38-
PhpCsFixer\Finder::create()
39-
->in(__DIR__)
40-
->exclude('doc')
37+
->setFinder(PhpCsFixer\Finder::create()
38+
->in(__DIR__)
39+
->exclude('doc')
40+
->exclude('ci_generated')
4141
)
4242
;

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)