Skip to content

Commit

Permalink
Merge pull request zendframework#287 from ins0/fix/github-service
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 11, 2015
2 parents fb27548 + 9964ba1 commit ae68452
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 38 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: php
sudo: false

php:
- 5.4
- 5.5
- 5.6
- hhvm
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "BSD-3-Clause",
"homepage": "https://modules.zendframework.com",
"require": {
"php": "~5.4",
"php": "~5.5",
"bshaffer/oauth2-server-php": "dev-develop",
"zendframework/zendframework": "~2.3.0",
"rwoverdijk/assetmanager": "1.3.*",
Expand Down
3 changes: 3 additions & 0 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

use Application\Service;

return array(
'router' => array(
'routes' => array(
Expand Down Expand Up @@ -87,6 +89,7 @@
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
Service\RepositoryRetriever::class => Service\RepositoryRetrieverFactory::class,
),
),
'translator' => array(
Expand Down
85 changes: 85 additions & 0 deletions module/Application/src/Application/Service/RepositoryRetriever.php
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);
}
}
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);
}
}
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)
);
}
}
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;
}
}
Loading

0 comments on commit ae68452

Please sign in to comment.