Skip to content

Commit 34c1dff

Browse files
committed
Polish ClientTest to better cover authentication call
1 parent 283d4a9 commit 34c1dff

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

test/Github/Tests/ClientTest.php

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github\Tests;
44

55
use Github\Client;
6+
use Github\HttpClient\Listener\AuthListener;
67
use Github\Exception\InvalidArgumentException;
78

89
/**
@@ -25,67 +26,63 @@ public function shouldNotHaveToPassHttpClientToConstructor()
2526
/**
2627
* @test
2728
*/
28-
public function shouldAuthenticateUsingLoginAndPassword()
29+
public function shouldPassHttpClientInterfaceToConstructor()
2930
{
30-
$client = new Client();
31-
$client->authenticate('login', 'password', Client::AUTH_HTTP_PASSWORD);
31+
$client = new Client($this->getHttpClientMock());
3232

33-
$this->assertInstanceOf('Github\HttpClient\HttpClient', $client->getHttpClient());
34-
}
35-
36-
/**
37-
* @test
38-
*/
39-
public function shouldAuthenticateUsingHttpToken()
40-
{
41-
$client = new Client();
42-
$client->authenticate('login', 'password', Client::AUTH_HTTP_TOKEN);
43-
44-
$this->assertInstanceOf('Github\HttpClient\HttpClient', $client->getHttpClient());
33+
$this->assertInstanceOf('Github\HttpClient\HttpClientInterface', $client->getHttpClient());
4534
}
4635

4736
/**
4837
* @test
38+
* @dataProvider getAuthenticationData
4939
*/
50-
public function shouldAuthenticateUsingUrlToken()
40+
public function shouldAuthenticateUsingGivenParameters($login, $password, $method)
5141
{
52-
$client = new Client();
53-
$client->authenticate('login', 'password', Client::AUTH_URL_TOKEN);
42+
$httpClient = $this->getHttpClientMock(array('addListener'));
43+
$httpClient->expects($this->once())
44+
->method('addListener')
45+
->with(new AuthListener($method, array('tokenOrLogin' => $login, 'password' => $password)));
5446

55-
$this->assertInstanceOf('Github\HttpClient\HttpClient', $client->getHttpClient());
47+
$client = new Client($httpClient);
48+
$client->authenticate($login, $password, $method);
5649
}
5750

58-
/**
59-
* @test
60-
*/
61-
public function shouldAuthenticateUsingUrlClientId()
51+
public function getAuthenticationData()
6252
{
63-
$client = new Client();
64-
$client->authenticate('client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID);
65-
66-
$this->assertInstanceOf('Github\HttpClient\HttpClient', $client->getHttpClient());
53+
return array(
54+
array('login', null, null),
55+
array('login', 'password', Client::AUTH_HTTP_PASSWORD),
56+
array('token', null, Client::AUTH_HTTP_TOKEN),
57+
array('token', null, Client::AUTH_URL_TOKEN),
58+
array('client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID),
59+
);
6760
}
6861

6962
/**
7063
* @test
7164
*/
7265
public function shouldClearHeadersLazy()
7366
{
74-
$client = new Client();
75-
$client->clearHeaders();
67+
$httpClient = $this->getHttpClientMock(array('clearHeaders'));
68+
$httpClient->expects($this->once())->method('clearHeaders');
7669

77-
$this->assertInstanceOf('Github\HttpClient\HttpClientInterface', $client->getHttpClient());
70+
$client = new Client($httpClient);
71+
$client->clearHeaders();
7872
}
7973

8074
/**
8175
* @test
8276
*/
8377
public function shouldSetHeadersLaizly()
8478
{
85-
$client = new Client();
86-
$client->setHeaders(array('header1', 'header2'));
79+
$headers = array('header1', 'header2');
8780

88-
$this->assertInstanceOf('Github\HttpClient\HttpClientInterface', $client->getHttpClient());
81+
$httpClient = $this->getHttpClientMock();
82+
$httpClient->expects($this->once())->method('setHeaders')->with($headers);
83+
84+
$client = new Client($httpClient);
85+
$client->setHeaders($headers);
8986
}
9087

9188
/**
@@ -142,4 +139,14 @@ public function getApiClassesProvider()
142139
array('pull_requests', 'Github\Api\PullRequest'),
143140
);
144141
}
142+
143+
public function getHttpClientMock(array $methods = array())
144+
{
145+
$methods = array_merge(
146+
array('get', 'post', 'patch', 'put', 'delete', 'request', 'setOption', 'setHeaders'),
147+
$methods
148+
);
149+
150+
return $this->getMock('Github\HttpClient\HttpClientInterface', $methods);
151+
}
145152
}

0 commit comments

Comments
 (0)