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

Commit ba68f07

Browse files
committed
Merge pull request #455 from localheinz/feature/repository-entity
Enhancement: Implement Entity\Repository
2 parents b65fd39 + b561121 commit ba68f07

File tree

11 files changed

+209
-66
lines changed

11 files changed

+209
-66
lines changed

module/Application/config/module.config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Psr\Log;
77

88
return [
9-
'github_repository' => [
9+
'project_github_repository' => [
1010
'owner' => 'zendframework',
1111
'name' => 'modules.zendframework.com',
1212
],
@@ -98,6 +98,7 @@
9898
'factories' => [
9999
\HTMLPurifier::class => Service\HtmlPurifierFactory::class,
100100
Log\LoggerInterface::class => Service\LoggerFactory::class,
101+
'project_github_repository' => Service\GitHubRepositoryFactory::class,
101102
Service\ErrorHandlingService::class => Service\ErrorHandlingServiceFactory::class,
102103
Service\RepositoryRetriever::class => Service\RepositoryRetrieverFactory::class,
103104
],

module/Application/src/Application/Controller/ContributorsController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@
22

33
namespace Application\Controller;
44

5-
use Application\Service\RepositoryRetriever;
5+
use Application\Entity;
6+
use Application\Service;
67
use Zend\Mvc\Controller\AbstractActionController;
78
use Zend\View\Model\ViewModel;
89

910
class ContributorsController extends AbstractActionController
1011
{
1112
/**
12-
* @var RepositoryRetriever
13+
* @var Service\RepositoryRetriever
1314
*/
1415
private $repositoryRetriever;
1516

1617
/**
17-
* @var array
18+
* @var Entity\Repository
1819
*/
19-
private $repositoryData;
20+
private $repository;
2021

2122
/**
22-
* @param RepositoryRetriever $repositoryRetriever
23-
* @param array $repositoryData
23+
* @param Service\RepositoryRetriever $repositoryRetriever
24+
* @param Entity\Repository $repository
2425
*/
25-
public function __construct(RepositoryRetriever $repositoryRetriever, array $repositoryData)
26+
public function __construct(Service\RepositoryRetriever $repositoryRetriever, Entity\Repository $repository)
2627
{
2728
$this->repositoryRetriever = $repositoryRetriever;
28-
$this->repositoryData = $repositoryData;
29+
$this->repository = $repository;
2930
}
3031

3132
public function indexAction()
3233
{
3334
$contributors = $this->repositoryRetriever->getContributors(
34-
$this->repositoryData['owner'],
35-
$this->repositoryData['name']
35+
$this->repository->owner(),
36+
$this->repository->name()
3637
);
3738

3839
shuffle($contributors);
3940

4041
$metadata = $this->repositoryRetriever->getUserRepositoryMetadata(
41-
$this->repositoryData['owner'],
42-
$this->repositoryData['name']
42+
$this->repository->owner(),
43+
$this->repository->name()
4344
);
4445

4546
return new ViewModel([

module/Application/src/Application/Controller/ContributorsControllerFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Application\Controller;
44

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

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

2628
return new ContributorsController(
2729
$repositoryRetriever,
28-
$repositoryData
30+
$repository
2931
);
3032
}
3133
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Application\Entity;
4+
5+
class Repository
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $owner;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $name;
16+
17+
/**
18+
* @param string $owner
19+
* @param string $name
20+
*/
21+
public function __construct($owner, $name)
22+
{
23+
$this->owner = (string) $owner;
24+
$this->name = (string) $name;
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function owner()
31+
{
32+
return $this->owner;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function name()
39+
{
40+
return $this->name;
41+
}
42+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Application\Service;
4+
5+
use Application\Entity;
6+
use Zend\ServiceManager\FactoryInterface;
7+
use Zend\ServiceManager\ServiceLocatorInterface;
8+
9+
class GitHubRepositoryFactory implements FactoryInterface
10+
{
11+
/**
12+
* @param ServiceLocatorInterface $serviceLocator
13+
* @return Entity\Repository
14+
*/
15+
public function createService(ServiceLocatorInterface $serviceLocator)
16+
{
17+
$config = $serviceLocator->get('Config')['project_github_repository'];
18+
19+
return new Entity\Repository(
20+
$config['owner'],
21+
$config['name']
22+
);
23+
}
24+
}

module/Application/src/Application/View/Helper/GitHubRepositoryUrl.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,27 @@
22

33
namespace Application\View\Helper;
44

5+
use Application\Entity;
56
use Zend\View\Helper\AbstractHelper;
67

78
class GitHubRepositoryUrl extends AbstractHelper
89
{
910
/**
10-
* @var string
11-
*/
12-
private $owner;
13-
14-
/**
15-
* @var string
11+
* @var Entity\Repository
1612
*/
17-
private $name;
13+
private $repository;
1814

1915
/**
2016
* @var string
2117
*/
2218
private $url;
2319

2420
/**
25-
* @param string $owner
26-
* @param string $name
21+
* @param Entity\Repository $repository
2722
*/
28-
public function __construct($owner, $name)
23+
public function __construct(Entity\Repository $repository)
2924
{
30-
$this->owner = (string) $owner;
31-
$this->name = (string) $name;
25+
$this->repository = $repository;
3226
}
3327

3428
/**
@@ -39,8 +33,8 @@ public function __invoke()
3933
if (null === $this->url) {
4034
$this->url = sprintf(
4135
'https://github.com/%s/%s',
42-
$this->owner,
43-
$this->name
36+
$this->repository->owner(),
37+
$this->repository->name()
4438
);
4539
}
4640

module/Application/src/Application/View/Helper/GitHubRepositoryUrlFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Application\View\Helper;
44

5+
use Application\Entity;
56
use Zend\ServiceManager\FactoryInterface;
67
use Zend\ServiceManager\ServiceLocatorInterface;
78
use Zend\View\HelperPluginManager;
@@ -18,11 +19,9 @@ public function createService(ServiceLocatorInterface $serviceLocator)
1819
/* @var HelperPluginManager $serviceLocator */
1920
$serviceManager = $serviceLocator->getServiceLocator();
2021

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

23-
return new GitHubRepositoryUrl(
24-
$config['owner'],
25-
$config['name']
26-
);
25+
return new GitHubRepositoryUrl($repository);
2726
}
2827
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ApplicationTest\Entity;
4+
5+
use Application\Entity;
6+
use ApplicationTest\Mock\StringCastable;
7+
use PHPUnit_Framework_TestCase;
8+
9+
class RepositoryTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testConstructorSetsValues()
12+
{
13+
$owner = 'foo';
14+
$name = 'bar';
15+
16+
$entity = new Entity\Repository(
17+
$owner,
18+
$name
19+
);
20+
21+
$this->assertSame($owner, $entity->owner());
22+
$this->assertSame($name, $entity->name());
23+
}
24+
25+
public function testConstructorCastsToString()
26+
{
27+
$owner = new StringCastable('foo');
28+
$name = new StringCastable('bar');
29+
30+
$entity = new Entity\Repository(
31+
$owner,
32+
$name
33+
);
34+
35+
$this->assertSame((string) $owner, $entity->owner());
36+
$this->assertSame((string) $name, $entity->name());
37+
}
38+
}

module/Application/test/ApplicationTest/Integration/Controller/ContributorsControllerTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ApplicationTest\Integration\Controller;
44

55
use Application\Controller;
6+
use Application\Entity;
67
use Application\Service;
78
use ApplicationTest\Integration\Util\Bootstrap;
89
use stdClass;
@@ -23,12 +24,22 @@ public function testContributorsActionCanBeAccessed()
2324
$vendor = 'foo';
2425
$name = 'bar';
2526

26-
$config = $this->getApplicationServiceLocator()->get('Config');
27+
$repository = $this->getMockBuilder(Entity\Repository::class)
28+
->disableOriginalConstructor()
29+
->getMock()
30+
;
2731

28-
$config['github_repository'] = [
29-
'owner' => $vendor,
30-
'name' => $name,
31-
];
32+
$repository
33+
->expects($this->any())
34+
->method('owner')
35+
->willReturn($vendor)
36+
;
37+
38+
$repository
39+
->expects($this->any())
40+
->method('name')
41+
->willReturn($name)
42+
;
3243

3344
$repositoryRetriever = $this->getMockBuilder(Service\RepositoryRetriever::class)
3445
->disableOriginalConstructor()
@@ -63,8 +74,8 @@ public function testContributorsActionCanBeAccessed()
6374
$this->getApplicationServiceLocator()
6475
->setAllowOverride(true)
6576
->setService(
66-
'Config',
67-
$config
77+
'project_github_repository',
78+
$repository
6879
)
6980
->setService(
7081
Service\RepositoryRetriever::class,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ApplicationTest\Integration\Service;
4+
5+
use Application\Entity;
6+
use ApplicationTest\Integration\Util\Bootstrap;
7+
use PHPUnit_Framework_TestCase;
8+
9+
/**
10+
* @group Functional
11+
* @coversNothing
12+
*/
13+
class GitHubRepositoryTest extends PHPUnit_Framework_TestCase
14+
{
15+
public function testServiceCanBeRetrieved()
16+
{
17+
$serviceManager = Bootstrap::getServiceManager();
18+
19+
$this->assertInstanceOf(
20+
Entity\Repository::class,
21+
$serviceManager->get('project_github_repository')
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)