forked from jasny/session-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashBagTest.php
More file actions
152 lines (115 loc) · 4.28 KB
/
Copy pathFlashBagTest.php
File metadata and controls
152 lines (115 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace Jasny\Session\Tests\Flash;
use Jasny\PHPUnit\ExpectWarningTrait;
use Jasny\Session\Flash\Flash;
use Jasny\Session\Flash\FlashBag;
use PHPUnit\Framework\TestCase;
/**
* @covers \Jasny\Session\Flash\Flash
* @covers \Jasny\Session\Flash\FlashBag
*/
class FlashBagTest extends TestCase
{
use ExpectWarningTrait;
public function testFromSession()
{
$session = new \ArrayObject([
'flash' => [
['type' => 'notice', 'message' => 'one', 'contentType' => 'text/html'],
['message' => 'two']
]
]);
$baseBag = new FlashBag();
$bag = $baseBag->withSession($session);
$this->assertNotSame($baseBag, $bag);
$this->assertArrayNotHasKey('flash', $session->getArrayCopy());
$expected = [
new Flash('notice', 'one', 'text/html'),
new Flash('', 'two', 'text/plain'),
];
$this->assertEquals($expected, $bag->getArrayCopy());
$this->assertSame($bag, $bag->withSession($session));
$this->assertEquals($expected, $bag->getArrayCopy());
$entries = iterator_to_array($bag, true);
$this->assertEquals($expected, $entries);
}
public function testWithInvalidData()
{
$session = new \ArrayObject([
'flash' => 'foo'
]);
$this->expectWarningMessage('Invalid flash session data');
$bag = (new FlashBag())->withSession($session);
$this->assertEquals([], $bag->getArrayCopy());
$this->assertArrayNotHasKey('flash', $session->getArrayCopy());
}
public function testWithInvalidEntries()
{
$session = new \ArrayObject([
'flash' => [
['abc'],
['foo' => 'bar'],
['message' => 'two'],
]
]);
$this->expectNoticeMessage('Ignoring 2 invalid flash messages');
$bag = (new FlashBag())->withSession($session);
$this->assertArrayNotHasKey('flash', $session->getArrayCopy());
$expected = [
new Flash('', 'two', 'text/plain'),
];
$this->assertEquals($expected, $bag->getArrayCopy());
}
public function testAdd()
{
$session = new \ArrayObject();
$bag = (new FlashBag())->withSession($session);
$expected = [
['type' => 'notice', 'message' => 'foo', 'contentType' => 'text/plain'],
['type' => 'warning', 'message' => 'bar', 'contentType' => 'text/html'],
];
$bag->add('notice', 'foo');
$bag->add('warning', 'bar', 'text/html');
$this->assertArrayHasKey('flash', $session->getArrayCopy());
$this->assertEquals($expected, $session['flash']);
$this->assertEquals([], $bag->getArrayCopy());
}
public function testReissue()
{
$session = new \ArrayObject([
'flash' => [
['type' => 'notice', 'message' => 'one', 'contentType' => 'text/html'],
['message' => 'two']
]
]);
$bag = (new FlashBag())->withSession($session);
$bag->add('notice', 'foo');
$bag->add('warning', 'bar', 'text/html');
$expected = [
['type' => 'notice', 'message' => 'one', 'contentType' => 'text/html'],
['type' => '', 'message' => 'two', 'contentType' => 'text/plain'],
['type' => 'notice', 'message' => 'foo', 'contentType' => 'text/plain'],
['type' => 'warning', 'message' => 'bar', 'contentType' => 'text/html'],
];
$bag->reissue();
$this->assertArrayHasKey('flash', $session->getArrayCopy());
$this->assertEquals($expected, $session['flash']);
$this->assertEquals([], $bag->getArrayCopy());
}
public function testClear()
{
$session = new \ArrayObject([
'flash' => [
['type' => 'notice', 'message' => 'one', 'contentType' => 'text/html'],
['message' => 'two']
]
]);
$bag = (new FlashBag())->withSession($session);
$bag->add('notice', 'foo');
$bag->add('warning', 'bar', 'text/html');
$bag->clear();
$this->assertArrayNotHasKey('flash', $session->getArrayCopy());
$this->assertEquals([], $bag->getArrayCopy());
}
}