forked from zendframework/modules.zendframework.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework#287 from ins0/fix/github-service
- Loading branch information
Showing
9 changed files
with
358 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ language: php | |
sudo: false | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- hhvm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
module/Application/src/Application/Service/RepositoryRetriever.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Application\Service; | ||
|
||
use EdpGithub\Client; | ||
use EdpGithub\Collection\RepositoryCollection; | ||
use EdpGithub\Listener\Exception\RuntimeException; | ||
|
||
class RepositoryRetriever | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $githubClient; | ||
|
||
/** | ||
* @param Client $githubClient | ||
*/ | ||
public function __construct(Client $githubClient) | ||
{ | ||
$this->githubClient = $githubClient; | ||
} | ||
|
||
/** | ||
* Return MetaData from User Repository | ||
* @param string $user | ||
* @param string $module | ||
* @return mixed | ||
*/ | ||
public function getUserRepositoryMetadata($user, $module) | ||
{ | ||
return json_decode($this->githubClient->api('repos')->show($user, $module)); | ||
} | ||
|
||
/** | ||
* Get all Repositories from GitHub User | ||
* @param string $user | ||
* @param array $params | ||
* @return RepositoryCollection | ||
*/ | ||
public function getUserRepositories($user, array $params = array()) | ||
{ | ||
return $this->githubClient->api('user')->repos($user, $params); | ||
} | ||
|
||
/** | ||
* Get File Content from User Repository | ||
* @param string $user | ||
* @param string $module | ||
* @param string $filePath | ||
* @return string|null | ||
*/ | ||
public function getRepositoryFileContent($user, $module, $filePath) | ||
{ | ||
$contentResponse = $this->getRepositoryFileMetadata($user, $module, $filePath); | ||
|
||
if (!isset($contentResponse->content)) { | ||
return null; | ||
} | ||
|
||
return base64_decode($contentResponse->content); | ||
} | ||
|
||
/** | ||
* Return File MetaData from User Repository | ||
* @param string $user | ||
* @param string $module | ||
* @param string $filePath | ||
* @return mixed | ||
*/ | ||
public function getRepositoryFileMetadata($user, $module, $filePath) | ||
{ | ||
return json_decode($this->githubClient->api('repos')->content($user, $module, $filePath)); | ||
} | ||
|
||
/** | ||
* Return all Repositories from current authenticated GitHub User | ||
* @param array $params | ||
* @return RepositoryCollection | ||
*/ | ||
public function getAuthenticatedUserRepositories(array $params = array()) | ||
{ | ||
return $this->githubClient->api('current_user')->repos($params); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
module/Application/src/Application/Service/RepositoryRetrieverFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Application\Service; | ||
|
||
use EdpGithub\Client; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class RepositoryRetrieverFactory implements FactoryInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return RepositoryRetriever | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
/* @var Client $githubClient */ | ||
$githubClient = $serviceLocator->get(Client::class); | ||
|
||
return new RepositoryRetriever($githubClient); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
module/Application/test/ApplicationTest/Integration/Service/RepositoryRetrieverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace ApplicationTest\Integration\Service; | ||
|
||
use Application\Service; | ||
use ApplicationTest\Integration\Util\Bootstrap; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testServiceCanBeRetrieved() | ||
{ | ||
$serviceManager = Bootstrap::getServiceManager(); | ||
|
||
$this->assertInstanceOf( | ||
Service\RepositoryRetriever::class, | ||
$serviceManager->get(Service\RepositoryRetriever::class) | ||
); | ||
} | ||
} |
193 changes: 193 additions & 0 deletions
193
module/Application/test/ApplicationTest/Service/RepositoryRetrieverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
namespace ApplicationTest\Service; | ||
|
||
use Application\Service\RepositoryRetriever; | ||
use EdpGithub\Api; | ||
use EdpGithub\Collection; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testCanRetrieveUserRepositories() | ||
{ | ||
$payload = [ | ||
['name' => 'foo'], | ||
['name' => 'bar'], | ||
['name' => 'baz'], | ||
]; | ||
|
||
$client = $this->getClientMock( | ||
new Api\User(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$repositories = $service->getUserRepositories('foo'); | ||
|
||
$this->assertInstanceOf(Collection\RepositoryCollection::class, $repositories); | ||
|
||
$count = 0; | ||
foreach ($repositories as $repository) { | ||
$this->assertEquals(current($payload), (array)$repository); | ||
next($payload); | ||
++$count; | ||
} | ||
|
||
$this->assertEquals(count($payload), $count); | ||
} | ||
|
||
public function testCanRetrieveUserRepositoryMetadata() | ||
{ | ||
$payload = [ | ||
'name' => 'foo', | ||
'url' => 'http://foo.com', | ||
]; | ||
|
||
$client = $this->getClientMock( | ||
new Api\Repos(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$metadata = $service->getUserRepositoryMetadata('foo', 'bar'); | ||
|
||
$this->assertInstanceOf('stdClass', $metadata); | ||
$this->assertEquals($payload, (array)$metadata); | ||
} | ||
|
||
public function testCanRetrieveRepositoryFileContent() | ||
{ | ||
$payload = [ | ||
'content' => base64_encode('foo'), | ||
]; | ||
|
||
$client = $this->getClientMock( | ||
new Api\Repos(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$response = $service->getRepositoryFileContent('foo', 'bar', 'foo.baz'); | ||
|
||
$this->assertEquals('foo', $response); | ||
} | ||
|
||
public function testResponseContentMissingOnGetRepositoryFileContent() | ||
{ | ||
$payload = []; | ||
|
||
$client = $this->getClientMock( | ||
new Api\Repos(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$response = $service->getRepositoryFileContent('foo', 'bar', 'baz'); | ||
|
||
$this->assertNull($response); | ||
} | ||
|
||
public function testCanRetrieveRepositoryFileMetadata() | ||
{ | ||
$payload = [ | ||
'name' => 'foo', | ||
'url' => 'http://foo.com', | ||
]; | ||
|
||
$client = $this->getClientMock( | ||
new Api\Repos(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$metadata = $service->getRepositoryFileMetadata('foo', 'bar', 'baz'); | ||
|
||
$this->assertInstanceOf('stdClass', $metadata); | ||
$this->assertEquals($payload, (array) $metadata); | ||
} | ||
|
||
public function testCanRetrieveAuthenticatedUserRepositories() | ||
{ | ||
$payload = [ | ||
['name' => 'foo'], | ||
['name' => 'bar'], | ||
['name' => 'baz'], | ||
]; | ||
|
||
$client = $this->getClientMock( | ||
new Api\CurrentUser(), | ||
$payload | ||
); | ||
|
||
$service = new RepositoryRetriever($client); | ||
|
||
$repositories = $service->getAuthenticatedUserRepositories(); | ||
|
||
$this->assertInstanceOf(Collection\RepositoryCollection::class, $repositories); | ||
|
||
$count = 0; | ||
foreach ($repositories as $repository) { | ||
$this->assertEquals(current($payload), (array) $repository); | ||
next($payload); | ||
++$count; | ||
} | ||
|
||
$this->assertEquals(count($payload), $count); | ||
} | ||
|
||
/** | ||
* @param Api\AbstractApi $apiInstance | ||
* @param array $payload | ||
* @return \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function getClientMock(Api\AbstractApi $apiInstance, array $payload = []) | ||
{ | ||
$response = $this->getMock('Zend\Http\Response'); | ||
|
||
$response | ||
->expects($this->any()) | ||
->method('getBody') | ||
->willReturn(json_encode($payload)) | ||
; | ||
|
||
$headers = $this->getMock('Zend\Http\Headers'); | ||
|
||
$response | ||
->expects($this->any()) | ||
->method('getHeaders') | ||
->willReturn($headers) | ||
; | ||
|
||
$httpClient = $this->getMock('EdpGithub\Http\Client'); | ||
|
||
$httpClient | ||
->expects($this->any()) | ||
->method('get') | ||
->willReturn($response) | ||
; | ||
|
||
$client = $this->getMock('EdpGithub\Client'); | ||
|
||
$client | ||
->expects($this->any()) | ||
->method('getHttpClient') | ||
->willReturn($httpClient) | ||
; | ||
|
||
$apiInstance->setClient($client); | ||
|
||
$client | ||
->expects($this->any()) | ||
->method('api') | ||
->willReturn($apiInstance) | ||
; | ||
|
||
return $client; | ||
} | ||
} |
Oops, something went wrong.