Skip to content

Commit 9375b47

Browse files
committed
Merge pull request #283 from lucasmichot/feature/user-membership
Add user memberships
2 parents 84ac636 + 1367eb4 commit 9375b47

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

doc/currentuser/memberships.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Current user / Memberships API
2+
[Back to the navigation](../README.md)
3+
4+
Wraps [GitHub Issue Comments API](https://developer.github.com/v3/orgs/members/#get-your-organization-membership).
5+
6+
### List your memberships
7+
8+
> Requires [authentication](../security.md).
9+
10+
```php
11+
$memberships = $client->user()->memberships()->all();
12+
```
13+
14+
Returns an array of your memberships in all organizations you are part of.
15+
16+
### Show an organization membership
17+
18+
> Requires [authentication](../security.md).
19+
20+
```php
21+
$membership = $client->user()->memberships()->organization('KnpLabs');
22+
```
23+
* `KnpLabs` : the organization
24+
25+
Returns an array of one membership in a specific organization.
26+
27+
### Update an organization membership
28+
29+
> Requires [authentication](../security.md).
30+
31+
```php
32+
$membership = $client->user()->memberships()->edit('KnpLabs');
33+
```
34+
* `KnpLabs` : the organization
35+
36+
Update your membership to an organization. The only possible action is to activate your membership.

lib/Github/Api/CurrentUser.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Github\Api\CurrentUser\DeployKeys;
66
use Github\Api\CurrentUser\Emails;
77
use Github\Api\CurrentUser\Followers;
8+
use Github\Api\CurrentUser\Memberships;
89
use Github\Api\CurrentUser\Notifications;
910
use Github\Api\CurrentUser\Watchers;
1011
use Github\Api\CurrentUser\Starring;
@@ -78,6 +79,14 @@ public function notifications()
7879
return new Notifications($this->client);
7980
}
8081

82+
/**
83+
* @return Memberships
84+
*/
85+
public function memberships()
86+
{
87+
return new Memberships($this->client);
88+
}
89+
8190
/**
8291
* @link http://developer.github.com/v3/orgs/#list-user-organizations
8392
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Github\Api\CurrentUser;
4+
5+
use Github\Api\AbstractApi;
6+
7+
class Memberships extends AbstractApi
8+
{
9+
/**
10+
* List your organization memberships.
11+
*
12+
* @link https://developer.github.com/v3/orgs/members/#get-your-organization-membership
13+
*
14+
* @return array
15+
*/
16+
public function all()
17+
{
18+
return $this->get('user/memberships/orgs');
19+
}
20+
21+
/**
22+
* Get your organization membership.
23+
*
24+
* @link https://developer.github.com/v3/orgs/members/#get-your-organization-membership
25+
*
26+
* @return array
27+
*/
28+
public function organization($organization)
29+
{
30+
return $this->get('user/memberships/orgs/'.rawurlencode($organization));
31+
}
32+
33+
/**
34+
* Edit your organization membership
35+
*
36+
* @link https://developer.github.com/v3/orgs/members/#edit-your-organization-membership
37+
*
38+
* @return array
39+
*/
40+
public function edit($organization)
41+
{
42+
return $this->patch('user/memberships/orgs/'.rawurlencode($organization), array('state' => 'active'));
43+
}
44+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class MembershipsTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldGetMemberships()
11+
{
12+
$expectedValue = array(
13+
array(
14+
'organization' => array(
15+
'login' => 'octocat',
16+
'id' => 1,
17+
),
18+
'user' => array(
19+
'login' => 'defunkt',
20+
'id' => 3,
21+
),
22+
),
23+
array(
24+
'organization' => array(
25+
'login' => 'invitocat',
26+
'id' => 2,
27+
),
28+
'user' => array(
29+
'login' => 'defunkt',
30+
'id' => 3,
31+
),
32+
),
33+
);
34+
35+
$api = $this->getApiMock();
36+
$api->expects($this->once())
37+
->method('get')
38+
->with('user/memberships/orgs')
39+
->will($this->returnValue($expectedValue));
40+
41+
$this->assertEquals($expectedValue, $api->all());
42+
}
43+
44+
/**
45+
* @test
46+
*/
47+
public function shouldGetMembershipsForOrganization()
48+
{
49+
$expectedValue = array(
50+
'organization' => array(
51+
'login' => 'invitocat',
52+
'id' => 2,
53+
),
54+
'user' => array(
55+
'login' => 'defunkt',
56+
'id' => 3,
57+
),
58+
);
59+
60+
$api = $this->getApiMock();
61+
$api->expects($this->once())
62+
->method('get')
63+
->with('user/memberships/orgs/invitocat')
64+
->will($this->returnValue($expectedValue));
65+
66+
$this->assertEquals($expectedValue, $api->organization('invitocat'));
67+
}
68+
69+
/**
70+
* @test
71+
*/
72+
public function shouldEditMembershipsForOrganization()
73+
{
74+
$expectedValue = array(
75+
'state' => 'active',
76+
);
77+
78+
$api = $this->getApiMock();
79+
$api->expects($this->once())
80+
->method('patch')
81+
->with('user/memberships/orgs/invitocat')
82+
->will($this->returnValue($expectedValue));
83+
84+
$this->assertEquals($expectedValue, $api->edit('invitocat'));
85+
}
86+
87+
protected function getApiClass()
88+
{
89+
return 'Github\Api\CurrentUser\Memberships';
90+
}
91+
}

0 commit comments

Comments
 (0)