diff --git a/doc/currentuser/memberships.md b/doc/currentuser/memberships.md new file mode 100644 index 00000000000..989766bc254 --- /dev/null +++ b/doc/currentuser/memberships.md @@ -0,0 +1,36 @@ +## Current user / Memberships API +[Back to the navigation](../README.md) + +Wraps [GitHub Issue Comments API](https://developer.github.com/v3/orgs/members/#get-your-organization-membership). + +### List your memberships + +> Requires [authentication](../security.md). + +```php +$memberships = $client->user()->memberships()->all(); +``` + +Returns an array of your memberships in all organizations you are part of. + +### Show an organization membership + +> Requires [authentication](../security.md). + +```php +$membership = $client->user()->memberships()->organization('KnpLabs'); +``` +* `KnpLabs` : the organization + +Returns an array of one membership in a specific organization. + +### Update an organization membership + +> Requires [authentication](../security.md). + +```php +$membership = $client->user()->memberships()->edit('KnpLabs'); +``` +* `KnpLabs` : the organization + +Update your membership to an organization. The only possible action is to activate your membership. diff --git a/lib/Github/Api/CurrentUser.php b/lib/Github/Api/CurrentUser.php index f9e9cd79f6c..b525ee185a9 100644 --- a/lib/Github/Api/CurrentUser.php +++ b/lib/Github/Api/CurrentUser.php @@ -5,6 +5,7 @@ use Github\Api\CurrentUser\DeployKeys; use Github\Api\CurrentUser\Emails; use Github\Api\CurrentUser\Followers; +use Github\Api\CurrentUser\Memberships; use Github\Api\CurrentUser\Notifications; use Github\Api\CurrentUser\Watchers; use Github\Api\CurrentUser\Starring; @@ -78,6 +79,14 @@ public function notifications() return new Notifications($this->client); } + /** + * @return Memberships + */ + public function memberships() + { + return new Memberships($this->client); + } + /** * @link http://developer.github.com/v3/orgs/#list-user-organizations * diff --git a/lib/Github/Api/CurrentUser/Memberships.php b/lib/Github/Api/CurrentUser/Memberships.php new file mode 100644 index 00000000000..c02c0122ef6 --- /dev/null +++ b/lib/Github/Api/CurrentUser/Memberships.php @@ -0,0 +1,44 @@ +get('user/memberships/orgs'); + } + + /** + * Get your organization membership. + * + * @link https://developer.github.com/v3/orgs/members/#get-your-organization-membership + * + * @return array + */ + public function organization($organization) + { + return $this->get('user/memberships/orgs/'.rawurlencode($organization)); + } + + /** + * Edit your organization membership + * + * @link https://developer.github.com/v3/orgs/members/#edit-your-organization-membership + * + * @return array + */ + public function edit($organization) + { + return $this->patch('user/memberships/orgs/'.rawurlencode($organization), array('state' => 'active')); + } +} diff --git a/test/Github/Tests/Api/CurrentUser/MembershipsTest.php b/test/Github/Tests/Api/CurrentUser/MembershipsTest.php new file mode 100644 index 00000000000..571f1bce41d --- /dev/null +++ b/test/Github/Tests/Api/CurrentUser/MembershipsTest.php @@ -0,0 +1,91 @@ + array( + 'login' => 'octocat', + 'id' => 1, + ), + 'user' => array( + 'login' => 'defunkt', + 'id' => 3, + ), + ), + array( + 'organization' => array( + 'login' => 'invitocat', + 'id' => 2, + ), + 'user' => array( + 'login' => 'defunkt', + 'id' => 3, + ), + ), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('user/memberships/orgs') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->all()); + } + + /** + * @test + */ + public function shouldGetMembershipsForOrganization() + { + $expectedValue = array( + 'organization' => array( + 'login' => 'invitocat', + 'id' => 2, + ), + 'user' => array( + 'login' => 'defunkt', + 'id' => 3, + ), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('user/memberships/orgs/invitocat') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->organization('invitocat')); + } + + /** + * @test + */ + public function shouldEditMembershipsForOrganization() + { + $expectedValue = array( + 'state' => 'active', + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('patch') + ->with('user/memberships/orgs/invitocat') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->edit('invitocat')); + } + + protected function getApiClass() + { + return 'Github\Api\CurrentUser\Memberships'; + } +}