Skip to content

Commit 290d96f

Browse files
committed
Fix fail of functional test when API limit was reached
1 parent d72b200 commit 290d96f

File tree

6 files changed

+52
-50
lines changed

6 files changed

+52
-50
lines changed

test/Github/Tests/Functional/CommitTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Github\Tests\Functional;
44

5-
use Github\Client;
6-
7-
class CommitTest extends \PHPUnit_Framework_TestCase
5+
class CommitTest extends TestCase
86
{
97
/**
108
* @test
@@ -15,9 +13,8 @@ public function shouldRetrieveCommitsForRepositoryBranch()
1513
$repo = 'php-github-api';
1614
$branch = 'master';
1715

18-
$github = new Client();
19-
$commits = $github->api('repo')->commits()->all($username, $repo, array('sha' => $branch));
20-
$commit = array_pop($commits);
16+
$commits = $this->client->api('repo')->commits()->all($username, $repo, array('sha' => $branch));
17+
$commit = array_pop($commits);
2118

2219
$this->assertArrayHasKey('url', $commit);
2320
$this->assertArrayHasKey('committer', $commit);
@@ -34,8 +31,7 @@ public function shouldRetrieveCommitBySha()
3431
$username = 'KnpLabs';
3532
$repo = 'php-github-api';
3633

37-
$github = new Client();
38-
$commit = $github->api('repo')->commits()->show($username, $repo, '6df3adf5bd16745299c6429e163265daed430fa1');
34+
$commit = $this->client->api('repo')->commits()->show($username, $repo, '6df3adf5bd16745299c6429e163265daed430fa1');
3935

4036
$this->assertArrayHasKey('url', $commit);
4137
$this->assertArrayHasKey('committer', $commit);
@@ -54,8 +50,7 @@ public function shouldRetrieveCommitsForFile()
5450
$repo = 'php-github-api';
5551
$branch = 'master';
5652

57-
$github = new Client();
58-
$commits = $github->api('repo')->commits()->all($username, $repo, array('sha' => $branch, 'path' => 'composer.json'));
53+
$commits = $this->client->api('repo')->commits()->all($username, $repo, array('sha' => $branch, 'path' => 'composer.json'));
5954
$commit = array_pop($commits);
6055

6156
$this->assertArrayHasKey('url', $commit);

test/Github/Tests/Functional/GistTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
namespace Github\Tests\Functional;
44

5-
use Github\Client;
6-
7-
class GistTest extends \PHPUnit_Framework_TestCase
5+
class GistTest extends TestCase
86
{
97
/**
108
* @test
119
*/
1210
public function shouldRetrievePublicGistsListWhenCalledAnonymously()
1311
{
14-
$github = new Client();
15-
$gists = $github->api('gists')->all();
12+
$gists = $this->client->api('gists')->all();
1613
$gist = array_pop($gists);
1714

1815
$this->assertArrayHasKey('url', $gist);
@@ -29,17 +26,15 @@ public function shouldRetrievePublicGistsListWhenCalledAnonymously()
2926
*/
3027
public function shouldNotGetStarredListWithoutAuthorization()
3128
{
32-
$github = new Client();
33-
$github->api('gists')->all('starred');
29+
$this->client->api('gists')->all('starred');
3430
}
3531

3632
/**
3733
* @test
3834
*/
3935
public function shouldRetrievePublicGistsList()
4036
{
41-
$github = new Client();
42-
$gists = $github->api('gists')->all('public');
37+
$gists = $this->client->api('gists')->all('public');
4338
$gist = array_pop($gists);
4439

4540
$this->assertArrayHasKey('url', $gist);
@@ -57,8 +52,7 @@ public function shouldRetrieveGistById()
5752
{
5853
$id = 1;
5954

60-
$github = new Client();
61-
$gist = $github->api('gists')->show($id);
55+
$gist = $this->client->api('gists')->show($id);
6256

6357
$this->assertArrayHasKey('url', $gist);
6458
$this->assertArrayHasKey('files', $gist);

test/Github/Tests/Functional/MarkdownTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
namespace Github\Tests\Functional;
44

5-
use Github\Client;
6-
7-
class MarkdownTest extends \PHPUnit_Framework_TestCase
5+
class MarkdownTest extends TestCase
86
{
97
/**
108
* @test
119
*/
1210
public function shouldRetrieveParsedMarkdownContent()
1311
{
14-
$api = new Client();
15-
$api = $api->api('markdown');
12+
$api = $this->client->api('markdown');
1613

1714
$input = 'Hello world github/linguist#1 **cool**, and #1!';
1815
$output = '<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>';

test/Github/Tests/Functional/RepoTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Github\Tests\Functional;
44

5-
use Github\Client;
6-
7-
class RepoTest extends \PHPUnit_Framework_TestCase
5+
class RepoTest extends TestCase
86
{
97
/**
108
* @test
@@ -14,8 +12,7 @@ public function shouldRetrieveContributorsList()
1412
$username = 'KnpLabs';
1513
$repo = 'php-github-api';
1614

17-
$github = new Client();
18-
$contributors = $github->api('repo')->contributors($username, $repo);
15+
$contributors = $this->client->api('repo')->contributors($username, $repo);
1916
$contributor = array_pop($contributors);
2017

2118
$this->assertArrayHasKey('url', $contributor);
@@ -34,8 +31,7 @@ public function shouldShowRepo()
3431
$username = 'KnpLabs';
3532
$repo = 'php-github-api';
3633

37-
$github = new Client();
38-
$repo = $github->api('repo')->show($username, $repo);
34+
$repo = $this->client->api('repo')->show($username, $repo);
3935

4036
$this->assertArrayHasKey('id', $repo);
4137
$this->assertArrayHasKey('name', $repo);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
use Github\Client;
6+
use Github\Exception\ApiLimitExceedException;
7+
8+
class TestCase
9+
{
10+
protected $client;
11+
12+
public function setUp()
13+
{
14+
$client = new Client();
15+
16+
try {
17+
$client->api('current_user')->show();
18+
} catch (ApiLimitExceedException $e) {
19+
$this->markTestSkipped('API limit reached. Skipping to prevent unnecessary failure.');
20+
}
21+
22+
$this->client = $client;
23+
}
24+
}

test/Github/Tests/Functional/UserTest.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Github\Tests\Functional;
44

5-
use Github\Client;
6-
7-
class UsetTest extends \PHPUnit_Framework_TestCase
5+
class UsetTest extends TestCase
86
{
97
/**
108
* @test
@@ -13,8 +11,7 @@ public function shouldShowUserData()
1311
{
1412
$username = 'KnpLabs';
1513

16-
$github = new Client();
17-
$user = $github->api('user')->show($username);
14+
$user = $this->client->api('user')->show($username);
1815

1916
$this->assertArrayHasKey('id', $user);
2017
$this->assertEquals('KnpLabs', $user['login']);
@@ -36,8 +33,7 @@ public function shouldShowUserData()
3633
*/
3734
public function shouldNotUpdateUserWithoutAuthorization()
3835
{
39-
$github = new Client();
40-
$github->api('current_user')->update(array('email' => '[email protected]'));
36+
$this->client->api('current_user')->update(array('email' => '[email protected]'));
4137
}
4238

4339
/**
@@ -47,8 +43,7 @@ public function shouldGetUsersWhoUserIsFollowing()
4743
{
4844
$username = 'l3l0';
4945

50-
$github = new Client();
51-
$users = $github->api('user')->following($username);
46+
$users = $this->client->api('user')->following($username);
5247
$user = array_pop($users);
5348

5449
$this->assertArrayHasKey('id', $user);
@@ -62,8 +57,12 @@ public function shouldGetFollowersUsers()
6257
{
6358
$username = 'KnpLabs';
6459

65-
$github = new Client();
66-
$users = $github->api('user')->followers($username);
60+
$this->client = new Client();
61+
try {
62+
$users = $this->client->api('user')->followers($username);
63+
} catch (ApiLimitExceedException $e) {
64+
$this->markTestSkipped('API limit reached. Skipping to prevent unnecessary failure.');
65+
}
6766
$user = array_pop($users);
6867

6968
$this->assertArrayHasKey('id', $user);
@@ -76,8 +75,7 @@ public function shouldGetFollowersUsers()
7675
*/
7776
public function shouldNotFollowUserWithoutAuthorization()
7877
{
79-
$github = new Client();
80-
$github->api('current_user')->follow()->follow('KnpLabs');
78+
$this->client->api('current_user')->follow()->follow('KnpLabs');
8179
}
8280

8381
/**
@@ -86,8 +84,7 @@ public function shouldNotFollowUserWithoutAuthorization()
8684
*/
8785
public function shouldNotUnfollowUserWithoutAuthorization()
8886
{
89-
$github = new Client();
90-
$github->api('current_user')->follow()->unfollow('KnpLabs');
87+
$this->client->api('current_user')->follow()->unfollow('KnpLabs');
9188
}
9289

9390
/**
@@ -97,8 +94,7 @@ public function shouldGetReposBeingWatched()
9794
{
9895
$username = 'l3l0';
9996

100-
$github = new Client();
101-
$repos = $github->api('user')->watched($username);
97+
$repos = $this->client->api('user')->watched($username);
10298
$repo = array_pop($repos);
10399

104100
$this->assertArrayHasKey('id', $repo);

0 commit comments

Comments
 (0)