2
2
3
3
namespace Github \Tests ;
4
4
5
+ use Github \Api \Issue ;
5
6
use Github \Api \Organization \Members ;
7
+ use Github \Api \Repository \Statuses ;
6
8
use Github \Api \Search ;
9
+ use Github \Client ;
7
10
use Github \ResultPager ;
8
11
use Github \Tests \Mock \PaginatedResponse ;
12
+ use GuzzleHttp \Psr7 \Response ;
13
+ use GuzzleHttp \Psr7 \Utils ;
9
14
use Http \Client \HttpClient ;
10
15
use Psr \Http \Client \ClientInterface ;
11
16
@@ -37,14 +42,14 @@ public function shouldGetAllResults(string $fetchMethod)
37
42
38
43
// httpClient mock
39
44
$ httpClientMock = $ this ->getMockBuilder (ClientInterface::class)
40
- ->setMethods (['sendRequest ' ])
45
+ ->onlyMethods (['sendRequest ' ])
41
46
->getMock ();
42
47
$ httpClientMock
43
48
->expects ($ this ->exactly ($ amountLoops ))
44
49
->method ('sendRequest ' )
45
50
->will ($ this ->returnValue ($ response ));
46
51
47
- $ client = \ Github \ Client::createWithHttpClient ($ httpClientMock );
52
+ $ client = Client::createWithHttpClient ($ httpClientMock );
48
53
49
54
// memberApi Mock
50
55
$ memberApi = new Members ($ client );
@@ -91,14 +96,14 @@ public function shouldGetAllSearchResults()
91
96
92
97
// httpClient mock
93
98
$ httpClientMock = $ this ->getMockBuilder (ClientInterface::class)
94
- ->setMethods (['sendRequest ' ])
99
+ ->onlyMethods (['sendRequest ' ])
95
100
->getMock ();
96
101
$ httpClientMock
97
102
->expects ($ this ->exactly ($ amountLoops ))
98
103
->method ('sendRequest ' )
99
- ->will ( $ this -> returnValue ( $ response) );
104
+ ->willReturn ( $ response );
100
105
101
- $ client = \ Github \ Client::createWithHttpClient ($ httpClientMock );
106
+ $ client = Client::createWithHttpClient ($ httpClientMock );
102
107
103
108
$ searchApi = new Search ($ client );
104
109
$ method = 'users ' ;
@@ -115,7 +120,7 @@ public function testFetch()
115
120
$ parameters = ['baz ' ];
116
121
$ api = $ this ->getMockBuilder (Members::class)
117
122
->disableOriginalConstructor ()
118
- ->setMethods (['all ' ])
123
+ ->onlyMethods (['all ' ])
119
124
->getMock ();
120
125
$ api ->expects ($ this ->once ())
121
126
->method ('all ' )
@@ -124,12 +129,72 @@ public function testFetch()
124
129
125
130
$ paginator = $ this ->getMockBuilder (ResultPager::class)
126
131
->disableOriginalConstructor ()
127
- ->setMethods (['postFetch ' ])
132
+ ->onlyMethods (['postFetch ' ])
128
133
->getMock ();
129
134
130
135
$ paginator ->expects ($ this ->once ())
131
136
->method ('postFetch ' );
132
137
133
138
$ this ->assertEquals ($ result , $ paginator ->fetch ($ api , $ method , $ parameters ));
134
139
}
140
+
141
+ public function testFetchAllPreserveKeys ()
142
+ {
143
+ $ content = [
144
+ 'state ' => 'success ' ,
145
+ 'statuses ' => [
146
+ ['description ' => 'status 1 ' , 'state ' => 'success ' ],
147
+ ['description ' => 'status 2 ' , 'state ' => 'failure ' ],
148
+ ],
149
+ 'sha ' => '43068834af7e501778708ed13106de95f782328c ' ,
150
+ ];
151
+
152
+ $ response = new Response (200 , ['Content-Type ' =>'application/json ' ], Utils::streamFor (json_encode ($ content )));
153
+
154
+ // httpClient mock
155
+ $ httpClientMock = $ this ->getMockBuilder (HttpClient::class)
156
+ ->onlyMethods (['sendRequest ' ])
157
+ ->getMock ();
158
+ $ httpClientMock
159
+ ->method ('sendRequest ' )
160
+ ->willReturn ($ response );
161
+
162
+ $ client = Client::createWithHttpClient ($ httpClientMock );
163
+
164
+ $ api = new Statuses ($ client );
165
+ $ paginator = new ResultPager ($ client );
166
+ $ result = $ paginator ->fetchAll ($ api , 'combined ' , ['knplabs ' , 'php-github-api ' , '43068834af7e501778708ed13106de95f782328c ' ]);
167
+
168
+ $ this ->assertArrayHasKey ('state ' , $ result );
169
+ $ this ->assertArrayHasKey ('statuses ' , $ result );
170
+ $ this ->assertCount (2 , $ result ['statuses ' ]);
171
+ }
172
+
173
+ public function testFetchAllWithoutKeys ()
174
+ {
175
+ $ content = [
176
+ ['title ' => 'issue 1 ' ],
177
+ ['title ' => 'issue 2 ' ],
178
+ ['title ' => 'issue 3 ' ],
179
+ ];
180
+
181
+ $ response = new PaginatedResponse (3 , $ content );
182
+
183
+ // httpClient mock
184
+ $ httpClientMock = $ this ->getMockBuilder (HttpClient::class)
185
+ ->onlyMethods (['sendRequest ' ])
186
+ ->getMock ();
187
+ $ httpClientMock
188
+ ->expects ($ this ->exactly (3 ))
189
+ ->method ('sendRequest ' )
190
+ ->willReturn ($ response );
191
+
192
+ $ client = Client::createWithHttpClient ($ httpClientMock );
193
+
194
+ $ api = new Issue ($ client );
195
+ $ paginator = new ResultPager ($ client );
196
+ $ result = $ paginator ->fetchAll ($ api , 'all ' , ['knplabs ' , 'php-github-api ' ]);
197
+
198
+ $ this ->assertCount (9 , $ result );
199
+ }
135
200
}
0 commit comments