diff --git a/doc/activity.md b/doc/activity.md new file mode 100644 index 00000000000..ca8c74bf3a1 --- /dev/null +++ b/doc/activity.md @@ -0,0 +1,94 @@ +## Activity API (incomplete) +[Back to the navigation](index.md) + +Access to Starring and Watching a Repository for [non] authenticated users. +Wrap [GitHub Activity API](https://developer.github.com/v3/activity/). + +> *** No authentication required. *** + +### Get repos that a specific user has starred + +```php +$users = $client->api('user')->starred('ornicar'); +``` + +Returns an array of starred repos. + +### Get repos that a specific user is watching + +```php +$users = $client->api('user')->watched('ornicar'); +``` + +Returns an array of watched repos. + +> *** Requires [authentication](security.md). *** + +### Get repos that a authenticated user has starred + +```php +$activity = $client->api('current_user')->starring()->all(); +``` +Returns an array of starred repos. + +### Check if authenticated user has starred a specific repo + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->starring()->check($owner, $repo); +``` +Throws an Exception with code 404 in case that the repo is not starred by the authenticated user or NULL in case that it is starred by the authenticated user. + +### Star a specific repo for authenticated user + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->starring()->star($owner, $repo); +``` +Throws an Exception in case of failure or NULL in case of success. + +### Unstar a specific repo for authenticated user + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->starring()->unstar($owner, $repo); +``` +Throws an Exception in case of failure or NULL in case of success. + + +### Get repos that a authenticated user is watching + +```php +$activity = $client->api('current_user')->watchers()->all(); +``` +Returns an array of watched repos. + +### Check if authenticated user is watching a specific repo + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->watchers()->check($owner, $repo); +``` +Throws an Exception with code 404 in case that the repo is not beeing watched by the authenticated user or NULL in case that it is beeing watched by the authenticated user. + +### Watch a specific repo for authenticated user + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->watchers()->watch($owner, $repo); +``` +Throws an Exception in case of failure or NULL in case of success. + +### Stop watching a specific repo for authenticated user + +```php +$owner = "KnpLabs"; +$repo = "php-github-api"; +$activity = $client->api('current_user')->watchers()->unwatch($owner, $repo); +``` +Throws an Exception in case of failure or NULL in case of success. \ No newline at end of file diff --git a/doc/index.md b/doc/index.md index e278e2c931b..176e5a2b51d 100644 --- a/doc/index.md +++ b/doc/index.md @@ -20,6 +20,7 @@ APIs: * [Assets](repo/assets.md) * [Users](users.md) * [Meta](meta.md) +* [Activity](activity.md) Additional features: diff --git a/doc/users.md b/doc/users.md index b8a8e09949e..810226af55b 100644 --- a/doc/users.md +++ b/doc/users.md @@ -108,12 +108,24 @@ $client->api('current_user')->follow()->unfollow('symfony'); Returns an array of followed users. ### Get repos that a specific user is watching +> See [more](activity.md). ```php $users = $client->api('user')->watched('ornicar'); ``` +For authenticated user use. + +> Requires [authentication](security.md). + +```php +$users = $client->api('current_user')->watchers()->all(); +``` + +Returns an array of watched repos. + ### Get repos that a specific user has starred +> See [more](activity.md). ```php $users = $client->api('user')->starred('ornicar'); @@ -124,10 +136,10 @@ For authenticated user use. > Requires [authentication](security.md). ```php -$users = $client->api('current_user')->watched(); +$users = $client->api('current_user')->starring()->all(); ``` -Returns an array of watched repos. +Returns an array of starred repos. ### Get the authenticated user emails diff --git a/lib/Github/Api/CurrentUser.php b/lib/Github/Api/CurrentUser.php index 38318959589..6f920668ad7 100644 --- a/lib/Github/Api/CurrentUser.php +++ b/lib/Github/Api/CurrentUser.php @@ -7,10 +7,12 @@ use Github\Api\CurrentUser\Followers; use Github\Api\CurrentUser\Notifications; use Github\Api\CurrentUser\Watchers; +use Github\Api\CurrentUser\Starring; /** * @link http://developer.github.com/v3/users/ * @author Joseph Bielawski + * @revised Felipe Valtl de Mello */ class CurrentUser extends AbstractApi { @@ -111,16 +113,27 @@ public function watchers() { return new Watchers($this->client); } - + + /** + * @Deprecated Use watchers() instead + */ public function watched($page = 1) { return $this->get('user/watched', array( 'page' => $page )); } + + /** + * @return Starring + */ + public function starring() + { + return new Starring($this->client); + } - /** - * @link http://developer.github.com/changes/2012-9-5-watcher-api/ + /** + * @Deprecated Use starring() instead */ public function starred($page = 1) { diff --git a/lib/Github/Api/CurrentUser/Starring.php b/lib/Github/Api/CurrentUser/Starring.php new file mode 100644 index 00000000000..97e0b017c9b --- /dev/null +++ b/lib/Github/Api/CurrentUser/Starring.php @@ -0,0 +1,65 @@ + + */ +class Starring extends AbstractApi +{ + /** + * List repositories starred by the authenticated user + * @link https://developer.github.com/v3/activity/starring/ + * + * @param integer $page + * @return array + */ + public function all($page = 1) + { + return $this->get('user/starred', array( + 'page' => $page + )); + } + + /** + * Check that the authenticated user starres a repository + * @link https://developer.github.com/v3/activity/starring/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function check($username, $repository) + { + return $this->get('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository)); + } + + /** + * Make the authenticated user star a repository + * @link https://developer.github.com/v3/activity/starring/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function star($username, $repository) + { + return $this->put('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository)); + } + + /** + * Make the authenticated user unstar a repository + * @link https://developer.github.com/v3/activity/starring + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function unstar($username, $repository) + { + return $this->delete('user/starred/'.rawurlencode($username).'/'.rawurlencode($repository)); + } +} diff --git a/lib/Github/Api/CurrentUser/Watchers.php b/lib/Github/Api/CurrentUser/Watchers.php index 398ac58536a..035b78f568e 100644 --- a/lib/Github/Api/CurrentUser/Watchers.php +++ b/lib/Github/Api/CurrentUser/Watchers.php @@ -5,28 +5,29 @@ use Github\Api\AbstractApi; /** - * @link http://developer.github.com/v3/repos/watching/ + * @link https://developer.github.com/v3/activity/watching/ * @author Joseph Bielawski + * @revised Felipe Valtl de Mello */ class Watchers extends AbstractApi { /** * List repositories watched by the authenticated user - * @link http://developer.github.com/v3/repos/watching/ + * @link https://developer.github.com/v3/activity/watching/ * * @param integer $page * @return array */ public function all($page = 1) { - return $this->get('user/watched', array( + return $this->get('user/subscriptions', array( 'page' => $page )); } /** * Check that the authenticated user watches a repository - * @link http://developer.github.com/v3/repos/watching/ + * @link https://developer.github.com/v3/activity/watching/ * * @param string $username the user who owns the repo * @param string $repository the name of the repo @@ -34,12 +35,12 @@ public function all($page = 1) */ public function check($username, $repository) { - return $this->get('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository)); + return $this->get('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository)); } /** * Make the authenticated user watch a repository - * @link http://developer.github.com/v3/repos/watching/ + * @link https://developer.github.com/v3/activity/watching/ * * @param string $username the user who owns the repo * @param string $repository the name of the repo @@ -47,12 +48,12 @@ public function check($username, $repository) */ public function watch($username, $repository) { - return $this->put('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository)); + return $this->put('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository)); } /** * Make the authenticated user unwatch a repository - * @link http://developer.github.com/v3/repos/watching/ + * @link https://developer.github.com/v3/activity/watching/ * * @param string $username the user who owns the repo * @param string $repository the name of the repo @@ -60,6 +61,6 @@ public function watch($username, $repository) */ public function unwatch($username, $repository) { - return $this->delete('user/watched/'.rawurlencode($username).'/'.rawurlencode($repository)); + return $this->delete('user/subscriptions/'.rawurlencode($username).'/'.rawurlencode($repository)); } } diff --git a/test/Github/Tests/Api/CurrentUser/StarringTest.php b/test/Github/Tests/Api/CurrentUser/StarringTest.php new file mode 100644 index 00000000000..c2cba90cfb1 --- /dev/null +++ b/test/Github/Tests/Api/CurrentUser/StarringTest.php @@ -0,0 +1,74 @@ + 'l3l0/test'), + array('name' => 'cordoval/test') + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('user/starred') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->all()); + } + + /** + * @test + */ + public function shouldCheckStar() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('user/starred/l3l0/test') + ->will($this->returnValue(null)); + + $this->assertNull($api->check('l3l0', 'test')); + } + + /** + * @test + */ + public function shouldStarUser() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('user/starred/l3l0/test') + ->will($this->returnValue(null)); + + $this->assertNull($api->star('l3l0', 'test')); + } + + /** + * @test + */ + public function shouldUnstarUser() + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('user/starred/l3l0/test') + ->will($this->returnValue(null)); + + $this->assertNull($api->unstar('l3l0', 'test')); + } + + protected function getApiClass() + { + return 'Github\Api\CurrentUser\Starring'; + } +} diff --git a/test/Github/Tests/Api/CurrentUser/WatchersTest.php b/test/Github/Tests/Api/CurrentUser/WatchersTest.php index 15e009f6a08..e2234ed7efb 100644 --- a/test/Github/Tests/Api/CurrentUser/WatchersTest.php +++ b/test/Github/Tests/Api/CurrentUser/WatchersTest.php @@ -19,7 +19,7 @@ public function shouldGetWatchers() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('user/watched') + ->with('user/subscriptions') ->will($this->returnValue($expectedValue)); $this->assertEquals($expectedValue, $api->all()); @@ -33,7 +33,7 @@ public function shouldCheckWatcher() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('user/watched/l3l0/test') + ->with('user/subscriptions/l3l0/test') ->will($this->returnValue(null)); $this->assertNull($api->check('l3l0', 'test')); @@ -47,7 +47,7 @@ public function shouldWatchUser() $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('user/watched/l3l0/test') + ->with('user/subscriptions/l3l0/test') ->will($this->returnValue(null)); $this->assertNull($api->watch('l3l0', 'test')); @@ -61,7 +61,7 @@ public function shouldUnwatchUser() $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with('user/watched/l3l0/test') + ->with('user/subscriptions/l3l0/test') ->will($this->returnValue(null)); $this->assertNull($api->unwatch('l3l0', 'test')); diff --git a/test/Github/Tests/Api/CurrentUserTest.php b/test/Github/Tests/Api/CurrentUserTest.php index 36316f3c02e..902b53957b8 100644 --- a/test/Github/Tests/Api/CurrentUserTest.php +++ b/test/Github/Tests/Api/CurrentUserTest.php @@ -133,6 +133,16 @@ public function shouldGetWatchersApiObject() $this->assertInstanceOf('Github\Api\CurrentUser\Watchers', $api->watchers()); } + + /** + * @test + */ + public function shouldGetStarredApiObject() + { + $api = $this->getApiMock(); + + $this->assertInstanceOf('Github\Api\CurrentUser\Starring', $api->starring()); + } protected function getApiClass() {