Skip to content

Commit d1d9af8

Browse files
committed
Allow to easier skip $password parameter when calling authenticate() method.
1 parent 0880dbd commit d1d9af8

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

lib/Github/Client.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,22 @@ public function api($name)
144144
* Authenticate a user for all next requests
145145
*
146146
* @param string $tokenOrLogin GitHub private token/username/client ID
147-
* @param null|string $password GitHub password/secret
147+
* @param null|string $password GitHub password/secret (optionally can contain $authMethod)
148148
* @param null|string $authMethod One of the AUTH_* class constants
149+
*
150+
* @throws InvalidArgumentException If no authentication method was given
149151
*/
150152
public function authenticate($tokenOrLogin, $password = null, $authMethod = null)
151153
{
154+
if (null === $password && null === $authMethod) {
155+
throw new InvalidArgumentException('You need to specify authentication method!');
156+
}
157+
158+
if (null === $authMethod && in_array($password, array(self::AUTH_URL_TOKEN, self::AUTH_URL_CLIENT_ID, self::AUTH_HTTP_PASSWORD, self::AUTH_HTTP_TOKEN))) {
159+
$authMethod = $password;
160+
$password = null;
161+
}
162+
152163
$this->httpClient->addListener(
153164
new AuthListener(
154165
$authMethod,

test/Github/Tests/ClientTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function shouldPassHttpClientInterfaceToConstructor()
3030

3131
/**
3232
* @test
33-
* @dataProvider getAuthenticationData
33+
* @dataProvider getAuthenticationFullData
3434
*/
35-
public function shouldAuthenticateUsingGivenParameters($login, $password, $method)
35+
public function shouldAuthenticateUsingAllGivenParameters($login, $password, $method)
3636
{
3737
$httpClient = $this->getHttpClientMock(array('addListener'));
3838
$httpClient->expects($this->once())
@@ -43,17 +43,51 @@ public function shouldAuthenticateUsingGivenParameters($login, $password, $metho
4343
$client->authenticate($login, $password, $method);
4444
}
4545

46-
public function getAuthenticationData()
46+
public function getAuthenticationFullData()
4747
{
4848
return array(
49-
array('login', null, null),
5049
array('login', 'password', Client::AUTH_HTTP_PASSWORD),
5150
array('token', null, Client::AUTH_HTTP_TOKEN),
5251
array('token', null, Client::AUTH_URL_TOKEN),
5352
array('client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID),
5453
);
5554
}
5655

56+
/**
57+
* @test
58+
* @dataProvider getAuthenticationPartialData
59+
*/
60+
public function shouldAuthenticateUsingGivenParameters($token, $method)
61+
{
62+
$httpClient = $this->getHttpClientMock(array('addListener'));
63+
$httpClient->expects($this->once())
64+
->method('addListener')
65+
->with(new AuthListener($method, array('tokenOrLogin' => $token, 'password' => null)));
66+
67+
$client = new Client($httpClient);
68+
$client->authenticate($token, $method);
69+
}
70+
71+
public function getAuthenticationPartialData()
72+
{
73+
return array(
74+
array('token', Client::AUTH_HTTP_TOKEN),
75+
array('token', Client::AUTH_URL_TOKEN),
76+
);
77+
}
78+
79+
/**
80+
* @test
81+
* @expectedException InvalidArgumentException
82+
*/
83+
public function shouldThrowExceptionWhenAuthenticatingWithoutMethodSet()
84+
{
85+
$httpClient = $this->getHttpClientMock(array('addListener'));
86+
87+
$client = new Client($httpClient);
88+
$client->authenticate('login', null, null);
89+
}
90+
5791
/**
5892
* @test
5993
*/

0 commit comments

Comments
 (0)