|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coderello\Laraflash\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use ErrorException; |
|
7
|
|
|
use Coderello\Laraflash\FlashMessage; |
|
8
|
|
|
use Coderello\Laraflash\FlashMessagesBag; |
|
9
|
|
|
use Coderello\Laraflash\Exceptions\InvalidArgumentException; |
|
10
|
|
|
use Coderello\Laraflash\Contracts\FlashMessagesBag as FlashMessagesBagContract; |
|
11
|
|
|
|
|
12
|
|
|
class FlashMessageBagTest extends AbstractTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function test_implements_contract() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->assertTrue(new FlashMessagesBag instanceof FlashMessagesBagContract); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function test_initial_state() |
|
20
|
|
|
{ |
|
21
|
|
|
$bag = new FlashMessagesBag(); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertEquals([], $bag->all()); |
|
24
|
|
|
$this->assertEquals([], $bag->ready()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function test_add_method_without_arguments() |
|
28
|
|
|
{ |
|
29
|
|
|
$bag = new FlashMessagesBag(); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertEquals(0, count($bag->all())); |
|
32
|
|
|
|
|
33
|
|
|
$bag->add(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals(1, count($bag->all())); |
|
36
|
|
|
|
|
37
|
|
|
$message = $bag->all()[0]; |
|
38
|
|
|
|
|
39
|
|
|
$this->assertTrue($message instanceof FlashMessage); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_add_method_with_message_argument() |
|
43
|
|
|
{ |
|
44
|
|
|
$bag = new FlashMessagesBag(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals(0, count($bag->all())); |
|
47
|
|
|
|
|
48
|
|
|
$message = new FlashMessage(); |
|
49
|
|
|
|
|
50
|
|
|
$bag->add($message); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals(1, count($bag->all())); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($message, $bag->all()[0]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function test_clear_method() |
|
58
|
|
|
{ |
|
59
|
|
|
$bag = new FlashMessagesBag(); |
|
60
|
|
|
|
|
61
|
|
|
$bag->add(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals(1, count($bag->all())); |
|
64
|
|
|
|
|
65
|
|
|
$bag->clear(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(0, count($bag->all())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function test_keep_method() |
|
71
|
|
|
{ |
|
72
|
|
|
$bag = new FlashMessagesBag(); |
|
73
|
|
|
|
|
74
|
|
|
$bag->add()->hops(2); |
|
75
|
|
|
|
|
76
|
|
|
$bag->add()->hops(6); |
|
77
|
|
|
|
|
78
|
|
|
$bag->keep(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals(3, $bag[0]['hops']); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals(7, $bag[1]['hops']); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function test_all_method() |
|
86
|
|
|
{ |
|
87
|
|
|
$bag = new FlashMessagesBag(); |
|
88
|
|
|
|
|
89
|
|
|
$firstMessage = $bag->add(); |
|
90
|
|
|
$secondMessage = $bag->add(); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertSame([ |
|
93
|
|
|
0 => $firstMessage, |
|
94
|
|
|
1 => $secondMessage, |
|
95
|
|
|
], $bag->all()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function test_ready_method() |
|
99
|
|
|
{ |
|
100
|
|
|
$bag = new FlashMessagesBag(); |
|
101
|
|
|
|
|
102
|
|
|
$bag->add(); |
|
103
|
|
|
$secondMessage = $bag->add()->now(); |
|
104
|
|
|
$thirdMessage = $bag->add()->delay(0); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertSame([ |
|
107
|
|
|
1 => $secondMessage, |
|
108
|
|
|
2 => $thirdMessage, |
|
109
|
|
|
], $bag->ready()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function test_bag_implements_array_access() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->assertTrue(new FlashMessagesBag() instanceof ArrayAccess); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function test_offset_exists_method() |
|
118
|
|
|
{ |
|
119
|
|
|
$bag = new FlashMessagesBag(); |
|
120
|
|
|
|
|
121
|
|
|
$bag->add(); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertTrue($bag->offsetExists(0)); |
|
124
|
|
|
$this->assertFalse($bag->offsetExists(1)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function test_offset_get_method() |
|
128
|
|
|
{ |
|
129
|
|
|
$bag = new FlashMessagesBag(); |
|
130
|
|
|
|
|
131
|
|
|
$message = $bag->add(); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame($message, $bag[0]); |
|
134
|
|
|
|
|
135
|
|
|
$this->expectException(ErrorException::class); |
|
136
|
|
|
|
|
137
|
|
|
$bag[1]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function test_offset_set_method() |
|
141
|
|
|
{ |
|
142
|
|
|
$bag = new FlashMessagesBag(); |
|
143
|
|
|
|
|
144
|
|
|
$message = new FlashMessage(); |
|
145
|
|
|
|
|
146
|
|
|
$bag[2] = $message; |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals(1, count($bag->all())); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertSame($message, $bag[2]); |
|
151
|
|
|
|
|
152
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
153
|
|
|
|
|
154
|
|
|
$bag[3] = 'Hello, I\'m string.'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function test_offset_unset_method() |
|
158
|
|
|
{ |
|
159
|
|
|
$bag = new FlashMessagesBag(); |
|
160
|
|
|
|
|
161
|
|
|
$bag->add(); |
|
162
|
|
|
|
|
163
|
|
|
$secondMessage = $bag->add(); |
|
164
|
|
|
|
|
165
|
|
|
unset($bag[0]); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertSame([ |
|
168
|
|
|
1 => $secondMessage, |
|
169
|
|
|
], $bag->all()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function test_prepare_method_hops_amount_will_not_be_decremented_if_delay_is_greater_than_zero() |
|
173
|
|
|
{ |
|
174
|
|
|
$bag = new FlashMessagesBag(); |
|
175
|
|
|
|
|
176
|
|
|
$bag->add()->hops(2)->delay(2); |
|
177
|
|
|
|
|
178
|
|
|
$bag->prepare(); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertSame(2, $bag[0]->toArray()['hops']); |
|
181
|
|
|
|
|
182
|
|
|
$bag->prepare(); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertSame(2, $bag[0]->toArray()['hops']); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function test_prepare_method_hops_amount_will_be_decremented_if_delay_is_zero() |
|
188
|
|
|
{ |
|
189
|
|
|
$bag = new FlashMessagesBag(); |
|
190
|
|
|
|
|
191
|
|
|
$bag->add()->hops(5)->delay(0); |
|
192
|
|
|
|
|
193
|
|
|
$bag->prepare(); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertSame(4, $bag[0]->toArray()['hops']); |
|
196
|
|
|
|
|
197
|
|
|
$bag->prepare(); |
|
198
|
|
|
|
|
199
|
|
|
$this->assertSame(3, $bag[0]->toArray()['hops']); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function test_prepare_method_delay_will_be_decremented_if_it_is_greater_than_zero() |
|
203
|
|
|
{ |
|
204
|
|
|
$bag = new FlashMessagesBag(); |
|
205
|
|
|
|
|
206
|
|
|
$bag->add()->hops(4)->delay(2); |
|
207
|
|
|
|
|
208
|
|
|
$bag->prepare(); |
|
209
|
|
|
|
|
210
|
|
|
$this->assertSame(1, $bag[0]->toArray()['delay']); |
|
211
|
|
|
|
|
212
|
|
|
$bag->prepare(); |
|
213
|
|
|
|
|
214
|
|
|
$this->assertSame(0, $bag[0]->toArray()['delay']); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function test_prepare_method_delay_will_not_be_decremented_if_it_is_zero() |
|
218
|
|
|
{ |
|
219
|
|
|
$bag = new FlashMessagesBag(); |
|
220
|
|
|
|
|
221
|
|
|
$bag->add()->hops(6)->delay(0); |
|
222
|
|
|
|
|
223
|
|
|
$bag->prepare(); |
|
224
|
|
|
|
|
225
|
|
|
$this->assertSame(0, $bag[0]->toArray()['delay']); |
|
226
|
|
|
|
|
227
|
|
|
$bag->prepare(); |
|
228
|
|
|
|
|
229
|
|
|
$this->assertSame(0, $bag[0]->toArray()['delay']); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function test_prepare_method_message_will_be_deleted_if_hops_amount_is_less_or_equal_to_one_and_delay_is_zero() |
|
233
|
|
|
{ |
|
234
|
|
|
$bag = new FlashMessagesBag(); |
|
235
|
|
|
|
|
236
|
|
|
$bag->add()->hops(1)->delay(0); |
|
237
|
|
|
|
|
238
|
|
|
$bag->prepare(); |
|
239
|
|
|
|
|
240
|
|
|
$this->assertFalse(isset($bag[0])); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function test_json_serialize_method() |
|
244
|
|
|
{ |
|
245
|
|
|
$bag = new FlashMessagesBag(); |
|
246
|
|
|
|
|
247
|
|
|
$bag->add()->hops(2); |
|
248
|
|
|
|
|
249
|
|
|
$bag->prepare(); |
|
250
|
|
|
|
|
251
|
|
|
$response = $bag->jsonSerialize(); |
|
252
|
|
|
|
|
253
|
|
|
$this->assertEquals(2, $response[0]['hops']); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function test_to_json_method() |
|
257
|
|
|
{ |
|
258
|
|
|
$bag = new FlashMessagesBag(); |
|
259
|
|
|
|
|
260
|
|
|
$bag->add()->hops(2); |
|
261
|
|
|
|
|
262
|
|
|
$bag->prepare(); |
|
263
|
|
|
|
|
264
|
|
|
$response = $bag->toJson(); |
|
265
|
|
|
|
|
266
|
|
|
$this->assertEquals(2, json_decode($response)[0]->hops); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function test_to_array_method() |
|
270
|
|
|
{ |
|
271
|
|
|
$bag = new FlashMessagesBag(); |
|
272
|
|
|
|
|
273
|
|
|
$bag->add()->hops(2); |
|
274
|
|
|
|
|
275
|
|
|
$bag->prepare(); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertEquals([ |
|
278
|
|
|
'title' => null, |
|
279
|
|
|
'content' => null, |
|
280
|
|
|
'type' => 'info', |
|
281
|
|
|
'hops' => 2, |
|
282
|
|
|
'delay' => 0, |
|
283
|
|
|
'important' => false |
|
284
|
|
|
], $bag->toArray()[0]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function test_render_method() |
|
288
|
|
|
{ |
|
289
|
|
|
$bag = new FlashMessagesBag(); |
|
290
|
|
|
|
|
291
|
|
|
$bag->add()->hops(2); |
|
292
|
|
|
|
|
293
|
|
|
$bag->prepare(); |
|
294
|
|
|
|
|
295
|
|
|
try { |
|
296
|
|
|
$this->assertEquals( |
|
297
|
|
|
view(config('laraflash.skin'), [ |
|
298
|
|
|
'title' => null, |
|
299
|
|
|
'content' => null, |
|
300
|
|
|
'type' => 'info', |
|
301
|
|
|
])->render(), |
|
302
|
|
|
$bag->render() |
|
303
|
|
|
); |
|
304
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|