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

Commit 1ef97b5

Browse files
committed
Add a test case to enforce the exception handling behaviour
1 parent 11b8d8e commit 1ef97b5

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
namespace ZfModuleTest\Controller;
4+
5+
use ZfModule\Controller\IndexController;
6+
use PHPUnit_Framework_TestCase;
7+
use ZfModule\Mapper\Module as ModuleMapper;
8+
use ZfModule\Service\Module as ModuleService;
9+
use Application\Service\RepositoryRetriever;
10+
use ZfcUser\Controller\Plugin\ZfcUserAuthentication;
11+
use EdpGithub\Collection\RepositoryCollection;
12+
use EdpGithub\Api;
13+
use EdpGithub\Listener\Exception\RuntimeException as EdpGithubRuntimeException;
14+
use Zend\Mvc\Router\RouteMatch;
15+
16+
class ModuleTest extends PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var IndexController
20+
*/
21+
protected $controller;
22+
23+
/**
24+
* @var ModuleMapper
25+
*/
26+
protected $moduleMapper;
27+
28+
/**
29+
* @var ModuleService
30+
*/
31+
protected $moduleService;
32+
33+
/**
34+
* @var RepositoryRetriever
35+
*/
36+
protected $repositoryRetriever;
37+
38+
/**
39+
* @var ZfcUserAuthentication
40+
*/
41+
protected $zfcUserAuthentication;
42+
43+
public function setUp()
44+
{
45+
$this->moduleMapper = $this->getMockBuilder('ZfModule\Mapper\Module')
46+
->disableOriginalConstructor()
47+
->getMock();
48+
49+
$this->moduleService = $this->getMockBuilder('ZfModule\Service\Module')
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$this->repositoryRetriever = $this->getMockBuilder('Application\Service\RepositoryRetriever')
54+
->disableOriginalConstructor()
55+
->getMock();
56+
57+
$this->controller = new IndexController(
58+
$this->moduleMapper,
59+
$this->moduleService,
60+
$this->repositoryRetriever
61+
);
62+
63+
$this->zfcUserAuthentication = $this->getMockBuilder('ZfcUser\Controller\Plugin\ZfcUserAuthentication')
64+
->getMock();
65+
66+
$this->controller->getPluginManager()->setService(
67+
'zfcUserAuthentication',
68+
$this->zfcUserAuthentication
69+
);
70+
}
71+
72+
public function testIndexActionReturnsRepositoryListOnSuccessfulSync()
73+
{
74+
$this->zfcUserAuthentication->expects($this->once())
75+
->method('hasIdentity')
76+
->will($this->returnValue(true));
77+
78+
$githubClient = $this->getClientMock(new Api\Repos(), [
79+
['name' => 'foo', 'fork' => false, 'permissions' => ['push' => true]]
80+
]);
81+
$githubResponse = new RepositoryCollection($githubClient->getHttpClient(), '');
82+
83+
$this->repositoryRetriever->expects($this->once())
84+
->method('getAuthenticatedUserRepositories')
85+
->will($this->returnValue($githubResponse));
86+
87+
$this->moduleService->expects($this->once())
88+
->method('isModule')
89+
->will($this->returnValue(true));
90+
91+
$viewModel = $this->controller->indexAction();
92+
$this->assertInstanceOf('Zend\View\Model\ModelInterface', $viewModel);
93+
$this->assertEmpty($viewModel->errorMessage);
94+
$this->assertNotEmpty($viewModel->repositories);
95+
}
96+
97+
public function testIndexActionReturnsErrorMessageWhenRepositoryRetrieverThrowsEdpGithubRuntimeException()
98+
{
99+
$this->zfcUserAuthentication->expects($this->once())
100+
->method('hasIdentity')
101+
->will($this->returnValue(true));
102+
103+
$exception = new EdpGithubRuntimeException('EdpGithub throws this exception');
104+
105+
$this->repositoryRetriever->expects($this->once())
106+
->method('getAuthenticatedUserRepositories')
107+
->will($this->throwException($exception));
108+
109+
$viewModel = $this->controller->indexAction();
110+
$this->assertInstanceOf('Zend\View\Model\ModelInterface', $viewModel);
111+
$this->assertEquals($exception->getMessage(), $viewModel->errorMessage);
112+
$this->assertEmpty($viewModel->repositories);
113+
}
114+
115+
public function testOrganizationActionReturnsRepositoryListOnSuccessfulSync()
116+
{
117+
$this->zfcUserAuthentication->expects($this->once())
118+
->method('hasIdentity')
119+
->will($this->returnValue(true));
120+
121+
$routeMatch = new RouteMatch(['owner' => 'zendframework']);
122+
$this->controller->getEvent()->setRouteMatch($routeMatch);
123+
124+
$githubClient = $this->getClientMock(new Api\Repos(), [
125+
['name' => 'foo', 'fork' => false, 'permissions' => ['push' => true]]
126+
]);
127+
$githubResponse = new RepositoryCollection($githubClient->getHttpClient(), '');
128+
129+
$this->repositoryRetriever->expects($this->once())
130+
->method('getUserRepositories')
131+
->will($this->returnValue($githubResponse));
132+
133+
$this->moduleService->expects($this->once())
134+
->method('isModule')
135+
->will($this->returnValue(true));
136+
137+
$viewModel = $this->controller->organizationAction();
138+
$this->assertInstanceOf('Zend\View\Model\ModelInterface', $viewModel);
139+
$this->assertEmpty($viewModel->errorMessage);
140+
$this->assertNotEmpty($viewModel->repositories);
141+
}
142+
143+
public function testOrganizationActionReturnsErrorMessageWhenRepositoryRetrieverThrowsEdpGithubRuntimeException()
144+
{
145+
$this->zfcUserAuthentication->expects($this->once())
146+
->method('hasIdentity')
147+
->will($this->returnValue(true));
148+
149+
$routeMatch = new RouteMatch(['owner' => 'zendframework']);
150+
$this->controller->getEvent()->setRouteMatch($routeMatch);
151+
152+
$exception = new EdpGithubRuntimeException('EdpGithub throws this exception');
153+
154+
$this->repositoryRetriever->expects($this->once())
155+
->method('getUserRepositories')
156+
->will($this->throwException($exception));
157+
158+
$viewModel = $this->controller->organizationAction();
159+
$this->assertInstanceOf('Zend\View\Model\ModelInterface', $viewModel);
160+
$this->assertEquals($exception->getMessage(), $viewModel->errorMessage);
161+
$this->assertEmpty($viewModel->repositories);
162+
}
163+
164+
/**
165+
* @param Api\AbstractApi $apiInstance
166+
* @param array $payload
167+
* @return \PHPUnit_Framework_MockObject_MockObject
168+
*/
169+
private function getClientMock(Api\AbstractApi $apiInstance, array $payload = [])
170+
{
171+
$response = $this->getMock('Zend\Http\Response');
172+
173+
$response
174+
->expects($this->any())
175+
->method('getBody')
176+
->willReturn(json_encode($payload))
177+
;
178+
179+
$headers = $this->getMock('Zend\Http\Headers');
180+
181+
$response
182+
->expects($this->any())
183+
->method('getHeaders')
184+
->willReturn($headers)
185+
;
186+
187+
$httpClient = $this->getMock('EdpGithub\Http\Client');
188+
189+
$httpClient
190+
->expects($this->any())
191+
->method('get')
192+
->willReturn($response)
193+
;
194+
195+
$client = $this->getMock('EdpGithub\Client');
196+
197+
$client
198+
->expects($this->any())
199+
->method('getHttpClient')
200+
->willReturn($httpClient)
201+
;
202+
203+
$apiInstance->setClient($client);
204+
205+
$client
206+
->expects($this->any())
207+
->method('api')
208+
->willReturn($apiInstance)
209+
;
210+
211+
return $client;
212+
}
213+
}

0 commit comments

Comments
 (0)