Skip to content

Commit 00aac6b

Browse files
committed
Add unit tests for Github_Client, fix bugs
1 parent f9302ae commit 00aac6b

3 files changed

Lines changed: 132 additions & 4 deletions

File tree

‎lib/Github/Api/Abstract.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
77
* @license MIT License
88
*/
9-
abstract class Github_Api_Abstract implements Github_Api_Interface
9+
abstract class Github_Api_Abstract implements Github_ApiInterface
1010
{
1111
/**
1212
* The client

‎lib/Github/Client.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ public function getObjectApi()
224224
* Inject an API instance
225225
*
226226
* @param string $name the API name
227-
* @param Github_Api_Interface $api the API instance
227+
* @param Github_ApiInterface $api the API instance
228228
*
229229
* @return null
230230
*/
231-
public function setApi($name, Github_Api_Interface $instance)
231+
public function setApi($name, Github_ApiInterface $instance)
232232
{
233233
$this->apis[$name] = $instance;
234234

@@ -239,7 +239,7 @@ public function setApi($name, Github_Api_Interface $instance)
239239
* Get any API
240240
*
241241
* @param string $name the API name
242-
* @return Github_Api_Interface the API instance
242+
* @return Github_ApiInterface the API instance
243243
*/
244244
public function getApi($name)
245245
{

‎test/Github/Tests/ClientTest.php‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
class Github_Tests_ClientTest extends PHPUnit_Framework_TestCase
4+
{
5+
public function testInstanciateWithoutHttpClient()
6+
{
7+
$client = new Github_Client();
8+
9+
$this->assertInstanceOf('Github_HttpClientInterface', $client->getHttpClient());
10+
}
11+
12+
public function testInstanciateWithHttpClient()
13+
{
14+
$httpClient = $this->getHttpClientMock();
15+
$client = new Github_Client($httpClient);
16+
17+
$this->assertEquals($httpClient, $client->getHttpClient());
18+
}
19+
20+
public function testAuthenticate()
21+
{
22+
$login = 'login';
23+
$secret = 'secret';
24+
$method = 'method';
25+
26+
$httpClient = $this->getHttpClientMock();
27+
$httpClient->expects($this->exactly(3))
28+
->method('setOption')
29+
->will($this->returnValue($httpClient));
30+
31+
$client = $this->getClientMockBuilder()
32+
->setMethods(array('getHttpClient'))
33+
->getMock();
34+
$client->expects($this->once())
35+
->method('getHttpClient')
36+
->with()
37+
->will($this->returnValue($httpClient));
38+
39+
$client->authenticate($login, $secret, $method);
40+
}
41+
42+
public function testDeauthenticate()
43+
{
44+
$client = $this->getClientMockBuilder()
45+
->setMethods(array('authenticate'))
46+
->getMock();
47+
$client->expects($this->once())
48+
->method('authenticate')
49+
->with(null, null, null);
50+
51+
$client->deAuthenticate();
52+
}
53+
54+
public function testGet()
55+
{
56+
$route = '/some/route';
57+
$parameters = array('a' => 'b');
58+
$options = array('c' => 'd');
59+
60+
$httpClient = $this->getHttpClientMock();
61+
$httpClient->expects($this->once())
62+
->method('get')
63+
->with($route, $parameters, $options);
64+
65+
$client = $this->getClientMockBuilder()
66+
->setMethods(array('getHttpClient'))
67+
->getMock();
68+
$client->expects($this->once())
69+
->method('getHttpClient')
70+
->with()
71+
->will($this->returnValue($httpClient));
72+
73+
$client->get($route, $parameters, $options);
74+
}
75+
76+
public function testPost()
77+
{
78+
$route = '/some/route';
79+
$parameters = array('a' => 'b');
80+
$options = array('c' => 'd');
81+
82+
$httpClient = $this->getHttpClientMock();
83+
$httpClient->expects($this->once())
84+
->method('post')
85+
->with($route, $parameters, $options);
86+
87+
$client = $this->getClientMockBuilder()
88+
->setMethods(array('getHttpClient'))
89+
->getMock();
90+
$client->expects($this->once())
91+
->method('getHttpClient')
92+
->with()
93+
->will($this->returnValue($httpClient));
94+
95+
$client->post($route, $parameters, $options);
96+
}
97+
98+
public function testDefaultApi()
99+
{
100+
$client = new Github_Client();
101+
102+
$this->assertInstanceOf('Github_Api_User', $client->getUserApi());
103+
}
104+
105+
public function testInjectApi()
106+
{
107+
$client = new Github_Client();
108+
109+
$userApiMock = $this->getMockBuilder('Github_ApiInterface')
110+
->getMock();
111+
112+
$client->setApi('user', $userApiMock);
113+
114+
$this->assertSame($userApiMock, $client->getUserApi());
115+
}
116+
117+
protected function getClientMockBuilder()
118+
{
119+
return $this->getMockBuilder('Github_Client')
120+
->disableOriginalConstructor();
121+
}
122+
123+
protected function getHttpClientMock()
124+
{
125+
return $this->getMockBuilder('Github_HttpClientInterface')
126+
->getMock();
127+
}
128+
}

0 commit comments

Comments
 (0)