Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #455 from localheinz/feature/repository-entity
Browse files Browse the repository at this point in the history
Enhancement: Implement Entity\Repository
  • Loading branch information
Ocramius committed Mar 4, 2015
2 parents b65fd39 + b561121 commit ba68f07
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 66 deletions.
3 changes: 2 additions & 1 deletion module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Log;

return [
'github_repository' => [
'project_github_repository' => [
'owner' => 'zendframework',
'name' => 'modules.zendframework.com',
],
Expand Down Expand Up @@ -98,6 +98,7 @@
'factories' => [
\HTMLPurifier::class => Service\HtmlPurifierFactory::class,
Log\LoggerInterface::class => Service\LoggerFactory::class,
'project_github_repository' => Service\GitHubRepositoryFactory::class,
Service\ErrorHandlingService::class => Service\ErrorHandlingServiceFactory::class,
Service\RepositoryRetriever::class => Service\RepositoryRetrieverFactory::class,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,45 @@

namespace Application\Controller;

use Application\Service\RepositoryRetriever;
use Application\Entity;
use Application\Service;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ContributorsController extends AbstractActionController
{
/**
* @var RepositoryRetriever
* @var Service\RepositoryRetriever
*/
private $repositoryRetriever;

/**
* @var array
* @var Entity\Repository
*/
private $repositoryData;
private $repository;

/**
* @param RepositoryRetriever $repositoryRetriever
* @param array $repositoryData
* @param Service\RepositoryRetriever $repositoryRetriever
* @param Entity\Repository $repository
*/
public function __construct(RepositoryRetriever $repositoryRetriever, array $repositoryData)
public function __construct(Service\RepositoryRetriever $repositoryRetriever, Entity\Repository $repository)
{
$this->repositoryRetriever = $repositoryRetriever;
$this->repositoryData = $repositoryData;
$this->repository = $repository;
}

public function indexAction()
{
$contributors = $this->repositoryRetriever->getContributors(
$this->repositoryData['owner'],
$this->repositoryData['name']
$this->repository->owner(),
$this->repository->name()
);

shuffle($contributors);

$metadata = $this->repositoryRetriever->getUserRepositoryMetadata(
$this->repositoryData['owner'],
$this->repositoryData['name']
$this->repository->owner(),
$this->repository->name()
);

return new ViewModel([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Application\Controller;

use Application\Entity;
use Application\Service;
use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\FactoryInterface;
Expand All @@ -21,11 +22,12 @@ public function createService(ServiceLocatorInterface $controllerManager)
/* @var Service\RepositoryRetriever $repositoryRetriever */
$repositoryRetriever = $serviceManager->get(Service\RepositoryRetriever::class);

$repositoryData = $serviceManager->get('Config')['github_repository'];
/* @var Entity\Repository $repository */
$repository = $serviceManager->get('project_github_repository');

return new ContributorsController(
$repositoryRetriever,
$repositoryData
$repository
);
}
}
42 changes: 42 additions & 0 deletions module/Application/src/Application/Entity/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Application\Entity;

class Repository
{
/**
* @var string
*/
private $owner;

/**
* @var string
*/
private $name;

/**
* @param string $owner
* @param string $name
*/
public function __construct($owner, $name)
{
$this->owner = (string) $owner;
$this->name = (string) $name;
}

/**
* @return string
*/
public function owner()
{
return $this->owner;
}

/**
* @return string
*/
public function name()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Application\Service;

use Application\Entity;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class GitHubRepositoryFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @return Entity\Repository
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config')['project_github_repository'];

return new Entity\Repository(
$config['owner'],
$config['name']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@

namespace Application\View\Helper;

use Application\Entity;
use Zend\View\Helper\AbstractHelper;

class GitHubRepositoryUrl extends AbstractHelper
{
/**
* @var string
*/
private $owner;

/**
* @var string
* @var Entity\Repository
*/
private $name;
private $repository;

/**
* @var string
*/
private $url;

/**
* @param string $owner
* @param string $name
* @param Entity\Repository $repository
*/
public function __construct($owner, $name)
public function __construct(Entity\Repository $repository)
{
$this->owner = (string) $owner;
$this->name = (string) $name;
$this->repository = $repository;
}

/**
Expand All @@ -39,8 +33,8 @@ public function __invoke()
if (null === $this->url) {
$this->url = sprintf(
'https://github.com/%s/%s',
$this->owner,
$this->name
$this->repository->owner(),
$this->repository->name()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Application\View\Helper;

use Application\Entity;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\HelperPluginManager;
Expand All @@ -18,11 +19,9 @@ public function createService(ServiceLocatorInterface $serviceLocator)
/* @var HelperPluginManager $serviceLocator */
$serviceManager = $serviceLocator->getServiceLocator();

$config = $serviceManager->get('Config')['github_repository'];
/* @var Entity\Repository $repository */
$repository = $serviceManager->get('project_github_repository');

return new GitHubRepositoryUrl(
$config['owner'],
$config['name']
);
return new GitHubRepositoryUrl($repository);
}
}
38 changes: 38 additions & 0 deletions module/Application/test/ApplicationTest/Entity/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ApplicationTest\Entity;

use Application\Entity;
use ApplicationTest\Mock\StringCastable;
use PHPUnit_Framework_TestCase;

class RepositoryTest extends PHPUnit_Framework_TestCase
{
public function testConstructorSetsValues()
{
$owner = 'foo';
$name = 'bar';

$entity = new Entity\Repository(
$owner,
$name
);

$this->assertSame($owner, $entity->owner());
$this->assertSame($name, $entity->name());
}

public function testConstructorCastsToString()
{
$owner = new StringCastable('foo');
$name = new StringCastable('bar');

$entity = new Entity\Repository(
$owner,
$name
);

$this->assertSame((string) $owner, $entity->owner());
$this->assertSame((string) $name, $entity->name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ApplicationTest\Integration\Controller;

use Application\Controller;
use Application\Entity;
use Application\Service;
use ApplicationTest\Integration\Util\Bootstrap;
use stdClass;
Expand All @@ -23,12 +24,22 @@ public function testContributorsActionCanBeAccessed()
$vendor = 'foo';
$name = 'bar';

$config = $this->getApplicationServiceLocator()->get('Config');
$repository = $this->getMockBuilder(Entity\Repository::class)
->disableOriginalConstructor()
->getMock()
;

$config['github_repository'] = [
'owner' => $vendor,
'name' => $name,
];
$repository
->expects($this->any())
->method('owner')
->willReturn($vendor)
;

$repository
->expects($this->any())
->method('name')
->willReturn($name)
;

$repositoryRetriever = $this->getMockBuilder(Service\RepositoryRetriever::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -63,8 +74,8 @@ public function testContributorsActionCanBeAccessed()
$this->getApplicationServiceLocator()
->setAllowOverride(true)
->setService(
'Config',
$config
'project_github_repository',
$repository
)
->setService(
Service\RepositoryRetriever::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ApplicationTest\Integration\Service;

use Application\Entity;
use ApplicationTest\Integration\Util\Bootstrap;
use PHPUnit_Framework_TestCase;

/**
* @group Functional
* @coversNothing
*/
class GitHubRepositoryTest extends PHPUnit_Framework_TestCase
{
public function testServiceCanBeRetrieved()
{
$serviceManager = Bootstrap::getServiceManager();

$this->assertInstanceOf(
Entity\Repository::class,
$serviceManager->get('project_github_repository')
);
}
}
Loading

0 comments on commit ba68f07

Please sign in to comment.