Skip to content

Commit ca8eea7

Browse files
committed
test for firing failure event
1 parent ae690b4 commit ca8eea7

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/PusherChannel.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@
22

33
namespace NotificationChannels\PusherPushNotifications;
44

5-
use Illuminate\Notifications\Events\NotificationFailed;
6-
use Illuminate\Notifications\Notification;
75
use Pusher;
6+
use Illuminate\Events\Dispatcher;
7+
use Illuminate\Notifications\Notification;
8+
use Illuminate\Notifications\Events\NotificationFailed;
89

910
class PusherChannel
1011
{
11-
/** @var Pusher */
12+
/**
13+
* @var \Pusher
14+
*/
1215
protected $pusher;
1316

17+
/**
18+
* @var \Illuminate\Events\Dispatcher
19+
*/
20+
private $events;
21+
1422
/**
1523
* @param \Pusher $pusher
1624
*/
17-
public function __construct(Pusher $pusher)
25+
public function __construct(Pusher $pusher, Dispatcher $events)
1826
{
1927
$this->pusher = $pusher;
28+
$this->events = $events;
2029
}
2130

2231
/**
@@ -26,7 +35,6 @@ public function __construct(Pusher $pusher)
2635
* @param \Illuminate\Notifications\Notification $notification
2736
*
2837
* @return void
29-
* @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotSendNotification
3038
*/
3139
public function send($notifiable, Notification $notification)
3240
{
@@ -40,7 +48,9 @@ public function send($notifiable, Notification $notification)
4048
);
4149

4250
if (! in_array($response['status'], [200, 202])) {
43-
event(new NotificationFailed($notifiable, $notification, $response));
51+
$this->events->fire(
52+
new NotificationFailed($notifiable, $notification, $response)
53+
);
4454
}
4555
}
4656

tests/ChannelTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace NotificationChannels\PusherPushNotifications\Test;
44

5+
use Illuminate\Events\Dispatcher;
6+
use Illuminate\Notifications\Events\NotificationFailed;
57
use Illuminate\Notifications\Notifiable;
68
use NotificationChannels\PusherPushNotifications\PusherChannel;
79
use Illuminate\Notifications\Notification;
@@ -16,7 +18,9 @@ public function setUp()
1618
{
1719
$this->pusher = Mockery::mock(Pusher::class);
1820

19-
$this->channel = new PusherChannel($this->pusher);
21+
$this->events = Mockery::mock(Dispatcher::class);
22+
23+
$this->channel = new PusherChannel($this->pusher, $this->events);
2024

2125
$this->notification = new TestNotification;
2226

@@ -41,6 +45,20 @@ public function it_can_send_a_notification()
4145

4246
$this->channel->send($this->notifiable, $this->notification);
4347
}
48+
49+
/** @test */
50+
public function it_fires_failure_event_on_failure()
51+
{
52+
$message = $this->notification->toPushNotification($this->notifiable);
53+
54+
$data = $message->toArray();
55+
56+
$this->pusher->shouldReceive('notify')->with('interest_name', $data, true)->andReturn(['status' => 500]);
57+
58+
$this->events->shouldReceive('fire')->with(Mockery::type(NotificationFailed::class));
59+
60+
$this->channel->send($this->notifiable, $this->notification);
61+
}
4462
}
4563

4664
class TestNotifiable

0 commit comments

Comments
 (0)