|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Flash; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Yiisoft\Yii\Web\Flash; |
|
7
|
|
|
|
|
8
|
|
|
class FlashTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var MockArraySessionStorage |
|
12
|
|
|
*/ |
|
13
|
|
|
private $session; |
|
14
|
|
|
|
|
15
|
|
|
protected function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
parent::setUp(); |
|
18
|
|
|
$this->session = new MockArraySessionStorage([ |
|
19
|
|
|
'__flash' => [ |
|
20
|
|
|
'__counters' => [ |
|
21
|
|
|
'info' => 0, |
|
22
|
|
|
'error' => 0, |
|
23
|
|
|
], |
|
24
|
|
|
'info' => 'Some message to show', |
|
25
|
|
|
'error' => 'Some error message to show', |
|
26
|
|
|
], |
|
27
|
|
|
]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testRemove(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$flash = new Flash($this->session); |
|
33
|
|
|
|
|
34
|
|
|
$flash->remove('error'); |
|
35
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
36
|
|
|
$this->assertSame(['__counters' => ['info' => 1], 'info' => 'Some message to show'], $rawFlashes); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testRemoveAll(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$flash = new Flash($this->session); |
|
42
|
|
|
|
|
43
|
|
|
$flash->removeAll(); |
|
44
|
|
|
|
|
45
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
46
|
|
|
$this->assertSame(['__counters' => []], $rawFlashes); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testHas(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$flash = new Flash($this->session); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertTrue($flash->has('error')); |
|
54
|
|
|
$this->assertFalse($flash->has('nope')); |
|
55
|
|
|
|
|
56
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
57
|
|
|
$this->assertSame([ |
|
58
|
|
|
'__counters' => [ |
|
59
|
|
|
'info' => 1, |
|
60
|
|
|
'error' => 1, |
|
61
|
|
|
], |
|
62
|
|
|
'info' => 'Some message to show', |
|
63
|
|
|
'error' => 'Some error message to show', |
|
64
|
|
|
], $rawFlashes); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testGet(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$flash = new Flash($this->session); |
|
70
|
|
|
|
|
71
|
|
|
$value = $flash->get('error'); |
|
72
|
|
|
$this->assertSame('Some error message to show', $value); |
|
73
|
|
|
|
|
74
|
|
|
$value = $flash->get('nope'); |
|
75
|
|
|
$this->assertNull($value); |
|
76
|
|
|
|
|
77
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
78
|
|
|
$this->assertSame([ |
|
79
|
|
|
'__counters' => [ |
|
80
|
|
|
'info' => 1, |
|
81
|
|
|
'error' => 1, |
|
82
|
|
|
], |
|
83
|
|
|
'info' => 'Some message to show', |
|
84
|
|
|
'error' => 'Some error message to show', |
|
85
|
|
|
], $rawFlashes); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testAdd(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$flash = new Flash($this->session); |
|
91
|
|
|
|
|
92
|
|
|
$flash->add('info', 'One another message', false); |
|
93
|
|
|
|
|
94
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
95
|
|
|
$this->assertSame([ |
|
96
|
|
|
'__counters' => [ |
|
97
|
|
|
'info' => 0, |
|
98
|
|
|
'error' => 1, |
|
99
|
|
|
], |
|
100
|
|
|
'info' => [ |
|
101
|
|
|
'Some message to show', |
|
102
|
|
|
'One another message', |
|
103
|
|
|
], |
|
104
|
|
|
'error' => 'Some error message to show', |
|
105
|
|
|
], $rawFlashes); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testSet(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$flash = new Flash($this->session); |
|
111
|
|
|
|
|
112
|
|
|
$flash->set('warn', 'Warning message'); |
|
113
|
|
|
|
|
114
|
|
|
$rawFlashes = $this->session->get('__flash'); |
|
115
|
|
|
$this->assertSame([ |
|
116
|
|
|
'__counters' => [ |
|
117
|
|
|
'info' => 1, |
|
118
|
|
|
'error' => 1, |
|
119
|
|
|
'warn' => -1, |
|
120
|
|
|
], |
|
121
|
|
|
'info' => 'Some message to show', |
|
122
|
|
|
'error' => 'Some error message to show', |
|
123
|
|
|
'warn' => 'Warning message', |
|
124
|
|
|
], $rawFlashes); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testGetAll(): void |
|
128
|
|
|
{ |
|
129
|
|
|
$flash = new Flash($this->session); |
|
130
|
|
|
|
|
131
|
|
|
$flashes = $flash->getAll(); |
|
132
|
|
|
$this->assertSame([ |
|
133
|
|
|
'info' => 'Some message to show', |
|
134
|
|
|
'error' => 'Some error message to show', |
|
135
|
|
|
], $flashes); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|