Skip to content

Add repository stargazers support #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down
35 changes: 35 additions & 0 deletions lib/Github/Api/Repository/Stargazers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Github\Api\Repository;

use Github\Api\AbstractApi;

/**
* @link https://developer.github.com/v3/activity/starring/#list-stargazers
* @author Nicolas Dupont <[email protected]>
*/
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');
}
}
10 changes: 10 additions & 0 deletions test/Github/Tests/Api/RepoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
46 changes: 46 additions & 0 deletions test/Github/Tests/Api/Repository/StargazersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Github\Tests\Api\Repository;

use Github\Tests\Api\TestCase;

class StargazersTest extends TestCase
{
/**
* @test
*/
public function shouldGetAllRepositoryStargazers()
{
$expectedValue = array(array('login' => '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';
}
}