forked from jqhph/dcat-admin
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSessionMessageTest.php
More file actions
242 lines (197 loc) · 8.33 KB
/
Copy pathSessionMessageTest.php
File metadata and controls
242 lines (197 loc) · 8.33 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace Tests\Unit;
use Dcat\Admin\Support\SessionMessage;
use PHPUnit\Framework\TestCase;
class SessionMessageTest extends TestCase
{
// -----------------------------------------------------------------------
// make / getters
// -----------------------------------------------------------------------
public function test_make_returns_instance_with_correct_values(): void
{
$msg = SessionMessage::make('success', 'hello', ['timeOut' => 3000]);
$this->assertSame('success', $msg->getTitle());
$this->assertSame('hello', $msg->getMessage());
$this->assertSame(['timeOut' => 3000], $msg->getOptions());
}
public function test_make_defaults_message_and_options(): void
{
$msg = SessionMessage::make('info');
$this->assertSame('', $msg->getMessage());
$this->assertSame([], $msg->getOptions());
}
// -----------------------------------------------------------------------
// getToastrType — whitelist
// -----------------------------------------------------------------------
#[\PHPUnit\Framework\Attributes\DataProvider('validToastrTypes')]
public function test_get_toastr_type_returns_valid_type(string $type): void
{
$this->assertSame($type, SessionMessage::make($type)->getToastrType());
}
public static function validToastrTypes(): array
{
return [
['success'],
['error'],
['warning'],
['info'],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('invalidToastrTypes')]
public function test_get_toastr_type_falls_back_to_info_for_invalid_type(string $type): void
{
$this->assertSame('info', SessionMessage::make($type)->getToastrType());
}
public static function invalidToastrTypes(): array
{
return [
[''],
['alert'],
['SUCCESS'],
['<script>alert(1)</script>'],
['error; DROP TABLE'],
];
}
// -----------------------------------------------------------------------
// jsonSerialize (JSON session 序列化)
// -----------------------------------------------------------------------
public function test_json_serialize_includes_class_key_and_all_fields(): void
{
$msg = SessionMessage::make('error', 'something went wrong', ['closeButton' => true]);
$data = $msg->jsonSerialize();
$this->assertSame('Dcat\Admin\Support\SessionMessage', $data['__dcat_class']);
$this->assertSame('error', $data['title']);
$this->assertSame('something went wrong', $data['message']);
$this->assertSame(['closeButton' => true], $data['options']);
}
public function test_json_encode_roundtrip(): void
{
$msg = SessionMessage::make('warning', 'be careful', ['positionClass' => 'toast-top-right']);
$decoded = json_decode(json_encode($msg), true);
$this->assertSame('warning', $decoded['title']);
$this->assertSame('be careful', $decoded['message']);
$this->assertSame(['positionClass' => 'toast-top-right'], $decoded['options']);
}
// -----------------------------------------------------------------------
// tryFrom — PHP 序列化路径
// -----------------------------------------------------------------------
public function test_try_from_accepts_existing_instance(): void
{
$original = SessionMessage::make('success', 'done');
$result = SessionMessage::tryFrom($original);
$this->assertSame($original, $result);
}
public function test_try_from_returns_null_for_plain_object(): void
{
$this->assertNull(SessionMessage::tryFrom(new \stdClass()));
}
public function test_try_from_returns_null_for_null(): void
{
$this->assertNull(SessionMessage::tryFrom(null));
}
public function test_try_from_returns_null_for_string(): void
{
$this->assertNull(SessionMessage::tryFrom('success'));
}
// -----------------------------------------------------------------------
// tryFrom — JSON 序列化路径
// -----------------------------------------------------------------------
public function test_try_from_reconstructs_from_json_array(): void
{
$original = SessionMessage::make('info', 'hello', ['timeOut' => 5000]);
$array = json_decode(json_encode($original), true);
$restored = SessionMessage::tryFrom($array);
$this->assertNotNull($restored);
$this->assertSame('info', $restored->getTitle());
$this->assertSame('hello', $restored->getMessage());
$this->assertSame(['timeOut' => 5000], $restored->getOptions());
}
public function test_try_from_returns_null_for_array_without_class_key(): void
{
$this->assertNull(SessionMessage::tryFrom(['title' => 'info', 'message' => 'hi']));
}
public function test_try_from_returns_null_for_array_with_wrong_class_key(): void
{
$this->assertNull(SessionMessage::tryFrom([
'__dcat_class' => 'Some\Other\Class',
'title' => 'info',
'message' => 'hi',
]));
}
public function test_try_from_tolerates_missing_fields_in_json_array(): void
{
$restored = SessionMessage::tryFrom([
'__dcat_class' => 'Dcat\Admin\Support\SessionMessage',
]);
$this->assertNotNull($restored);
$this->assertSame('', $restored->getTitle());
$this->assertSame('', $restored->getMessage());
$this->assertSame([], $restored->getOptions());
}
public function test_try_from_ignores_non_string_title_and_message(): void
{
$restored = SessionMessage::tryFrom([
'__dcat_class' => 'Dcat\Admin\Support\SessionMessage',
'title' => ['injected'],
'message' => 42,
'options' => ['timeOut' => 3000],
]);
$this->assertNotNull($restored);
$this->assertSame('', $restored->getTitle());
$this->assertSame('', $restored->getMessage());
$this->assertSame(['timeOut' => 3000], $restored->getOptions());
}
public function test_try_from_ignores_non_array_options(): void
{
$restored = SessionMessage::tryFrom([
'__dcat_class' => 'Dcat\Admin\Support\SessionMessage',
'title' => 'info',
'message' => 'hello',
'options' => 'not-an-array',
]);
$this->assertNotNull($restored);
$this->assertSame('info', $restored->getTitle());
$this->assertSame('hello', $restored->getMessage());
$this->assertSame([], $restored->getOptions());
}
// -----------------------------------------------------------------------
// tryFrom — 旧契约 MessageBag 兼容
// -----------------------------------------------------------------------
public function test_try_from_accepts_legacy_message_bag_instance(): void
{
$bag = new \Illuminate\Support\MessageBag([
'title' => '操作成功',
'message' => '记录已保存',
]);
$restored = SessionMessage::tryFrom($bag);
$this->assertNotNull($restored);
$this->assertSame('操作成功', $restored->getTitle());
$this->assertSame('记录已保存', $restored->getMessage());
}
public function test_try_from_accepts_legacy_message_bag_serialized_array(): void
{
$bag = new \Illuminate\Support\MessageBag([
'title' => '操作成功',
'message' => '记录已保存',
]);
$array = json_decode(json_encode($bag), true);
$restored = SessionMessage::tryFrom($array);
$this->assertNotNull($restored);
$this->assertSame('操作成功', $restored->getTitle());
$this->assertSame('记录已保存', $restored->getMessage());
}
public function test_try_from_accepts_message_bag_with_only_title(): void
{
$restored = SessionMessage::tryFrom(['title' => ['仅标题']]);
$this->assertNotNull($restored);
$this->assertSame('仅标题', $restored->getTitle());
$this->assertSame('', $restored->getMessage());
}
public function test_try_from_rejects_message_bag_shaped_array_with_non_string_values(): void
{
$this->assertNull(SessionMessage::tryFrom([
'title' => [['nested']],
'message' => ['hi'],
]));
}
}