Skip to content

Commit 5541dec

Browse files
committed
Added TestResponse Mock to test the FetchAll
Added UnitTest
1 parent 84b76eb commit 5541dec

File tree

2 files changed

+183
-67
lines changed

2 files changed

+183
-67
lines changed

test/Github/Tests/Mock/TestResponse.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,43 @@
22

33
namespace Github\Tests\Mock;
44

5-
use Buzz\Message\Response as BaseResponse;
6-
use Github\Exception\ApiLimitExceedException;
7-
8-
class TestResponse extends BaseResponse
5+
class TestResponse
96
{
10-
/**
11-
* @var integer
12-
*/
13-
public $remainingCalls;
7+
protected $loopCount;
8+
9+
protected $content;
10+
11+
public function __construct( $loopCount, array $content = array() )
12+
{
13+
$this->loopCount = $loopCount;
14+
$this->content = $content;
15+
}
1416

1517
/**
1618
* {@inheritDoc}
1719
*/
1820
public function getContent()
1921
{
20-
return null;
22+
return $this->content;
2123
}
2224

2325
/**
2426
* @return array|null
2527
*/
2628
public function getPagination()
2729
{
28-
return null;
29-
}
30+
if($this->loopCount){
31+
$returnArray = array(
32+
'next' => 'http://github.com/' . $this->loopCount
33+
);
34+
} else {
35+
$returnArray = array(
36+
'prev' => 'http://github.com/prev'
37+
);
38+
}
3039

31-
/**
32-
* {@inheritDoc}
33-
*/
34-
public function getApiLimit()
35-
{
36-
return null;
40+
$this->loopCount--;
41+
42+
return $returnArray;
3743
}
3844
}

test/Github/Tests/ResultPagerTest.php

Lines changed: 160 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Github;
66
use Github\Client;
77
use Github\ResultPager;
8+
use Github\HttpClient\HttpClientInterface;
9+
use Github\Tests\Mock\TestResponse;
810

911
/**
1012
* ResultPagerTest
@@ -21,12 +23,63 @@ class ResultPagerTest extends \PHPUnit_Framework_TestCase
2123
*/
2224
public function shouldGetAllResults()
2325
{
24-
$organizationMockApi = $this->getApiMock( 'Github\Api\Organization' );
25-
$method = 'all';
26-
$parameters = array('netwerven');
26+
$amountLoops = 3;
27+
$content = array(1,2,3,4,5,6,7,8,9,10);
28+
$responseMock = new TestResponse( $amountLoops, $content );
29+
30+
// httpClient mock
31+
$httpClientMock = $this->getHttpClientMock($responseMock);
32+
$httpClientMock
33+
->expects($this->exactly($amountLoops))
34+
->method('get')
35+
->will($this->returnValue($responseMock));
36+
37+
$clientMock = $this->getClientMock($httpClientMock);
38+
39+
// memberApi Mock
40+
$memberApiMock = $this->getApiMock( 'Github\Api\Organization\Members' );
41+
$memberApiMock
42+
->expects($this->once())
43+
->method('all')
44+
->will($this->returnValue(array()));
45+
46+
$method = 'all';
47+
$parameters = array('netwerven');
48+
49+
// Run fetchAll on result paginator
50+
$paginator = new Github\ResultPager( $clientMock );
51+
$result = $paginator->fetchAll( $memberApiMock, $method, $parameters );
52+
53+
$this->assertEquals($amountLoops * count($content), count($result));
54+
}
55+
56+
/**
57+
* @test
58+
*
59+
* description fetch
60+
*/
61+
public function shouldGetSomeResults()
62+
{
63+
$pagination = array('next' => 'http://github.com/next');
64+
$resultContent = 'organization test';
2765

28-
// $paginator = new Github\ResultPaginator( $client );
29-
// $result = $paginator->fetchAll( $organizationMockApi, 'repositories', $parameters );
66+
$responseMock = $this->getResponseMock($pagination);
67+
$httpClient = $this->getHttpClientMock( $responseMock );
68+
$client = $this->getClientMock($httpClient);
69+
70+
$organizationApiMock = $this->getApiMock( 'Github\Api\Organization' );
71+
72+
$organizationApiMock
73+
->expects($this->once())
74+
->method('show')
75+
->with('github')
76+
->will($this->returnValue($resultContent));
77+
78+
$paginator = new Github\ResultPager( $client );
79+
$result = $paginator->fetch($organizationApiMock, 'show', 'github');
80+
81+
$this->assertEquals($resultContent, $result);
82+
$this->assertEquals($pagination, $paginator->getPagination());
3083
}
3184

3285
/**
@@ -36,7 +89,27 @@ public function shouldGetAllResults()
3689
*/
3790
public function postFetch()
3891
{
92+
$pagination = array(
93+
'first' => 'http://github.com',
94+
'next' => 'http://github.com',
95+
'prev' => 'http://github.com',
96+
'last' => 'http://github.com'
97+
);
98+
99+
// response mock
100+
$responseMock = $this->getMock('Github\HttpClient\Message\Response');
101+
$responseMock
102+
->expects($this->any())
103+
->method('getPagination')
104+
->will($this->returnValue($pagination));
39105

106+
$httpClient = $this->getHttpClientMock( $responseMock );
107+
$client = $this->getClientMock($httpClient);
108+
109+
$paginator = new Github\ResultPager( $client );
110+
$paginator->postFetch();
111+
112+
$this->assertEquals($paginator->getPagination(), $pagination);
40113
}
41114

