5
5
use Github ;
6
6
use Github \Client ;
7
7
use Github \ResultPager ;
8
+ use Github \HttpClient \HttpClientInterface ;
9
+ use Github \Tests \Mock \TestResponse ;
8
10
9
11
/**
10
12
* ResultPagerTest
@@ -21,12 +23,63 @@ class ResultPagerTest extends \PHPUnit_Framework_TestCase
21
23
*/
22
24
public function shouldGetAllResults ()
23
25
{
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 ' ;
27
65
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 ());
30
83
}
31
84
32
85
/**
@@ -36,7 +89,27 @@ public function shouldGetAllResults()
36
89
*/
37
90
public function postFetch ()
38
91
{
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 ));
39
105
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 );
40
113
}
41
114
42
115
/**
@@ -46,96 +119,133 @@ public function postFetch()
46
119
*/
47
120
public function fetchNext ()
48
121
{
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 ));
49
142
143
+ $ client = $ this ->getClientMock ($ httpClient );
144
+
145
+ $ paginator = new Github \ResultPager ( $ client );
146
+ $ paginator ->postFetch ();
147
+
148
+ $ this ->assertEquals ($ paginator ->fetchNext (), $ resultContent );
50
149
}
51
150
52
151
/**
53
152
* @test
54
153
*
55
154
* description hasNext
56
155
*/
57
- public function shouldHasNext ()
156
+ public function shouldHaveNext ()
58
157
{
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 ();
59
164
165
+ $ this ->assertEquals ($ paginator ->hasNext (), true );
166
+ $ this ->assertEquals ($ paginator ->hasPrevious (), false );
60
167
}
61
168
62
169
/**
63
170
* @test
64
171
*
65
172
* description hasPrevious
66
173
*/
67
- public function shouldHasPrevious ()
174
+ public function shouldHavePrevious ()
68
175
{
176
+ $ responseMock = $ this ->getResponseMock (array ('prev ' => 'http://github.com/previous ' ));
177
+ $ httpClient = $ this ->getHttpClientMock ( $ responseMock );
178
+ $ client = $ this ->getClientMock ($ httpClient );
69
179
180
+ $ paginator = new Github \ResultPager ( $ client );
181
+ $ paginator ->postFetch ();
182
+
183
+ $ this ->assertEquals ($ paginator ->hasPrevious (), true );
184
+ $ this ->assertEquals ($ paginator ->hasNext (), false );
70
185
}
71
186
72
- /**
73
- * @test
74
- *
75
- * description first
76
- */
77
- public function shouldHasFirst ()
187
+ protected function getResponseMock ( array $ pagination )
78
188
{
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
+ ));
79
197
198
+ return $ responseMock ;
80
199
}
81
200
82
- /**
83
- * @test
84
- *
85
- * description last
86
- */
87
- public function shouldHasLast ()
201
+ protected function getClientMock ( HttpClientInterface $ httpClient = null )
88
202
{
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 );
89
210
211
+ return $ client ;
90
212
}
91
213
92
- protected function getApiMock ( $ apiClass )
214
+ protected function getHttpClientMock ( $ responseMock = null )
93
215
{
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
106
219
->expects ($ this ->any ())
107
220
->method ('setTimeout ' )
108
221
->with (10 );
109
- $ httpClient
222
+ $ clientInterfaceMock
110
223
->expects ($ this ->any ())
111
224
->method ('setVerifyPeer ' )
112
225
->with (false );
113
- $ httpClient
226
+ $ clientInterfaceMock
114
227
->expects ($ this ->any ())
115
228
->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
- ));
125
229
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 ));
127
232
128
- var_dump ( $ mock ->getLastResponse (), $ mock );
233
+ if ( $ responseMock ){
234
+ $ httpClientMock
235
+ ->expects ($ this ->any ())
236
+ ->method ('getLastResponse ' )
237
+ ->will ($ this ->returnValue ($ responseMock ));
238
+ }
129
239
130
- $ client = new \ Github \ Client ( $ mock ) ;
131
- $ client -> setHttpClient ( $ mock );
240
+ return $ httpClientMock ;
241
+ }
132
242
133
- var_dump ( $ client ->getHttpClient ()->getLastResponse () );
243
+ protected function getApiMock ( $ apiClass )
244
+ {
245
+ $ client = $ this ->getClientMock ();
134
246
135
247
return $ this ->getMockBuilder ( $ apiClass )
136
- ->setMethods (array ('get ' , 'post ' , 'patch ' , 'delete ' , 'put ' ))
137
248
->setConstructorArgs (array ($ client ))
138
249
->getMock ();
139
250
}
140
-
141
251
}
0 commit comments