LaraflashTest::testLoadMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.9
1
<?php
2
3
namespace Coderello\Laraflash\Tests;
4
5
use Coderello\Laraflash\Laraflash\Laraflash;
6
use Coderello\Laraflash\Tests\Support\FlashMessageFactory;
7
use Coderello\Laraflash\Tests\Support\LaraflashRenderer;
8
use Coderello\Laraflash\Tests\Support\MessagesStorage;
9
10
class LaraflashTest extends AbstractTestCase
11
{
12
    /** @var FlashMessageFactory */
13
    protected $flashMessageFactory;
14
15
    /** @var MessagesStorage */
16
    protected $messagesStorage;
17
18
    /** @var LaraflashRenderer */
19
    protected $laraflashRenderer;
20
21
    /** @var Laraflash */
22
    protected $laraflash;
23
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->flashMessageFactory = new FlashMessageFactory;
29
30
        $this->messagesStorage = new MessagesStorage;
31
32
        $this->laraflashRenderer = new LaraflashRenderer;
33
34
        $this->laraflash = new Laraflash(
35
            $this->flashMessageFactory,
36
            $this->messagesStorage,
37
            $this->laraflashRenderer
38
        );
39
    }
40
41
    public function testLoadMethod()
42
    {
43
        $firstMessage = $this->flashMessageFactory->make();
44
        $secondMessage = $this->flashMessageFactory->make();
45
46
        $this->messagesStorage->messages = [
47
            $firstMessage,
48
            $secondMessage,
49
            'invalid value',
50
        ];
51
52
        $this->assertCount(0, $this->laraflash->all());
53
54
        $this->laraflash->load();
55
56
        $this->assertCount(2, $this->laraflash->all());
57
58
        $this->assertSame($firstMessage, $this->laraflash->all()->first());
59
        $this->assertSame($secondMessage, $this->laraflash->all()->last());
60
    }
61
62
    public function testSaveMethod()
63
    {
64
        $message = $this->laraflash->message();
65
66
        $this->assertCount(0, $this->messagesStorage->messages);
67
68
        $this->laraflash->save();
69
70
        $this->assertCount(1, $this->messagesStorage->messages);
71
72
        $this->assertSame($message, $this->messagesStorage->messages[0]);
73
    }
74
75
    public function testMessageMethod()
76
    {
77
        $this->assertCount(0, $this->laraflash->all());
78
79
        $message = $this->laraflash->message();
80
81
        $this->assertCount(1, $this->laraflash->all());
82
83
        $this->assertSame($message, $this->laraflash->all()->first());
84
    }
85
86
    public function testKeepMethod()
87
    {
88
        $firstMessage = $this->laraflash->message()->hops(5);
89
        $secondMessage = $this->laraflash->message()->hops(3);
90
91
        $this->assertSame(5, $firstMessage->get('hops'));
92
        $this->assertSame(3, $secondMessage->get('hops'));
93
94
        $this->laraflash->keep();
95
96
        $this->assertSame(6, $firstMessage->get('hops'));
97
        $this->assertSame(4, $secondMessage->get('hops'));
98
    }
99
100
    public function testClearMethod()
101
    {
102
        $this->laraflash->message();
103
104
        $this->assertCount(1, $this->laraflash->all());
105
106
        $this->laraflash->clear();
107
108
        $this->assertCount(0, $this->laraflash->all());
109
    }
110
111
    public function testAllMethod()
112
    {
113
        $message = $this->laraflash->message();
114
115
        $this->assertCount(1, $this->laraflash->all());
116
117
        $this->assertSame($message, $this->laraflash->all()->first());
118
    }
119
120
    public function testReadyMethod()
121
    {
122
        $this->laraflash->message();
123
124
        $readyMessage = $this->laraflash->message()->now();
125
126
        $this->assertCount(1, $this->laraflash->ready());
127
128
        $this->assertSame($readyMessage, $this->laraflash->ready()->first());
129
    }
130
131
    public function testTouchMethod()
132
    {
133
        $this->laraflash->message()->delay(2)->hops(2);
134
135
        foreach ([[2, 2], [1, 2], [0, 2], [0, 1]] as $values) {
136
            $this->assertSame($values[0], $this->laraflash->all()->first()->get('delay'));
137
            $this->assertSame($values[1], $this->laraflash->all()->first()->get('hops'));
138
139
            $this->laraflash->touch();
140
        }
141
142
        $this->assertSame(0, $this->laraflash->all()->count());
143
    }
144
145
    public function testToArrayMethod()
146
    {
147
        $firstMessage = $this->laraflash->message()->now();
148
149
        $secondMessage = $this->laraflash->message()->now();
150
151
        $this->laraflash->message();
152
153
        $this->assertSame(
154
            [$firstMessage->toArray(), $secondMessage->toArray()],
155
            $this->laraflash->toArray()
156
        );
157
    }
158
159
    public function testJsonSerializeMethod()
160
    {
161
        $this->laraflash->message()->now();
162
163
        $this->laraflash->message()->now();
164
165
        $this->laraflash->message();
166
167
        $this->assertSame(
168
            $this->laraflash->toArray(),
169
            $this->laraflash->jsonSerialize()
170
        );
171
    }
172
173
    public function testToJsonMethod()
174
    {
175
        $this->laraflash->message()->now();
176
177
        $this->laraflash->message()->now();
178
179
        $this->laraflash->message();
180
181
        $this->assertSame(
182
            $this->laraflash->jsonSerialize(),
183
            json_decode($this->laraflash->toJson(), true)
184
        );
185
    }
186
187
    public function testOffsetExistsMethod()
188
    {
189
        $this->assertFalse($this->laraflash->offsetExists(0));
190
191
        $this->laraflash->message();
192
193
        $this->assertTrue($this->laraflash->offsetExists(0));
194
    }
195
196
    public function testOffsetGetMethod()
197
    {
198
        $this->assertNull($this->laraflash->offsetGet(0));
199
200
        $message = $this->laraflash->message();
201
202
        $this->assertSame($message, $this->laraflash->offsetGet(0));
203
    }
204
205
    public function testOffsetSetMethod()
206
    {
207
        $this->expectException(\BadMethodCallException::class);
208
209
        $this->laraflash->offsetSet(0, $this->flashMessageFactory->make());
210
    }
211
212
    public function testOffsetUnsetMethod()
213
    {
214
        $this->laraflash->message();
215
216
        $message = $this->laraflash->message();
217
218
        $this->laraflash->offsetUnset(0);
219
220
        $this->assertFalse($this->laraflash->offsetExists(0));
221
222
        $this->assertTrue($this->laraflash->offsetExists(1));
223
224
        $this->assertSame($message, $this->laraflash->offsetGet(1));
225
    }
226
227
    public function testToHtmlMethod()
228
    {
229
        $this->assertSame(
230
            $this->laraflash->render(),
231
            $this->laraflash->toHtml()
232
        );
233
    }
234
235
    public function testRenderMethod()
236
    {
237
        $this->assertSame(
238
            LaraflashRenderer::RESULT,
239
            $this->laraflash->render()
240
        );
241
    }
242
}
243