-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from dennisdegreef/addNotificationsApi
Implemented notifications from github API
- Loading branch information
Showing
4 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## Notification API | ||
[Back to the navigation](index.md) | ||
|
||
Listing notifications and marking them as read. | ||
Wraps [GitHub Notification API](https://developer.github.com/v3/activity/notifications/). | ||
|
||
### List notifications | ||
|
||
```php | ||
$issues = $client->api('notification')->all(); | ||
``` | ||
|
||
Returns an array of unread notifications. | ||
|
||
### Include already read notifications, including participating, or since a certain date | ||
|
||
```php | ||
$includingRead = true; | ||
$participating = true; | ||
$since = new DateTime('1970/01/01'); | ||
$issues = $client->api('notification')->all($includingRead, $participating, $since); | ||
``` | ||
|
||
Returns an array of all notifications | ||
|
||
### Mark notifications as read | ||
|
||
```php | ||
$client->api('notification')->markRead(); | ||
``` | ||
|
||
or up until a certain date | ||
|
||
```php | ||
$client->api('notification')->markRead(new DateTime('2015/01/01')); | ||
``` | ||
|
||
Marks all notifications as read up until the current date, unless a date is given |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Github\Api; | ||
|
||
use DateTime; | ||
|
||
/** | ||
* API for accessing Notifications from your Git/Github repositories. | ||
* | ||
* Important! You have to be authenticated to perform these methods | ||
* | ||
* @link https://developer.github.com/v3/activity/notifications/ | ||
* @author Dennis de Greef <[email protected]> | ||
*/ | ||
class Notification extends AbstractApi | ||
{ | ||
/** | ||
* Get a listing of notifications | ||
* | ||
* @link https://developer.github.com/v3/activity/notifications/ | ||
* | ||
* @param bool $includingRead | ||
* @param bool $participating | ||
* @param DateTime|null $since | ||
* | ||
* @return array array of notifications | ||
*/ | ||
public function all($includingRead = false, $participating = false, DateTime $since = null) | ||
{ | ||
$parameters = array( | ||
'all' => $includingRead, | ||
'participating' => $participating | ||
); | ||
|
||
if($since !== null) { | ||
$parameters['since'] = $since->format(DateTime::ISO8601); | ||
} | ||
|
||
return $this->get('notifications', $parameters); | ||
} | ||
|
||
/** | ||
* Marks all notifications as read from the current date | ||
* Optionally give DateTime to mark as read before that date | ||
* | ||
* @link https://developer.github.com/v3/activity/notifications/#mark-as-read | ||
* | ||
* @param DateTime|null $since | ||
*/ | ||
public function markRead(DateTime $since = null) | ||
{ | ||
$parameters = array(); | ||
|
||
if($since !== null) { | ||
$parameters['last_read_at'] = $since->format(DateTime::ISO8601); | ||
} | ||
|
||
$this->put('notifications', $parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Github\Tests\Api; | ||
|
||
use DateTime; | ||
|
||
class NotificationTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldGetNotifications() | ||
{ | ||
$parameters = array( | ||
'all' => false, | ||
'participating' => false, | ||
); | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('notifications', $parameters); | ||
|
||
$api->all(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldGetNotificationsSince() | ||
{ | ||
$since = new DateTime('now'); | ||
|
||
$parameters = array( | ||
'all' => false, | ||
'participating' => false, | ||
'since' => $since->format(DateTime::ISO8601), | ||
); | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('notifications', $parameters); | ||
|
||
$api->all(false, false, $since); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldGetNotificationsIncludingAndParticipating() | ||
{ | ||
$parameters = array( | ||
'all' => true, | ||
'participating' => true, | ||
); | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('notifications', $parameters); | ||
|
||
$api->all(true, true); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldMarkNotificationsAsRead() | ||
{ | ||
$parameters = array(); | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('put') | ||
->with('notifications', $parameters); | ||
|
||
$api->markRead(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldMarkNotificationsAsReadForGivenDate() | ||
{ | ||
$since = new DateTime('now'); | ||
|
||
$parameters = array( | ||
'last_read_at' => $since->format(DateTime::ISO8601), | ||
); | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('put') | ||
->with('notifications', $parameters); | ||
|
||
$api->markRead($since); | ||
} | ||
|
||
protected function getApiClass() | ||
{ | ||
return 'Github\Api\Notification'; | ||
} | ||
} |