Skip to content

Commit

Permalink
Add message format types
Browse files Browse the repository at this point in the history
  • Loading branch information
casperboone committed Sep 13, 2020
1 parent 30723cb commit 97052f5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function routeNotificationForPushover() {
Please note that only the message content is mandatory, all other methods are optional. The message content can be set via `content('')`, via the create method `PushoverMessage::create('')` or via the constructor `new PushoverMessage('')`.

- `content($message)`: Accepts a string value for the message text.
- `html()`: Sets the message type to [HTML](https://pushover.net/api#html).
- `monospace()`: Sets the message type to monospace.
- `plain()`: Sets the message type to plain text, this is the default.
- `title($title)`: Accepts a string value for the message title.
- `time($timestamp)`: Accepts either a `Carbon` object or a UNIX timestamp.
- `url($url[, $title])`: Accepts a string value for a [supplementary url](https://pushover.net/api#urls) and an optional string value for the title of the url.
Expand Down
54 changes: 54 additions & 0 deletions src/PushoverMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ class PushoverMessage
*/
public $content;

/**
* The format of the message.
*
* Either "plain", "html" or "monospace".
*
* @var string
*/
public $format = self::FORMAT_PLAIN;

/**
* The (optional) title of the message.
*
Expand Down Expand Up @@ -72,6 +81,13 @@ class PushoverMessage
*/
public $sound;

/**
* Message formats.
*/
const FORMAT_PLAIN = 0;
const FORMAT_HTML = 1;
const FORMAT_MONOSPACE = 2;

/**
* Message priorities.
*/
Expand Down Expand Up @@ -112,6 +128,42 @@ public function content($content)
return $this;
}

/**
* Set the formatting type to plain text.
*
* @return $this
*/
public function plain()
{
$this->format = static::FORMAT_PLAIN;

return $this;
}

/**
* Set the formatting type to HTML.
*
* @return $this
*/
public function html()
{
$this->format = static::FORMAT_HTML;

return $this;
}

/**
* Set the formatting type to monospace.
*
* @return $this
*/
public function monospace()
{
$this->format = self::FORMAT_MONOSPACE;

return $this;
}

/**
* Set the title of the Pushover message.
*
Expand Down Expand Up @@ -260,6 +312,8 @@ public function toArray()
'sound' => $this->sound,
'retry' => $this->retry,
'expire' => $this->expire,
'html' => $this->format === static::FORMAT_HTML,
'monospace' => $this->format === static::FORMAT_MONOSPACE,
];
}

Expand Down
9 changes: 7 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token()
'sound' => 'boing',
'url' => 'http://example.com',
'url_title' => 'Example Website',
'html' => false,
'monospace' => false,
]);

$pushover = new Pushover($this->guzzleClient, 'global-application-token');
Expand All @@ -70,7 +72,8 @@ public function it_can_send_a_pushover_notification_with_a_global_token()
/** @test */
public function it_can_send_a_pushover_notification_with_an_overridden_token()
{
$message = (new PushoverMessage('Message text'))
$message = (new PushoverMessage('Message <b>text</b>'))
->html()
->title('Message title')
->emergencyPriority(60, 600)
->time(123456789)
Expand All @@ -81,7 +84,7 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token()
'token' => 'overridden-application-token',
'user' => 'pushover-key',
'device' => 'iphone,desktop',
'message' => 'Message text',
'message' => 'Message <b>text</b>',
'title' => 'Message title',
'priority' => 2,
'retry' => 60,
Expand All @@ -90,6 +93,8 @@ public function it_can_send_a_pushover_notification_with_an_overridden_token()
'sound' => 'boing',
'url' => 'http://example.com',
'url_title' => 'Example Website',
'html' => true,
'monospace' => false,
]);

$pushover = new Pushover($this->guzzleClient, 'global-application-token');
Expand Down
24 changes: 24 additions & 0 deletions tests/PushoverMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ public function it_can_set_content()
$this->assertEquals('message text', $this->message->content);
}

/** @test */
public function it_can_set_the_message_format_to_plain()
{
$this->message->plain();

$this->assertEquals(PushoverMessage::FORMAT_PLAIN, $this->message->format);
}

/** @test */
public function it_can_set_the_message_format_to_html()
{
$this->message->html();

$this->assertEquals(PushoverMessage::FORMAT_HTML, $this->message->format);
}

/** @test */
public function it_can_set_the_message_format_to_monospace()
{
$this->message->monospace();

$this->assertEquals(PushoverMessage::FORMAT_MONOSPACE, $this->message->format);
}

/** @test */
public function it_can_set_a_title()
{
Expand Down

0 comments on commit 97052f5

Please sign in to comment.