FlashMessageTest::testMagicUnsetMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Coderello\Laraflash\Tests;
4
5
use Coderello\Laraflash\Exceptions\InvalidDelayException;
6
use Coderello\Laraflash\Exceptions\InvalidHopsAmountException;
7
use Coderello\Laraflash\FlashMessage\FlashMessage;
8
use Illuminate\Support\Arr;
9
10
class FlashMessageTest extends AbstractTestCase
11
{
12
    /** @var FlashMessage */
13
    protected $flashMessage;
14
15
    protected function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $this->flashMessage = new FlashMessage;
20
    }
21
22
    protected function getMessageState(array $override = [])
23
    {
24
        $initialState = [
25
            'content' => null,
26
            'title' => null,
27
            'type' => null,
28
            'hops' => 1,
29
            'delay' => 1,
30
        ];
31
32
        return array_merge($initialState, $override);
33
    }
34
35
    public function testInitialState()
36
    {
37
        $expected = $this->getMessageState();
38
39
        $actual = $this->flashMessage->toArray();
40
41
        $this->assertSame(
42
            Arr::sortRecursive($expected),
43
            Arr::sortRecursive($actual)
44
        );
45
    }
46
47
    public function testContentMethod()
48
    {
49
        $message = $this->flashMessage
50
            ->content('foo');
51
52
        $this->assertSame(
53
            Arr::sortRecursive($this->getMessageState(['content' => 'foo'])),
54
            Arr::sortRecursive($message->toArray())
55
        );
56
    }
57
58
    public function testTitleMethod()
59
    {
60
        $message = $this->flashMessage
61
            ->title('foo');
62
63
        $this->assertSame(
64
            Arr::sortRecursive($this->getMessageState(['title' => 'foo'])),
65
            Arr::sortRecursive($message->toArray())
66
        );
67
    }
68
69
    public function testTypeMethod()
70
    {
71
        $message = $this->flashMessage
72
            ->type('foo');
73
74
        $this->assertSame(
75
            Arr::sortRecursive($this->getMessageState(['type' => 'foo'])),
76
            Arr::sortRecursive($message->toArray())
77
        );
78
    }
79
80
    public function testDangerMethod()
81
    {
82
        $message = $this->flashMessage
83
            ->danger();
84
85
        $this->assertSame(
86
            Arr::sortRecursive($this->getMessageState(['type' => 'danger'])),
87
            Arr::sortRecursive($message->toArray())
88
        );
89
    }
90
91
    public function testWarningMethod()
92
    {
93
        $message = $this->flashMessage
94
            ->warning();
95
96
        $this->assertSame(
97
            Arr::sortRecursive($this->getMessageState(['type' => 'warning'])),
98
            Arr::sortRecursive($message->toArray())
99
        );
100
    }
101
102
    public function testInfoMethod()
103
    {
104
        $message = $this->flashMessage
105
            ->info();
106
107
        $this->assertSame(
108
            Arr::sortRecursive($this->getMessageState(['type' => 'info'])),
109
            Arr::sortRecursive($message->toArray())
110
        );
111
    }
112
113
    public function testSuccessMethod()
114
    {
115
        $message = $this->flashMessage
116
            ->success();
117
118
        $this->assertSame(
119
            Arr::sortRecursive($this->getMessageState(['type' => 'success'])),
120
            Arr::sortRecursive($message->toArray())
121
        );
122
    }
123
124
    public function testHopsMethod()
125
    {
126
        $message = $this->flashMessage
127
            ->hops(5);
128
129
        $this->assertSame(
130
            Arr::sortRecursive($this->getMessageState(['hops' => 5])),
131
            Arr::sortRecursive($message->toArray())
132
        );
133
    }
134
135
    public function testHopsMethodWithInvalidHopsAmount()
136
    {
137
        $this->expectException(InvalidHopsAmountException::class);
138
139
        $this->flashMessage
140
            ->hops(0);
141
    }
142
143
    public function testDelayMethod()
144
    {
145
        $message = $this->flashMessage
146
            ->delay(5);
147
148
        $this->assertSame(
149
            Arr::sortRecursive($this->getMessageState(['delay' => 5])),
150
            Arr::sortRecursive($message->toArray())
151
        );
152
    }
153
154
    public function testDelayMethodWithInvalidDelay()
155
    {
156
        $this->expectException(InvalidDelayException::class);
157
158
        $this->flashMessage
159
            ->delay(-1);
160
    }
161
162
    public function testNowMethod()
163
    {
164
        $message = $this->flashMessage
165
            ->delay(5);
166
167
        $this->assertSame(
168
            Arr::sortRecursive($this->getMessageState(['delay' => 5])),
169
            Arr::sortRecursive($message->toArray())
170
        );
171
    }
172
173
    public function testKeepMethod()
