Skip to content

Commit 82b1a54

Browse files
author
Evgeniy Guseletov
committed
Merge pull request #52 from christofdamian/authorizations
Implement Authorizations API
2 parents 51f35f8 + 32b6ff3 commit 82b1a54

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

doc/authorizations.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Authorizations API
2+
[Back to the navigation](index.md)
3+
4+
Creating, deleting and listing authorizations. Wraps [GitHub Authorizations API](http://developer.github.com/v3/oauth/).
5+
6+
#### List all authorizations.
7+
8+
```php
9+
$authorizations = $github->api('authorizations')->all();
10+
```
11+
12+
#### Get a single authorization
13+
14+
```php
15+
$authorization = $github->api('authorizations')->show(1);
16+
```
17+
18+
#### Create an authorization
19+
20+
```php
21+
$data = array(
22+
'note' => 'This is an optional description'
23+
);
24+
25+
$authorization = $github->api('authorizations')->create($data);
26+
```
27+
28+
Creates and returns an authorization.
29+
30+
#### Update an authorization
31+
32+
You can update ``note``.
33+
34+
```php
35+
$data = array(
36+
'note' => 'This is new note'
37+
);
38+
39+
$authorization = $github->api('authorizations')->update(1234, $data);
40+
```
41+
42+
#### Delete an authorization
43+
44+
```php
45+
$authorization = $github->api('authorizations')->remove(1234);
46+
```
47+
48+
#### Check an authorization
49+
50+
```php
51+
$authorization = $github->api('authorizations')->check(1234, 'token');
52+
```

doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Navigation
22
==========
33

44
APIs:
5+
* [Authorizations](authorizations.md)
56
* [Commits](commits.md)
67
* [Gists](gists.md)
78
* [Issues](issues.md)

lib/Github/Api/Authorizations.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* Creating, deleting and listing authorizations
9+
*
10+
* @link http://developer.github.com/v3/oauth/
11+
*/
12+
class Authorizations extends AbstractApi
13+
{
14+
public function all()
15+
{
16+
return $this->get('authorizations');
17+
}
18+
19+
public function show($number)
20+
{
21+
return $this->get('authorizations/'.urlencode($number));
22+
}
23+
24+
public function create(array $params)
25+
{
26+
return $this->post('authorizations', $params);
27+
}
28+
29+
public function update($id, array $params)
30+
{
31+
return $this->patch('authorizations/'.urlencode($id), $params);
32+
}
33+
34+
public function remove($id)
35+
{
36+
return $this->delete('authorizations/'.urlencode($id));
37+
}
38+
39+
public function check($id, $token)
40+
{
41+
return $this->get('authorizations/'.urlencode($id).'/tokens/'.urlencode($token));
42+
}
43+
}

lib/Github/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ public function api($name)
134134
$api = new Api\User($this);
135135
break;
136136

137+
case 'authorization':
138+
case 'authorizations':
139+
$api = new Api\Authorizations($this);
140+
break;
141+
137142
default:
138143
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
139144
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class AuthorizationsTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldGetAllAuthorizations()
11+
{
12+
$expectedArray = array(array('id' => '123'));
13+
14+
$api = $this->getApiMock();
15+
$api->expects($this->once())
16+
->method('get')
17+
->with('authorizations')
18+
->will($this->returnValue($expectedArray));
19+
20+
$this->assertEquals($expectedArray, $api->all());
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function shouldShowAuthorization()
27+
{
28+
$id = 123;
29+
$expectedArray = array('id' => $id);
30+
31+
$api = $this->getApiMock();
32+
$api->expects($this->once())
33+
->method('get')
34+
->with('authorizations/'.$id)
35+
->will($this->returnValue($expectedArray));
36+
37+
$this->assertEquals($expectedArray, $api->show($id));
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function shouldCheckAuthorization()
44+
{
45+
$id = 123;
46+
$token = 'abc';
47+
$expectedArray = array('id' => $id);
48+
49+
$api = $this->getApiMock();
50+
$api->expects($this->once())
51+
->method('get')
52+
->with('authorizations/'.$id.'/tokens/'.$token)
53+
->will($this->returnValue($expectedArray));
54+
55+
$this->assertEquals($expectedArray, $api->check($id, $token));
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function shouldAuthorization()
62+
{
63+
$input = array(
64+
'note' => '',
65+
);
66+
67+
$api = $this->getApiMock();
68+
$api->expects($this->once())
69+
->method('post')
70+
->with('authorizations', $input);
71+
72+
$api->create($input);
73+
}
74+
75+
/**
76+
* @test
77+
*/
78+
public function shouldUpdateAuthorization()
79+
{
80+
$id = 123;
81+
$input = array(
82+
'note' => '',
83+
);
84+
85+
$api = $this->getApiMock();
86+
$api->expects($this->once())
87+
->method('patch')
88+
->with('authorizations/'.$id, $input);
89+
90+
$api->update($id, $input);
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function shouldDeleteAuthorization()
97+
{
98+
$id = 123;
99+
$api = $this->getApiMock();
100+
$api->expects($this->once())
101+
->method('delete')
102+
->with('authorizations/'.$id);
103+
104+
$api->remove($id);
105+
}
106+
107+
protected function getApiClass()
108+
{
109+
return 'Github\Api\Authorizations';
110+
}
111+
}

test/Github/Tests/ClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ public function getApiClassesProvider()
165165
array('pr', 'Github\Api\PullRequest'),
166166
array('pull_request', 'Github\Api\PullRequest'),
167167
array('pull_requests', 'Github\Api\PullRequest'),
168+
169+
array('authorization', 'Github\Api\Authorizations'),
170+
array('authorizations', 'Github\Api\Authorizations'),
168171
);
169172
}
170173

0 commit comments

Comments
 (0)