Completed
Push — master ( 4671d6...c5be3c )
by Andrey
03:04
created

IntercomMessageTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 93
dl 0
loc 201
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FtwSoft\NotificationChannels\Intercom\Tests;
4
5
use FtwSoft\NotificationChannels\Intercom\IntercomMessage;
6
use PHPUnit\Framework\TestCase;
7
8
class IntercomMessageTest extends TestCase
9
{
10
    public function testThatTypeInappConstantSetCorrectly(): void
11
    {
12
        $this->assertEquals('inapp', IntercomMessage::TYPE_INAPP);
13
    }
14
15
    public function testThatTypeEmailConstantSetCorrectly(): void
16
    {
17
        $this->assertEquals('email', IntercomMessage::TYPE_EMAIL);
18
    }
19
20
    public function testThatTemplatePlainConstantSetCorrectly(): void
21
    {
22
        $this->assertEquals('plain', IntercomMessage::TEMPLATE_PLAIN);
23
    }
24
25
    public function testThatTemplatePersonalConstantSetCorrectly(): void
26
    {
27
        $this->assertEquals('personal', IntercomMessage::TEMPLATE_PERSONAL);
28
    }
29
30
    public function testItAcceptsBodyWhenConstructed(): void
31
    {
32
        $message = new IntercomMessage('Intercom message test');
33
        $this->assertEquals('Intercom message test', $message->payload['body']);
34
    }
35
36
    public function testThatDefaultMessageTypeIsInapp(): void
37
    {
38
        $message = new IntercomMessage();
39
        $this->assertEquals(IntercomMessage::TYPE_INAPP, $message->payload['message_type']);
40
    }
41
42
    public function testThatBodyCanBeSet(): void
43
    {
44
        $message = new IntercomMessage('Intercom message test');
45
        $message->body('Some other intercom body');
46
        $this->assertEquals('Some other intercom body', $message->payload['body']);
47
    }
48
49
    public function testThatMessageTypeToEmailCanBeSet(): void
50
    {
51
        $message = new IntercomMessage();
52
        $message->email();
53
        $this->assertEquals(IntercomMessage::TYPE_EMAIL, $message->payload['message_type']);
54
    }
55
56
    public function testThatMessageTypeToInappCanBeSet(): void
57
    {
58
        $message = new IntercomMessage();
59
        $message->email()->inapp();
60
        $this->assertEquals(IntercomMessage::TYPE_INAPP, $message->payload['message_type']);
61
    }
62
63
    public function testThatSubjectCanBeSet(): void
64
    {
65
        $message = new IntercomMessage();
66
        $message->subject('Some interesting subject');
67
        $this->assertEquals('Some interesting subject', $message->payload['subject']);
68
    }
69
70
    public function testThatTemplatePlainCanBeSet(): void
71
    {
72
        $message = new IntercomMessage();
73
        $message->plain();
74
        $this->assertEquals(IntercomMessage::TEMPLATE_PLAIN, $message->payload['template']);
75
    }
76
77
    public function testThatTemplatePersonalCanBeSet(): void
78
    {
79
        $message = new IntercomMessage();
80
        $message->personal();
81
        $this->assertEquals(IntercomMessage::TEMPLATE_PERSONAL, $message->payload['template']);
82
    }
83
84
    public function testThatSenderCanBeSet(): void
85
    {
86
        $message = new IntercomMessage();
87
        $message->from(123);
88
        $this->assertEquals(
89
            [
90
                'type' => 'admin',
91
                'id'   => 123,
92
            ],
93
            $message->payload['from']
94
        );
95
    }
96
97
    public function testThatRecipientCanBeSet(): void
98
    {
99
        $message = new IntercomMessage();
100
        $expected = [
101
            'type'  => 'user',
102
            'email' => '[email protected]',
103
        ];
104
        $message->to($expected);
105
106
        $this->assertEquals($expected, $message->payload['to']);
107
    }
108
109
    public function testThatRecipientUserIdCanBeSet(): void
110
    {
111
        $message = new IntercomMessage();
112
        $message->toUserId(456);
113
        $this->assertEquals(
114
            [
115
                'type' => 'user',
116
                'id'   => 456,
117
            ],
118
            $message->payload['to']
119
        );
120
    }
121
122
    public function testThatRecipientUserEmailCanBeSet(): void
123
    {
124
        $message = new IntercomMessage();
125
        $message->toUserEmail('[email protected]');
126
        $this->assertEquals(
127
            [
128
                'type'  => 'user',
129
                'email' => '[email protected]',
130
            ],
131
            $message->payload['to']
132
        );
133
    }
134
135
    public function testThatContactIdCanBeSet(): void
136
    {
137
        $message = new IntercomMessage();
138
        $message->toContactId(789);
139
        $this->assertEquals(
140
            [
141
                'type' => 'contact',
142
                'id'   => 789,
143
            ],
144
            $message->payload['to']
145
        );
146
    }
147
148
    public function testItCanDetermiteIfToIsNotGiven()
149
    {
150
        $message = new IntercomMessage();
151
        $this->assertFalse($message->toIsGiven());
152
153
        $message->toUserId(123);
154
        $this->assertTrue($message->toIsGiven());
155
    }
156
157
    public function testInCanDetermineWhenRequiredParamsAreNotSet(): void
158
    {
159
        $message = new IntercomMessage();
160
        $this->assertFalse($message->isValid());
161
162
        $message->body('Some body');
163
        $this->assertFalse($message->isValid());
164
165
        $message->from(123);
166
        $this->assertFalse($message->isValid());
167
168
        $message->toUserId(321);
169
        $this->assertTrue($message->isValid());
170
    }
171
172
    public function testItCanReturnPayloadAsAnArray(): void
173
    {
174
        $message = new IntercomMessage();
175
176
        $message
177
            ->email()
178
            ->personal()
179
            ->from(123)
180
            ->toUserEmail('[email protected]')
181
            ->subject('Test case')
182
            ->body('Some message');
183
184
        $expected = [
185
            'message_type' => 'email',
186
            'template'     => 'personal',
187
            'from'         => [
188
                'type' => 'admin',
189
                'id'   => '123',
190
            ],
191
            'to'           => [
192
                'type'  => 'user',
193
                'email' => '[email protected]',
194
            ],
195
            'subject'      => 'Test case',
196
            'body'         => 'Some message',
197
        ];
198
199
        $this->assertEquals($expected, $message->toArray());
200
    }
201
202
    public function testThatStaticCreateMethodProvidesBodyToObject(): void
203
    {
204
        $message = IntercomMessage::create();
205
        $this->assertFalse(isset($message->payload['body']));
206
207
        $message = IntercomMessage::create('Intercom message test');
208
        $this->assertEquals('Intercom message test', $message->payload['body']);
209
    }
210
}
211