Skip to content

Commit 3fd505f

Browse files
committed
Merge pull request #361 from nidup/add_stargazers_support
Add repository stargazers support
2 parents 5720f82 + e6f0431 commit 3fd505f

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

lib/Github/Api/Repo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Github\Api\Repository\Forks;
1313
use Github\Api\Repository\Hooks;
1414
use Github\Api\Repository\Labels;
15+
use Github\Api\Repository\Stargazers;
1516
use Github\Api\Repository\Statuses;
1617

1718
/**
@@ -309,6 +310,18 @@ public function forks()
309310
return new Forks($this->client);
310311
}
311312

313+
/**
314+
* Manage the stargazers of a repository.
315+
*
316+
* @link https://developer.github.com/v3/activity/starring/#list-stargazers
317+
*
318+
* @return Stargazers
319+
*/
320+
public function stargazers()
321+
{
322+
return new Stargazers($this->client);
323+
}
324+
312325
/**
313326
* Manage the hooks of a repository.
314327
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Github\Api\Repository;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://developer.github.com/v3/activity/starring/#list-stargazers
9+
* @author Nicolas Dupont <[email protected]>
10+
*/
11+
class Stargazers extends AbstractApi
12+
{
13+
/**
14+
* Configure the body type
15+
*
16+
* @see https://developer.github.com/v3/activity/starring/#alternative-response-with-star-creation-timestamps
17+
*
18+
* @param string $bodyType
19+
*/
20+
public function configure($bodyType = null)
21+
{
22+
if ('star' === $bodyType) {
23+
$this->client->setHeaders(
24+
array(
25+
'Accept' => sprintf('application/vnd.github.%s.star+json', $this->client->getOption('api_version'))
26+
)
27+
);
28+
}
29+
}
30+
31+
public function all($username, $repository)
32+
{
33+
return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/stargazers');
34+
}
35+
}

test/Github/Tests/Api/RepoTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,16 @@ public function shouldGetStatusesApiObject()
465465
$this->assertInstanceOf('Github\Api\Repository\Statuses', $api->statuses());
466466
}
467467

468+
/**
469+
* @test
470+
*/
471+
public function shouldGetStargazersApiObject()
472+
{
473+
$api = $this->getApiMock();
474+
475+
$this->assertInstanceOf('Github\Api\Repository\Stargazers', $api->stargazers());
476+
}
477+
468478
/**
469479
* @test
470480
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class StargazersTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldGetAllRepositoryStargazers()
13+
{
14+
$expectedValue = array(array('login' => 'nidup'));
15+
16+
$api = $this->getApiMock();
17+
$api->expects($this->once())
18+
->method('get')
19+
->with('repos/KnpLabs/php-github-api/stargazers')
20+
->will($this->returnValue($expectedValue));
21+
22+
$this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api'));
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function shouldGetAllRepositoryStargazersWithAlternativeResponse()
29+
{
30+
$expectedValue = array(array('starred_at' => '2013-10-01T13:22:01Z', 'user' => array('login' => 'nidup')));
31+
32+
$api = $this->getApiMock();
33+
$api->expects($this->once())
34+
->method('get')
35+
->with('repos/KnpLabs/php-github-api/stargazers')
36+
->will($this->returnValue($expectedValue));
37+
$api->configure('star');
38+
39+
$this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api'));
40+
}
41+
42+
protected function getApiClass()
43+
{
44+
return 'Github\Api\Repository\Stargazers';
45+
}
46+
}

0 commit comments

Comments
 (0)