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