forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.php
More file actions
211 lines (173 loc) · 5.8 KB
/
ClientTest.php
File metadata and controls
211 lines (173 loc) · 5.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace Github\Tests;
use Github\Client;
use Github\Exception\InvalidArgumentException;
use Github\Exception\BadMethodCallException;
class ClientTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function shouldNotHaveToPassHttpClientToConstructor()
{
$client = new Client();
$this->assertInstanceOf('Github\HttpClient\HttpClient', $client->getHttpClient());
}
/**
* @test
*/
public function shouldPassHttpClientInterfaceToConstructor()
{
$client = new Client($this->getHttpClientMock());
$this->assertInstanceOf('Github\HttpClient\HttpClientInterface', $client->getHttpClient());
}
/**
* @test
* @dataProvider getAuthenticationFullData
*/
public function shouldAuthenticateUsingAllGivenParameters($login, $password, $method)
{
$httpClient = $this->getHttpClientMock();
$httpClient->expects($this->once())
->method('authenticate')
->with($login, $password, $method);
$client = new Client($httpClient);
$client->authenticate($login, $password, $method);
}
public function getAuthenticationFullData()
{
return array(
array('login', 'password', Client::AUTH_HTTP_PASSWORD),
array('token', null, Client::AUTH_HTTP_TOKEN),
array('token', null, Client::AUTH_URL_TOKEN),
array('client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID),
);
}
/**
* @test
* @dataProvider getAuthenticationPartialData
*/
public function shouldAuthenticateUsingGivenParameters($token, $method)
{
$httpClient = $this->getHttpClientMock();
$httpClient->expects($this->once())
->method('authenticate')
->with($token, null, $method);
$client = new Client($httpClient);
$client->authenticate($token, $method);
}
public function getAuthenticationPartialData()
{
return array(
array('token', Client::AUTH_HTTP_TOKEN),
array('token', Client::AUTH_URL_TOKEN),
);
}
/**
* @test
* @expectedException InvalidArgumentException
*/
public function shouldThrowExceptionWhenAuthenticatingWithoutMethodSet()
{
$httpClient = $this->getHttpClientMock(array('addListener'));
$client = new Client($httpClient);
$client->authenticate('login', null, null);
}
/**
* @test
*/
public function shouldClearHeadersLazy()
{
$httpClient = $this->getHttpClientMock(array('clearHeaders'));
$httpClient->expects($this->once())->method('clearHeaders');
$client = new Client($httpClient);
$client->clearHeaders();
}
/**
* @test
*/
public function shouldSetHeadersLaizly()
{
$headers = array('header1', 'header2');
$httpClient = $this->getHttpClientMock();
$httpClient->expects($this->once())->method('setHeaders')->with($headers);
$client = new Client($httpClient);
$client->setHeaders($headers);
}
/**
* @test
* @dataProvider getApiClassesProvider
*/
public function shouldGetApiInstance($apiName, $class)
{
$client = new Client();
$this->assertInstanceOf($class, $client->api($apiName));
}
/**
* @test
* @dataProvider getApiClassesProvider
*/
public function shouldGetMagicApiInstance($apiName, $class)
{
$client = new Client();
$this->assertInstanceOf($class, $client->$apiName());
}
/**
* @test
* @expectedException InvalidArgumentException
*/
public function shouldNotGetApiInstance()
{
$client = new Client();
$client->api('do_not_exist');
}
/**
* @test
* @expectedException BadMethodCallException
*/
public function shouldNotGetMagicApiInstance()
{
$client = new Client();
$client->doNotExist();
}
public function getApiClassesProvider()
{
return array(
array('user', 'Github\Api\User'),
array('users', 'Github\Api\User'),
array('me', 'Github\Api\CurrentUser'),
array('current_user', 'Github\Api\CurrentUser'),
array('currentUser', 'Github\Api\CurrentUser'),
array('git', 'Github\Api\GitData'),
array('git_data', 'Github\Api\GitData'),
array('gitData', 'Github\Api\GitData'),
array('gist', 'Github\Api\Gists'),
array('gists', 'Github\Api\Gists'),
array('issue', 'Github\Api\Issue'),
array('issues', 'Github\Api\Issue'),
array('markdown', 'Github\Api\Markdown'),
array('organization', 'Github\Api\Organization'),
array('organizations', 'Github\Api\Organization'),
array('repo', 'Github\Api\Repo'),
array('repos', 'Github\Api\Repo'),
array('repository', 'Github\Api\Repo'),
array('repositories', 'Github\Api\Repo'),
array('pr', 'Github\Api\PullRequest'),
array('pullRequest', 'Github\Api\PullRequest'),
array('pull_request', 'Github\Api\PullRequest'),
array('pullRequests', 'Github\Api\PullRequest'),
array('pull_requests', 'Github\Api\PullRequest'),
array('authorization', 'Github\Api\Authorizations'),
array('authorizations', 'Github\Api\Authorizations'),
array('meta', 'Github\Api\Meta')
);
}
public function getHttpClientMock(array $methods = array())
{
$methods = array_merge(
array('get', 'post', 'patch', 'put', 'delete', 'request', 'setOption', 'setHeaders', 'authenticate'),
$methods
);
return $this->getMock('Github\HttpClient\HttpClientInterface', $methods);
}
}