Skip to content

Commit cbc7b9f

Browse files
committed
feat: add Swal::success(), Swal::error(), Swal::toast()
1 parent aec6047 commit cbc7b9f

File tree

2 files changed

+121
-4
lines changed

2 files changed

+121
-4
lines changed

src/Swal.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,56 @@
44

55
class Swal
66
{
7-
/**
8-
* @param array{title?: string, html?: string, icon?: string, confirmButtonText?: string} $options
9-
*/
107
public static function fire(array $options = []): void
118
{
129
session()->flash('sweetalert2', $options);
1310
}
11+
12+
public static function success(array $options = []): void
13+
{
14+
session()->flash('sweetalert2', [
15+
'icon' => 'success',
16+
...$options,
17+
]);
18+
}
19+
20+
public static function error(array $options = []): void
21+
{
22+
session()->flash('sweetalert2', [
23+
'icon' => 'error',
24+
...$options,
25+
]);
26+
}
27+
28+
public static function warning(array $options = []): void
29+
{
30+
session()->flash('sweetalert2', [
31+
'icon' => 'warning',
32+
...$options,
33+
]);
34+
}
35+
36+
public static function info(array $options = []): void
37+
{
38+
session()->flash('sweetalert2', [
39+
'icon' => 'info',
40+
...$options,
41+
]);
42+
}
43+
44+
public static function question(array $options = []): void
45+
{
46+
session()->flash('sweetalert2', [
47+
'icon' => 'question',
48+
...$options,
49+
]);
50+
}
51+
52+
public static function toast(array $options = []): void
53+
{
54+
session()->flash('sweetalert2', [
55+
'toast' => true,
56+
...$options,
57+
]);
58+
}
1459
}

tests/SwalTest.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use SweetAlert2\Laravel\Swal;
44

5-
test('Swal::fire() should work', function () {
5+
test('Swal::fire()', function () {
66
Swal::fire([
77
'title' => 'SweetAlert2 + Laravel = <3',
88
'text' => 'This is a simple alert using SweetAlert2',
@@ -17,3 +17,75 @@
1717
->assertSee("import Swal from 'https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js'", escape: false)
1818
->assertSee('Swal.fire({"title":"SweetAlert2 + Laravel = \u003C3","text":"This is a simple alert using SweetAlert2","icon":"success","confirmButtonText":"Cool"})', escape: false);
1919
});
20+
21+
test('Swal::success()', function () {
22+
Swal::success([
23+
'title' => 'success title',
24+
]);
25+
26+
$response = $this->get('/');
27+
28+
$response
29+
->assertStatus(200)
30+
->assertSee('Swal.fire({"icon":"success","title":"success title"})', escape: false);
31+
});
32+
33+
test('Swal::error()', function () {
34+
Swal::error([
35+
'title' => 'error title',
36+
]);
37+
38+
$response = $this->get('/');
39+
40+
$response
41+
->assertStatus(200)
42+
->assertSee('Swal.fire({"icon":"error","title":"error title"})', escape: false);
43+
});
44+
45+
test('Swal::warning()', function () {
46+
Swal::warning([
47+
'title' => 'warning title',
48+
]);
49+
50+
$response = $this->get('/');
51+
52+
$response
53+
->assertStatus(200)
54+
->assertSee('Swal.fire({"icon":"warning","title":"warning title"})', escape: false);
55+
});
56+
57+
test('Swal::info()', function () {
58+
Swal::info([
59+
'title' => 'info title',
60+
]);
61+
62+
$response = $this->get('/');
63+
64+
$response
65+
->assertStatus(200)
66+
->assertSee('Swal.fire({"icon":"info","title":"info title"})', escape: false);
67+
});
68+
69+
test('Swal::question()', function () {
70+
Swal::question([
71+
'title' => 'question title',
72+
]);
73+
74+
$response = $this->get('/');
75+
76+
$response
77+
->assertStatus(200)
78+
->assertSee('Swal.fire({"icon":"question","title":"question title"})', escape: false);
79+
});
80+
81+
test('Swal::toast()', function () {
82+
Swal::toast([
83+
'title' => 'toast title',
84+
]);
85+
86+
$response = $this->get('/');
87+
88+
$response
89+
->assertStatus(200)
90+
->assertSee('Swal.fire({"toast":true,"title":"toast title"})', escape: false);
91+
});

0 commit comments

Comments
 (0)