Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Aug 13, 2016
1 parent baf078d commit 9b369db
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"require-dev": {
"mockery/mockery": "^0.9.5",
"phpunit/phpunit": "4.*"
"orchestra/testbench": "^3.3@dev"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions src/OneSignalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function toArray()
'url' => $this->url,
'buttons' => $this->buttons,
'web_buttons' => $this->webButtons,
'chrome_web_icon' => $this->icon,
'chrome_icon' => $this->icon,
'adm_small_icon' => $this->icon,
'small_icon' => $this->icon,
];

foreach ($this->data as $data => $value) {
Expand Down
83 changes: 83 additions & 0 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace NotificationChannels\OneSignal\Test;

use GuzzleHttp\Psr7\Response;
use Mockery;
use Berkayk\OneSignal\OneSignalClient;
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;
use NotificationChannels\OneSignal\OneSignalChannel;
use Orchestra\Testbench\TestCase;

class ChannelTest extends TestCase
{
/** @var Mockery\Mock */
protected $oneSignal;

/** @var \NotificationChannels\OneSignal\OneSignalChannel */
protected $channel;

public function setUp()
{
parent::setUp();
$this->oneSignal = Mockery::mock(OneSignalClient::class);

$this->channel = new OneSignalChannel($this->oneSignal);
}

public function tearDown()
{
Mockery::close();
parent::tearDown();
}

/** @test */
public function it_can_send_a_notification()
{
$response = new Response(200);

$this->oneSignal->shouldReceive('sendNotificationCustom')
->once()
->with([
'contents' => ['en' => 'Body'],
'headings' => ['en' => 'Subject'],
'url' => 'URL',
'buttons' => [],
'web_buttons' => [],
'chrome_web_icon' => 'Icon',
'chrome_icon' => 'Icon',
'adm_small_icon' => 'Icon',
'small_icon' => 'Icon',
'include_player_ids' => collect('player_id')
])
->andReturn($response);

$this->channel->send(new Notifiable(), new TestNotification());
}

/** @test */
public function it_throws_an_exception_when_it_could_not_send_the_notification()
{
$response = new Response(500,[],'ResponseBody');

$this->oneSignal->shouldReceive('sendNotificationCustom')
->once()
->with([
'contents' => ['en' => 'Body'],
'headings' => ['en' => 'Subject'],
'url' => 'URL',
'buttons' => [],
'web_buttons' => [],
'chrome_web_icon' => 'Icon',
'chrome_icon' => 'Icon',
'adm_small_icon' => 'Icon',
'small_icon' => 'Icon',
'include_player_ids' => collect('player_id')
])
->andReturn($response);

$this->setExpectedException(CouldNotSendNotification::class);

$this->channel->send(new Notifiable(), new TestNotification());
}
}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

111 changes: 111 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace NotificationChannels\OneSignal\Test;

use Illuminate\Support\Arr;
use NotificationChannels\OneSignal\OneSignalButton;
use NotificationChannels\OneSignal\OneSignalMessage;
use NotificationChannels\OneSignal\OneSignalWebButton;

class MessageTest extends \PHPUnit_Framework_TestCase
{
/** @var \NotificationChannels\OneSignal\OneSignalMessage */
protected $message;

public function setUp()
{
parent::setUp();
$this->message = new OneSignalMessage();
}

/** @test */
public function it_can_accept_a_message_when_constructing_a_message()
{
$message = new OneSignalMessage('Message body');

$this->assertEquals('Message body', Arr::get($message->toArray(), 'contents.en'));
}

/** @test */
public function it_provides_a_create_method()
{
$message = OneSignalMessage::create('Message body');

$this->assertEquals('Message body', Arr::get($message->toArray(), 'contents.en'));
}

/** @test */
public function it_can_set_the_body()
{
$this->message->body('myBody');

$this->assertEquals('myBody', Arr::get($this->message->toArray(), 'contents.en'));
}

/** @test */
public function it_can_set_the_subject()
{
$this->message->subject('mySubject');

$this->assertEquals('mySubject', Arr::get($this->message->toArray(), 'headings.en'));
}

/** @test */
public function it_can_set_the_url()
{
$this->message->url('myURL');

$this->assertEquals('myURL', Arr::get($this->message->toArray(), 'url'));
}

/** @test */
public function it_can_set_additional_data()
{
$this->message->setData('key_one', 'value_one');
$this->message->setData('key_two', 'value_two');

$this->assertEquals('value_one', Arr::get($this->message->toArray(), 'data.key_one'));
$this->assertEquals('value_two', Arr::get($this->message->toArray(), 'data.key_two'));
}

/** @test */
public function it_can_set_the_icon()
{
$this->message->icon('myIcon');

$this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_web_icon'));
$this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'chrome_icon'));
$this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'adm_small_icon'));
$this->assertEquals('myIcon', Arr::get($this->message->toArray(), 'small_icon'));
}

/** @test */
public function it_can_set_a_web_button()
{
$this->message->webButton(
OneSignalWebButton::create('buttonID')
->text('buttonText')
->url('buttonURL')
->icon('buttonIcon')
);

$this->assertEquals('buttonID', Arr::get($this->message->toArray(), 'web_buttons.0.id'));
$this->assertEquals('buttonText', Arr::get($this->message->toArray(), 'web_buttons.0.text'));
$this->assertEquals('buttonURL', Arr::get($this->message->toArray(), 'web_buttons.0.url'));
$this->assertEquals('buttonIcon', Arr::get($this->message->toArray(), 'web_buttons.0.icon'));
}

/** @test */
public function it_can_set_a_button()
{
$this->message->button(
OneSignalButton::create('buttonID')
->text('buttonText')
->icon('buttonIcon')
);

$this->assertEquals('buttonID', Arr::get($this->message->toArray(), 'buttons.0.id'));
$this->assertEquals('buttonText', Arr::get($this->message->toArray(), 'buttons.0.text'));
$this->assertEquals('buttonIcon', Arr::get($this->message->toArray(), 'buttons.0.icon'));
}
}
16 changes: 16 additions & 0 deletions tests/Notifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NotificationChannels\OneSignal\Test;

class Notifiable
{
use \Illuminate\Notifications\Notifiable;

/**
* @return int
*/
public function routeNotificationForOneSignal()
{
return 'player_id';
}
}
17 changes: 17 additions & 0 deletions tests/TestNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace NotificationChannels\OneSignal\Test;

use Illuminate\Notifications\Notification;
use NotificationChannels\OneSignal\OneSignalMessage;

class TestNotification extends Notification
{
public function toOneSignal($notifiable)
{
return (new OneSignalMessage('Body'))
->subject('Subject')
->icon('Icon')
->url('URL');
}
}

0 comments on commit 9b369db

Please sign in to comment.