Passed
Push — master ( 7c85b2...443a3c )
by Andrey
03:17
created

testItAcceptsBodyWhenConstructed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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