forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultPagerTest.php
More file actions
119 lines (100 loc) · 3.33 KB
/
ResultPagerTest.php
File metadata and controls
119 lines (100 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Github\Tests;
use Github\Api\Organization\Members;
use Github\Api\Search;
use Github\ResultPager;
use Github\Tests\Mock\PaginatedResponse;
use Http\Client\HttpClient;
/**
* ResultPagerTest.
*
* @author Ramon de la Fuente <[email protected]>
* @author Mitchel Verschoof <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class ResultPagerTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
*
* description fetchAll
*/
public function shouldGetAllResults()
{
$amountLoops = 3;
$content = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$response = new PaginatedResponse($amountLoops, $content);
// httpClient mock
$httpClientMock = $this->getMockBuilder(\Http\Client\HttpClient::class)
->setMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly($amountLoops))
->method('sendRequest')
->will($this->returnValue($response));
$client = \Github\Client::createWithHttpClient($httpClientMock);
// memberApi Mock
$memberApi = new Members($client);
$method = 'all';
$parameters = ['netwerven'];
// Run fetchAll on result paginator
$paginator = new ResultPager($client);
$result = $paginator->fetchAll($memberApi, $method, $parameters);
$this->assertCount($amountLoops * count($content), $result);
}
/**
* @test
*
* response in a search api has different format:
*
* {
* "total_count": 1,
* "incomplete_results": false,
* "items": []
* }
*
* and we need to extract result from `items`
*/
public function shouldGetAllSearchResults()
{
$amountLoops = 3;
$content = [
'total_count' => 12,
'items' => [1, 2, 3, 4],
];
$response = new PaginatedResponse($amountLoops, $content);
// httpClient mock
$httpClientMock = $this->getMockBuilder(\Http\Client\HttpClient::class)
->setMethods(['sendRequest'])
->getMock();
$httpClientMock
->expects($this->exactly($amountLoops))
->method('sendRequest')
->will($this->returnValue($response));
$client = \Github\Client::createWithHttpClient($httpClientMock);
$searchApi = new Search($client);
$method = 'users';
$paginator = new ResultPager($client);
$result = $paginator->fetchAll($searchApi, $method, ['knplabs']);
$this->assertCount($amountLoops * count($content['items']), $result);
}
public function testFetch()
{
$result = 'foo';
$method = 'bar';
$parameters = ['baz'];
$api = $this->getMockBuilder(\Github\Api\ApiInterface::class)
->getMock();
$paginator = $this->getMockBuilder(\Github\ResultPager::class)
->disableOriginalConstructor()
->setMethods(['callApi', 'postFetch'])
->getMock();
$paginator->expects($this->once())
->method('callApi')
->with($api, $method, $parameters)
->willReturn($result);
$paginator->expects($this->once())
->method('postFetch');
$this->assertEquals($result, $paginator->fetch($api, $method, $parameters));
}
}