174
    {
175
        $message = $this->flashMessage
176
            ->hops(5)
177
            ->keep();
178
179
        $this->assertSame(
180
            Arr::sortRecursive($this->getMessageState(['hops' => 6])),
181
            Arr::sortRecursive($message->toArray())
182
        );
183
    }
184
185
    public function testAttributeMethodWithNonNullValue()
186
    {
187
        $message = $this->flashMessage
188
            ->attribute('foo', 'bar');
189
190
        $this->assertSame(
191
            Arr::sortRecursive($this->getMessageState(['foo' => 'bar'])),
192
            Arr::sortRecursive($message->toArray())
193
        );
194
    }
195
196
    public function testAttributeMethodWithNullValue()
197
    {
198
        $message = $this->flashMessage
199
            ->attribute('foo', 'bar')
200
            ->attribute('foo', null);
201
202
        $this->assertSame(
203
            Arr::sortRecursive($this->getMessageState()),
204
            Arr::sortRecursive($message->toArray())
205
        );
206
    }
207
208
    public function testGetMethod()
209
    {
210
        $message = $this->flashMessage
211
            ->hops(5)
212
            ->attribute('foo', 'bar');
213
214
        $this->assertSame(5, $message->get('hops'));
215
        $this->assertSame('bar', $message->get('foo'));
216
        $this->assertSame(null, $message->get('baz'));
217
    }
218
219
    public function testToArrayMethod()
220
    {
221
        $message = $this->flashMessage
222
            ->attribute('foo', 'bar')
223
            ->delay(4);
224
225
        $this->assertSame(
226
            Arr::sortRecursive($this->getMessageState([
227
                'foo' => 'bar',
228
                'delay' => 4,
229
            ])),
230
            Arr::sortRecursive($message->toArray())
231
        );
232
    }
233
234
    public function testToJsonMethod()
235
    {
236
        $message = $this->flashMessage;
237
238
        $this->assertSame(
239
            Arr::sortRecursive($this->getMessageState()),
240
            Arr::sortRecursive(json_decode($message->toJson(), true))
241
        );
242
    }
243
244
    public function testJsonSerialize()
245
    {
246
        $message = $this->flashMessage;
247
248
        $this->assertSame(
249
            Arr::sortRecursive($message->toArray()),
250
            Arr::sortRecursive($message->jsonSerialize())
251
        );
252
    }
253
254
    public function testOffsetExistsMethod()
255
    {
256
        $message = $this->flashMessage;
257
258
        $this->assertFalse($message->offsetExists('foo'));
259
260
        $message->attribute('foo', 'bar');
261
262
        $this->assertTrue($message->offsetExists('foo'));
263
    }
264
265
    public function testOffsetGetMethod()
266
    {
267
        $message = $this->flashMessage
268
            ->attribute('foo', 'bar');
269
270
        $this->assertSame($message->offsetGet('foo'), 'bar');
271
    }
272
273
    public function testOffsetGetMethodWithNonexistentAttribute()
274
    {
275
        $message = $this->flashMessage;
276
277
        $this->expectException(\ErrorException::class);
278
279
        $message->offsetGet('foo');
280
    }
281
282
    public function testOffsetSetMethod()
283
    {
284
        $message = $this->flashMessage;
285
286
        $message['foo'] = 'bar';
287
288
        $this->assertSame(
289
            Arr::sortRecursive($this->getMessageState([
290
                'foo' => 'bar',
291
            ])),
292
            Arr::sortRecursive($message->toArray())
293
        );
294
    }
295
296
    public function testOffsetUnsetMethod()
297
    {
298
        $message = $this->flashMessage
299
            ->attribute('foo', 'bar');
300
301
        $message->offsetUnset('foo');
302
303
        $this->assertSame(
304
            Arr::sortRecursive($this->getMessageState()),
305
            Arr::sortRecursive($message->toArray())
306
        );
307
    }
308
309
    public function testMagicGetMethod()
310
    {
311
        $message = $this->flashMessage
312
            ->attribute('foo', 'bar');
313
314
        $this->assertSame('bar', $message->__get('foo'));
315
        $this->assertSame(null, $message->__get('baz'));
316
    }
317
318
    public function testMagicSetMethod()
319
    {
320
        $message = $this->flashMessage;
321
322
        $message->__set('foo', 'bar');
323
324
        $this->assertSame('bar', $message->__get('foo'));
325
    }
326
327
    public function testMagicIssetMethod()
328
    {
329
        $message = $this->flashMessage;
330
331
        $this->assertFalse($message->__isset('foo'));
332
333
        $message->attribute('foo', 'bar');
334
335
        $this->assertTrue($message->__isset('foo'));
336
    }
337
338
    public function testMagicUnsetMethod()
339
    {
340
        $message = $this->flashMessage
341
            ->attribute('foo', 'bar');
342
343
        $message->__unset('foo');
344
345
        $this->assertSame(
346
            Arr::sortRecursive($this->getMessageState()),
347
            Arr::sortRecursive($message->toArray())
348
        );
349
    }
350
}
351