diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index 772e0d55792..4a3e47a62a6 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -12,6 +12,7 @@ use Github\Api\Repository\Forks; use Github\Api\Repository\Hooks; use Github\Api\Repository\Labels; +use Github\Api\Repository\Stargazers; use Github\Api\Repository\Statuses; /** @@ -309,6 +310,18 @@ public function forks() return new Forks($this->client); } + /** + * Manage the stargazers of a repository. + * + * @link https://developer.github.com/v3/activity/starring/#list-stargazers + * + * @return Stargazers + */ + public function stargazers() + { + return new Stargazers($this->client); + } + /** * Manage the hooks of a repository. * diff --git a/lib/Github/Api/Repository/Stargazers.php b/lib/Github/Api/Repository/Stargazers.php new file mode 100644 index 00000000000..f8d2ec17e14 --- /dev/null +++ b/lib/Github/Api/Repository/Stargazers.php @@ -0,0 +1,35 @@ + + */ +class Stargazers extends AbstractApi +{ + /** + * Configure the body type + * + * @see https://developer.github.com/v3/activity/starring/#alternative-response-with-star-creation-timestamps + * + * @param string $bodyType + */ + public function configure($bodyType = null) + { + if ('star' === $bodyType) { + $this->client->setHeaders( + array( + 'Accept' => sprintf('application/vnd.github.%s.star+json', $this->client->getOption('api_version')) + ) + ); + } + } + + public function all($username, $repository) + { + return $this->get('repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/stargazers'); + } +} diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index 849bfe35d10..3ada7d440fb 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -465,6 +465,16 @@ public function shouldGetStatusesApiObject() $this->assertInstanceOf('Github\Api\Repository\Statuses', $api->statuses()); } + /** + * @test + */ + public function shouldGetStargazersApiObject() + { + $api = $this->getApiMock(); + + $this->assertInstanceOf('Github\Api\Repository\Stargazers', $api->stargazers()); + } + /** * @test */ diff --git a/test/Github/Tests/Api/Repository/StargazersTest.php b/test/Github/Tests/Api/Repository/StargazersTest.php new file mode 100644 index 00000000000..e9c2dbbb50c --- /dev/null +++ b/test/Github/Tests/Api/Repository/StargazersTest.php @@ -0,0 +1,46 @@ + 'nidup')); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/stargazers') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api')); + } + + /** + * @test + */ + public function shouldGetAllRepositoryStargazersWithAlternativeResponse() + { + $expectedValue = array(array('starred_at' => '2013-10-01T13:22:01Z', 'user' => array('login' => 'nidup'))); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/stargazers') + ->will($this->returnValue($expectedValue)); + $api->configure('star'); + + $this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api')); + } + + protected function getApiClass() + { + return 'Github\Api\Repository\Stargazers'; + } +}