-
Notifications
You must be signed in to change notification settings - Fork 159
[WIP] Enhancement: Added GitHub Repository Service #287
Changes from all commits
7ea02c7
623bff3
927b151
b69d6f6
caa9da7
bc18d8b
fec4129
f0a1544
160505d
29e4063
75dd602
410933d
34d7988
d83a026
4794882
671cde3
1572a72
b714f69
8fe8d8b
48746fa
eb5dc54
89a3f78
afc153a
8d4eea1
1ffd29b
9964ba1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"license": "BSD-3-Clause", | ||
"homepage": "https://modules.zendframework.com", | ||
"require": { | ||
"php": "~5.4", | ||
"php": "~5.5", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confirmed here, no? #284 (comment) - on the other side i'm not sure if the deployment is working because a merged pr #292 is still broken on the live system. /cc @GeeH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
"bshaffer/oauth2-server-php": "dev-develop", | ||
"zendframework/zendframework": "~2.3.0", | ||
"rwoverdijk/assetmanager": "1.3.*", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ | |
'service_manager' => array( | ||
'factories' => array( | ||
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', | ||
\Application\Service\RepositoryRetriever::class => \Application\Service\RepositoryRetrieverFactory::class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I like that a lot, been using it in all projects where PHP5.5 was available. However, I'd avoid FCQNs and import |
||
), | ||
), | ||
'translator' => array( | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return type is missing (it's |
||
* @return RepositoryCollection | ||
*/ | ||
public function getAuthenticatedUserRepositories(array $params = array()) | ||
{ | ||
return $this->githubClient->api('current_user')->repos($params); | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to make sure it's PHP5.4-compliant for now, happy to revisit as soon as someone has confirmed we've got PHP5.5+ running on the production machine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #287 (comment) |
||
|
||
return new RepositoryRetriever($githubClient); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
namespace ApplicationTest\Service; | ||
|
||
use Application\Service\RepositoryRetriever; | ||
use EdpGithub\Api; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class RepositoryRetrieverTest extends PHPUnit_Framework_TestCase | ||
{ | ||
private $response; | ||
private $headers; | ||
private $httpClient; | ||
private $client; | ||
|
||
protected function setUp() | ||
{ | ||
$this->response = $this->getMock('Zend\Http\Response'); | ||
$this->headers = $this->getMock('Zend\Http\Headers'); | ||
$this->httpClient = $this->getMock('EdpGithub\Http\Client'); | ||
$this->client = $this->getMock('EdpGithub\Client'); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->response = null; | ||
$this->headers = null; | ||
$this->httpClient = null; | ||
$this->client = null; | ||
} | ||
|
||
private function getClientMock(Api\AbstractApi $apiInstance, $result) | ||
{ | ||
$this->response->expects($this->any()) | ||
->method('getBody') | ||
->will($this->returnValue($result)); | ||
|
||
$this->response->expects($this->any()) | ||
->method('getHeaders') | ||
->will($this->returnValue($this->headers)); | ||
|
||
$this->httpClient->expects($this->any()) | ||
->method('get') | ||
->will($this->returnValue($this->response)); | ||
|
||
$this->client->expects($this->any()) | ||
->method('getHttpClient') | ||
->will($this->returnValue($this->httpClient)); | ||
|
||
$apiInstance->setClient($this->client); | ||
|
||
$this->client->expects($this->any()) | ||
->method('api') | ||
->will($this->returnValue($apiInstance)); | ||
|
||
return $this->client; | ||
} | ||
|
||
public function testCanRetrieveUserRepositories() | ||
{ | ||
$payload = [ | ||
['name' => 'foo'], | ||
['name' => 'bar'], | ||
['name' => 'baz'] | ||
]; | ||
|
||
$clientMock = $this->getClientMock(new Api\User, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
|
||
$repositories = $service->getUserRepositories('foo'); | ||
$this->assertInstanceOf(\EdpGithub\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' | ||
]; | ||
|
||
$clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
$metadata = $service->getUserRepositoryMetadata('foo', 'bar'); | ||
|
||
$this->assertInstanceOf('stdClass', $metadata); | ||
$this->assertEquals($payload, (array)$metadata); | ||
} | ||
|
||
public function testCanRetrieveRepositoryFileContent() | ||
{ | ||
$payload = [ | ||
'content' => base64_encode('foo') | ||
]; | ||
|
||
$clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
|
||
$response = $service->getRepositoryFileContent('foo', 'bar', 'foo.baz'); | ||
|
||
$this->assertEquals('foo', $response); | ||
} | ||
|
||
public function testResponseContentMissingOnGetRepositoryFileContent() | ||
{ | ||
$payload = []; | ||
|
||
$clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
|
||
$response = $service->getRepositoryFileContent('foo', 'bar', 'baz'); | ||
|
||
$this->assertNull($response); | ||
} | ||
|
||
public function testCanRetrieveRepositoryFileMetadata() | ||
{ | ||
$payload = [ | ||
'name' => 'foo', | ||
'url' => 'http://foo.com' | ||
]; | ||
|
||
$clientMock = $this->getClientMock(new Api\Repos, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
|
||
$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'] | ||
]; | ||
|
||
$clientMock = $this->getClientMock(new Api\CurrentUser, json_encode($payload)); | ||
$service = new RepositoryRetriever($clientMock); | ||
|
||
$repositories = $service->getAuthenticatedUserRepositories(); | ||
$this->assertInstanceOf(\EdpGithub\Collection\RepositoryCollection::class, $repositories); | ||
|
||
$count = 0; | ||
foreach ($repositories as $repository) { | ||
$this->assertEquals(current($payload), (array)$repository); | ||
next($payload); | ||
++$count; | ||
} | ||
|
||
$this->assertEquals(count($payload), $count); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ins0
The only clue I currently have about what PHP version is currently running on the production machine is #284 (comment), so I think it's not really safe yet to assume we can rely on the availability of PHP5.5.
Can you make the changes in this PR PHP5.4 compliant?
class
keywordThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #287 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