1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FtwSoft\NotificationChannels\Intercom\Tests; |
4
|
|
|
|
5
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\InvalidArgumentException; |
6
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException; |
7
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException; |
8
|
|
|
use FtwSoft\NotificationChannels\Intercom\IntercomChannel; |
9
|
|
|
use FtwSoft\NotificationChannels\Intercom\IntercomMessage; |
10
|
|
|
use FtwSoft\NotificationChannels\Intercom\Tests\Mocks\TestNotifiable; |
11
|
|
|
use FtwSoft\NotificationChannels\Intercom\Tests\Mocks\TestNotification; |
12
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
13
|
|
|
use GuzzleHttp\Psr7\Request; |
14
|
|
|
use Illuminate\Notifications\Notification; |
15
|
|
|
use Intercom\IntercomClient; |
16
|
|
|
use Intercom\IntercomMessages; |
17
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase; |
18
|
|
|
|
19
|
|
|
class IntercomChannelTest extends MockeryTestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Intercom\IntercomMessages|\Mockery\Mock |
23
|
|
|
*/ |
24
|
|
|
private $intercomMessages; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Intercom\IntercomClient |
28
|
|
|
*/ |
29
|
|
|
private $intercom; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \FtwSoft\NotificationChannels\Intercom\IntercomChannel |
33
|
|
|
*/ |
34
|
|
|
private $channel; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
|
40
|
|
|
$this->intercom = new IntercomClient(null, null); |
41
|
|
|
$this->intercomMessages = \Mockery::mock(IntercomMessages::class, $this->intercom); |
42
|
|
|
$this->intercom->messages = $this->intercomMessages; |
43
|
|
|
$this->channel = new IntercomChannel($this->intercom); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testItCanSendMessage(): void |
47
|
|
|
{ |
48
|
|
|
$notification = new TestNotification( |
49
|
|
|
IntercomMessage::create('Hello World!') |
50
|
|
|
->from(123) |
51
|
|
|
->toUserId(321) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->intercomMessages->shouldReceive('create') |
55
|
|
|
->once() |
56
|
|
|
->with([ |
57
|
|
|
'body' => 'Hello World!', |
58
|
|
|
'message_type' => 'inapp', |
59
|
|
|
'from' => [ |
60
|
|
|
'type' => 'admin', |
61
|
|
|
'id' => '123', |
62
|
|
|
], |
63
|
|
|
'to' => [ |
64
|
|
|
'type' => 'user', |
65
|
|
|
'id' => '321', |
66
|
|
|
], |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$this->channel->send(new TestNotifiable(), $notification); |
70
|
|
|
$this->assertPostConditions(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInThrowsAnExceptionWhenNotificationIsNotAnIntercomNotification() |
74
|
|
|
{ |
75
|
|
|
$notification = new Notification(); |
76
|
|
|
|
77
|
|
|
$this->expectException(InvalidArgumentException::class); |
78
|
|
|
$this->channel->send(new TestNotifiable(), $notification); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testItThrowsAnExceptionWhenRecipientIsNotProvided() |
82
|
|
|
{ |
83
|
|
|
$notification = new TestNotification( |
84
|
|
|
IntercomMessage::create('Hello World!') |
85
|
|
|
->from(123) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->expectException(MessageIsNotCompleteException::class); |
89
|
|
|
$this->channel->send(new TestNotifiable(), $notification); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testItThrowsAnExceptionSomeOfRequiredParamsAreNotDefined() |
93
|
|
|
{ |
94
|
|
|
$notification = new TestNotification( |
95
|
|
|
IntercomMessage::create() |
96
|
|
|
->from(123) |
97
|
|
|
->toUserId(321) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->expectException(MessageIsNotCompleteException::class); |
101
|
|
|
$this->channel->send(new TestNotifiable(), $notification); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testItThrowsRequestExceptionOnGuzzleBadResponseException(): void |
105
|
|
|
{ |
106
|
|
|
$this->intercomMessages->shouldReceive('create') |
107
|
|
|
->once() |
108
|
|
|
->andThrow(new BadResponseException('Test case', new Request('post', 'http://foo.bar'))); |
109
|
|
|
|
110
|
|
|
$notification = new TestNotification( |
111
|
|
|
IntercomMessage::create('Hello World!') |
112
|
|
|
->from(123) |
113
|
|
|
->toUserId(321) |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->expectException(RequestException::class); |
117
|
|
|
|
118
|
|
|
$this->channel->send(new TestNotifiable(), $notification); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testItGetsToFromRouteNotificationForIntercomMethod(): void |
122
|
|
|
{ |
123
|
|
|
$this->intercomMessages->shouldReceive('create'); |
124
|
|
|
|
125
|
|
|
$message = IntercomMessage::create('Hello World!') |
126
|
|
|
->from(123); |
127
|
|
|
$notification = new TestNotification($message); |
128
|
|
|
|
129
|
|
|
$expected = ['type' => 'user', 'id' => 321]; |
130
|
|
|
$this->channel->send(new TestNotifiable($expected), $notification); |
131
|
|
|
|
132
|
|
|
$this->assertEquals($expected, $message->payload['to']); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|