From 1c28e4e113be68b8d7d54590620072d42ea7b157 Mon Sep 17 00:00:00 2001 From: Henk Koop Date: Wed, 31 Jan 2018 15:18:54 +0100 Subject: [PATCH] Add support for dynamic application tokens * allow a notifiable model to overrule the globally set token * Reflect app token design change in README * Change toArray of Pushover receiver to be a single statement --- README.md | 10 +++++ src/Pushover.php | 6 +-- src/PushoverReceiver.php | 18 ++++++++- tests/IntegrationTest.php | 40 +++++++++++++++++-- ...otifiableWithPushoverReceiverWithToken.php | 16 ++++++++ tests/PushoverReceiverTest.php | 18 +++++++++ tests/PushoverTest.php | 13 ++++++ 7 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 tests/NotifiableWithPushoverReceiverWithToken.php diff --git a/README.md b/README.md index 812bdf8..7cf6c70 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,16 @@ public function routeNotificationForPushover() { ->toDevice(['iphone', 'desktop']); } ``` + +If you want to (dynamically) overrule the application token from the services config, e.g. because each user holds their own application token, return a `PushoverReceiver` object like this: +```php +... +public function routeNotificationForPushover() { + return PushoverReceiver::withUserKey('pushover-key') + ->withApplicationToken('app-token'); +} +``` + You can also send a message to a Pushover group: ```php ... diff --git a/src/Pushover.php b/src/Pushover.php index d7dd115..04e3439 100644 --- a/src/Pushover.php +++ b/src/Pushover.php @@ -68,15 +68,15 @@ public function send($params) } /** - * Merge token into parameters array. + * Merge token into parameters array, unless it has been set on the PushoverReceiver. * * @param array $params * @return array */ protected function paramsWithToken($params) { - return array_merge($params, [ + return array_merge([ 'token' => $this->token, - ]); + ], $params); } } diff --git a/src/PushoverReceiver.php b/src/PushoverReceiver.php index 54c68a1..8daee71 100644 --- a/src/PushoverReceiver.php +++ b/src/PushoverReceiver.php @@ -5,6 +5,7 @@ class PushoverReceiver { protected $key; + protected $token; protected $devices = []; /** @@ -58,6 +59,19 @@ public function toDevice($device) return $this; } + /** + * Set the application token. + * + * @param $token + * @return PushoverReceiver + */ + public function withApplicationToken($token) + { + $this->token = $token; + + return $this; + } + /** * Get array representation of Pushover receiver. * @@ -65,9 +79,9 @@ public function toDevice($device) */ public function toArray() { - return [ + return array_merge([ 'user' => $this->key, 'device' => implode(',', $this->devices), - ]; + ], $this->token ? ['token' => $this->token] : []); } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 1516e2c..e919cb2 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -34,7 +34,7 @@ public function setUp() } /** @test */ - public function it_can_send_a_pushover_notification() + public function it_can_send_a_pushover_notification_with_a_global_token() { $message = (new PushoverMessage('Message text')) ->title('Message title') @@ -44,7 +44,7 @@ public function it_can_send_a_pushover_notification() ->url('http://example.com', 'Example Website'); $this->requestWillBeSentToPushoverWith([ - 'token' => 'application-token', + 'token' => 'global-application-token', 'user' => 'pushover-key', 'device' => 'iphone,desktop', 'message' => 'Message text', @@ -58,7 +58,7 @@ public function it_can_send_a_pushover_notification() 'url_title' => 'Example Website', ]); - $pushover = new Pushover($this->guzzleClient, 'application-token'); + $pushover = new Pushover($this->guzzleClient, 'global-application-token'); $channel = new PushoverChannel($pushover, $this->events); @@ -67,6 +67,40 @@ public function it_can_send_a_pushover_notification() $channel->send(new NotifiableWithPushoverReceiver, $this->notification); } + /** @test */ + public function it_can_send_a_pushover_notification_with_an_overridden_token() + { + $message = (new PushoverMessage('Message text')) + ->title('Message title') + ->emergencyPriority(60, 600) + ->time(123456789) + ->sound('boing') + ->url('http://example.com', 'Example Website'); + + $this->requestWillBeSentToPushoverWith([ + 'token' => 'overridden-application-token', + 'user' => 'pushover-key', + 'device' => 'iphone,desktop', + 'message' => 'Message text', + 'title' => 'Message title', + 'priority' => 2, + 'retry' => 60, + 'expire' => 600, + 'timestamp' => 123456789, + 'sound' => 'boing', + 'url' => 'http://example.com', + 'url_title' => 'Example Website', + ]); + + $pushover = new Pushover($this->guzzleClient, 'global-application-token'); + + $channel = new PushoverChannel($pushover, $this->events); + + $this->notification->shouldReceive('toPushover')->andReturn($message); + + $channel->send(new NotifiableWithPushoverReceiverWithToken(), $this->notification); + } + protected function requestWillBeSentToPushoverWith($params) { $this->guzzleClient->shouldReceive('post') diff --git a/tests/NotifiableWithPushoverReceiverWithToken.php b/tests/NotifiableWithPushoverReceiverWithToken.php new file mode 100644 index 0000000..bcff167 --- /dev/null +++ b/tests/NotifiableWithPushoverReceiverWithToken.php @@ -0,0 +1,16 @@ +withApplicationToken('overridden-application-token') + ->toDevice('iphone') + ->toDevice('desktop'); + } +} diff --git a/tests/PushoverReceiverTest.php b/tests/PushoverReceiverTest.php index a80097b..ffd4de5 100644 --- a/tests/PushoverReceiverTest.php +++ b/tests/PushoverReceiverTest.php @@ -30,6 +30,24 @@ public function it_can_set_up_a_receiver_with_a_group_key() $this->assertArraySubset(['user' => 'pushover-key'], $pushoverReceiver->toArray()); } + /** @test */ + public function it_can_set_up_a_receiver_with_an_application_token() + { + $pushoverReceiver = PushoverReceiver::withUserKey('pushover-key')->withApplicationToken('pushover-token'); + + $this->assertArraySubset(['user' => 'pushover-key', 'token' => 'pushover-token'], $pushoverReceiver->toArray()); + } + + /** @test */ + public function it_only_exposes_app_token_when_set() + { + $pushoverReceiverUserKey = PushoverReceiver::withUserKey('pushover-key'); + $pushoverReceiverGroupKey = PushoverReceiver::withGroupKey('pushover-key'); + + $this->assertArrayNotHasKey('token', $pushoverReceiverUserKey->toArray()); + $this->assertArrayNotHasKey('token', $pushoverReceiverGroupKey->toArray()); + } + /** @test */ public function it_can_add_a_single_device_to_the_receiver() { diff --git a/tests/PushoverTest.php b/tests/PushoverTest.php index 3e74566..5ed701e 100644 --- a/tests/PushoverTest.php +++ b/tests/PushoverTest.php @@ -40,6 +40,19 @@ public function it_can_send_a_request_to_pushover() $this->pushover->send([]); } + /** @test */ + public function it_can_send_a_request_with_an_overridden_token() + { + $this->guzzleClient->shouldReceive('post') + ->with('https://api.pushover.net/1/messages.json', [ + 'form_params' => [ + 'token' => 'dynamic-application-token', + ], + ]); + + $this->pushover->send(['token' => 'dynamic-application-token']); + } + /** @test */ public function it_can_accept_parameters_for_a_send_request() {