Skip to content

Commit

Permalink
fix flush reset queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink committed Dec 4, 2015
1 parent 11c70ef commit c25ea27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function __construct(array $apiKeys = array(), $TTL = null, $timeout = nu
* @param string|null $userPublicKey
* @param bool $flush If you want to flush directly (usually when you send only one notification)
*
* @return bool|array Return an array of information if $flush is set to true and the request has failed. Else return true.
* @return bool|array Return an array of information if $flush is set to true and the request has failed.
* Else return true.
* @throws \ErrorException
*/
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $flush = false)
Expand All @@ -81,12 +82,17 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
* Flush notifications. Triggers the requests.
*
* @return array|bool If there are no errors, return true.
* Else return an array of information for each notification sent (success, statusCode, headers).
* If there were no notifications in the queue, return false.
* Else return an array of information for each notification sent (success, statusCode, headers).
*
* @throws \ErrorException
*/
public function flush()
{
if (empty($this->notificationsByServerType)) {
return false;
}

// if GCM is present, we should check for the API key
if (array_key_exists('GCM', $this->notificationsByServerType)) {
if (empty($this->apiKeys['GCM'])) {
Expand Down Expand Up @@ -139,6 +145,9 @@ public function flush()
}
}

// reset queue
$this->notificationsByServerType = null;

return $completeSuccess ? true : $return;
}

Expand Down
16 changes: 14 additions & 2 deletions tests/WebPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testSendNotification()
{
$res = $this->webPush->sendNotification($this->endpoints['standard'], null, null, true);

$this->assertTrue($res);
$this->assertEquals($res, true);
}

public function testSendNotifications()
Expand All @@ -49,7 +49,19 @@ public function testSendNotifications()

$res = $this->webPush->flush();

$this->assertTrue($res);
$this->assertEquals(true, $res);
}

public function testFlush()
{
$this->webPush->sendNotification($this->endpoints['standard']);
$this->assertEquals(true, $this->webPush->flush());

// queue has been reset
$this->assertEquals(false, $this->webPush->flush());

$this->webPush->sendNotification($this->endpoints['standard']);
$this->assertEquals(true, $this->webPush->flush());
}

public function testSendGCMNotificationWithoutGCMApiKey()
Expand Down

0 comments on commit c25ea27

Please sign in to comment.