42115
/**
@@ -46,96 +119,133 @@ public function postFetch()
46119
*/
47120
public function fetchNext()
48121
{
122+
$pagination = array('next' => 'http://github.com/next');
123+
$resultContent = 'fetch test';
124+
125+
$responseMock = $this->getResponseMock( $pagination );
126+
$responseMock
127+
->expects($this->once())
128+
->method('getContent')
129+
->will($this->returnValue($resultContent));
130+
// Expected 2 times, 1 for setup and 1 for the actual test
131+
$responseMock
132+
->expects($this->exactly(2))
133+
->method('getPagination');
134+
135+
$httpClient = $this->getHttpClientMock( $responseMock );
136+
137+
$httpClient
138+
->expects($this->once())
139+
->method('get')
140+
->with( $pagination['next'] )
141+
->will($this->returnValue($responseMock));
49142

143+
$client = $this->getClientMock($httpClient);
144+
145+
$paginator = new Github\ResultPager( $client );
146+
$paginator->postFetch();
147+
148+
$this->assertEquals($paginator->fetchNext(), $resultContent);
50149
}
51150

52151
/**
53152
* @test
54153
*
55154
* description hasNext
56155
*/
57-
public function shouldHasNext()
156+
public function shouldHaveNext()
58157
{
158+
$responseMock = $this->getResponseMock(array('next' => 'http://github.com/next'));
159+
$httpClient = $this->getHttpClientMock( $responseMock );
160+
$client = $this->getClientMock($httpClient);
161+
162+
$paginator = new Github\ResultPager( $client );
163+
$paginator->postFetch();
59164

165+
$this->assertEquals($paginator->hasNext(), true);
166+
$this->assertEquals($paginator->hasPrevious(), false);
60167
}
61168

62169
/**
63170
* @test
64171
*
65172
* description hasPrevious
66173
*/
67-
public function shouldHasPrevious()
174+
public function shouldHavePrevious()
68175
{
176+
$responseMock = $this->getResponseMock(array('prev' => 'http://github.com/previous'));
177+
$httpClient = $this->getHttpClientMock( $responseMock );
178+
$client = $this->getClientMock($httpClient);
69179

180+
$paginator = new Github\ResultPager( $client );
181+
$paginator->postFetch();
182+
183+
$this->assertEquals($paginator->hasPrevious(), true);
184+
$this->assertEquals($paginator->hasNext(), false);
70185
}
71186

72-
/**
73-
* @test
74-
*
75-
* description first
76-
*/
77-
public function shouldHasFirst()
187+
protected function getResponseMock( array $pagination )
78188
{
189+
// response mock
190+
$responseMock = $this->getMock('Github\HttpClient\Message\Response');
191+
$responseMock
192+
->expects($this->any())
193+
->method('getPagination')
194+
->will($this->returnValue(
195+
$pagination
196+
));
79197

198+
return $responseMock;
80199
}
81200

82-
/**
83-
* @test
84-
*
85-
* description last
86-
*/
87-
public function shouldHasLast()
201+
protected function getClientMock( HttpClientInterface $httpClient = null )
88202
{
203+
// if no httpClient isset use the default HttpClient mock
204+
if( !$httpClient ){
205+
$httpClient = $this->getHttpClientMock();
206+
}
207+
208+
$client = new \Github\Client($httpClient);
209+
$client->setHttpClient($httpClient);
89210

211+
return $client;
90212
}
91213

92-
protected function getApiMock( $apiClass )
214+
protected function getHttpClientMock( $responseMock = null )
93215
{
94-
$responseStub = $this->getMock('Github\HttpClient\Message\Response', array('getPagination'));
95-
$responseStub
96-
->expects($this->any())
97-
->method('getPagination')
98-
->with(array('test' => 'test'));
99-
100-
var_dump( "\n" );
101-
var_dump( $responseStub );
102-
exit;
103-
104-
$httpClient = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send', 'getLastResponse'));
105-
$httpClient
216+
// mock the client interface
217+
$clientInterfaceMock = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send'));
218+
$clientInterfaceMock
106219
->expects($this->any())
107220
->method('setTimeout')
108221
->with(10);
109-
$httpClient
222+
$clientInterfaceMock
110223
->expects($this->any())
111224
->method('setVerifyPeer')
112225
->with(false);
113-
$httpClient
226+
$clientInterfaceMock
114227
->expects($this->any())
115228
->method('send');
116-
$httpClient
117-
->expects($this->any())
118-
->method('getLastResponse')
119-
->with(array(
120-
'first' => 'test',
121-
'next' => 'test',
122-
'previous' => 'test',
123-
'last' => 'test',
124-
));
125229

126-
$mock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $httpClient));
230+
// create the httpClient mock
231+
$httpClientMock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $clientInterfaceMock));
127232

128-
var_dump( $mock->getLastResponse(), $mock );
233+
if( $responseMock ){
234+
$httpClientMock
235+
->expects($this->any())
236+
->method('getLastResponse')
237+
->will($this->returnValue($responseMock));
238+
}
129239

130-
$client = new \Github\Client($mock);
131-
$client->setHttpClient($mock);
240+
return $httpClientMock;
241+
}
132242

133-
var_dump( $client->getHttpClient()->getLastResponse() );
243+
protected function getApiMock( $apiClass )
244+
{
245+
$client = $this->getClientMock();
134246

135247
return $this->getMockBuilder( $apiClass )
136-
->setMethods(array('get', 'post', 'patch', 'delete', 'put'))
137248
->setConstructorArgs(array($client))
138249
->getMock();
139250
}
140-
141251
}

0 commit comments

Comments
 (0)