8
8
use Illuminate \Http \Request ;
9
9
use Illuminate \Support \MessageBag ;
10
10
use Illuminate \Support \Facades \App ;
11
+ use Illuminate \Support \ViewErrorBag ;
11
12
use Illuminate \Contracts \Http \Kernel ;
12
13
use Illuminate \Support \Facades \Blade ;
13
14
use Illuminate \Support \Facades \Route ;
@@ -56,7 +57,15 @@ public function test_middleware_is_registered()
56
57
57
58
public function test_validation_errors_are_registered ()
58
59
{
59
- $ this ->assertTrue (Inertia::getShared ('errors ' ) instanceof Closure);
60
+ $ this ->assertInstanceOf (Closure::class, Inertia::getShared ('errors ' ));
61
+ }
62
+
63
+ public function test_validation_errors_can_be_empty ()
64
+ {
65
+ $ errors = Inertia::getShared ('errors ' )();
66
+
67
+ $ this ->assertIsObject ($ errors );
68
+ $ this ->assertEmpty (get_object_vars ($ errors ));
60
69
}
61
70
62
71
public function test_validation_errors_are_not_registered_when_already_registered ()
@@ -66,17 +75,68 @@ public function test_validation_errors_are_not_registered_when_already_registere
66
75
$ this ->assertSame ('This is a validation error ' , Inertia::getShared ('errors ' ));
67
76
}
68
77
69
- public function test_validation_errors_are_returned_in_the_correct_format ()
78
+ public function test_validation_errors_can_be_an_array ()
79
+ {
80
+ Session::put ('errors ' , [
81
+ 'name ' => 'The name field is required. ' ,
82
+ 'email ' => 'The email must be a valid email address. ' ,
83
+ ]);
84
+
85
+ $ errors = Inertia::getShared ('errors ' )();
86
+
87
+ $ this ->assertIsObject ($ errors );
88
+ $ this ->assertSame ('The name field is required. ' , $ errors ->name );
89
+ $ this ->assertSame ('The email must be a valid email address. ' , $ errors ->email );
90
+ }
91
+
92
+ public function test_validation_exceptions_can_be_a_message_bag ()
70
93
{
71
94
Session::put ('errors ' , new MessageBag ([
72
95
'name ' => 'The name field is required. ' ,
73
- 'email ' => 'Not a valid email address ' ,
96
+ 'email ' => 'The email must be a valid email address. ' ,
74
97
]));
75
98
76
99
$ errors = Inertia::getShared ('errors ' )();
77
100
78
101
$ this ->assertIsObject ($ errors );
79
102
$ this ->assertSame ('The name field is required. ' , $ errors ->name );
80
- $ this ->assertSame ('Not a valid email address ' , $ errors ->email );
103
+ $ this ->assertSame ('The email must be a valid email address. ' , $ errors ->email );
104
+ }
105
+
106
+ public function test_validation_exceptions_can_be_an_error_bag ()
107
+ {
108
+ Session::put ('errors ' , (new ViewErrorBag ())->put ('default ' , new MessageBag ([
109
+ 'name ' => 'The name field is required. ' ,
110
+ 'email ' => 'The email must be a valid email address. ' ,
111
+ ])));
112
+
113
+ $ errors = Inertia::getShared ('errors ' )();
114
+
115
+ $ this ->assertIsObject ($ errors );
116
+ $ this ->assertSame ('The name field is required. ' , $ errors ->name );
117
+ $ this ->assertSame ('The email must be a valid email address. ' , $ errors ->email );
118
+ }
119
+
120
+ public function test_validation_exceptions_can_be_multiple_error_bags ()
121
+ {
122
+ Session::put ('errors ' , tap (new ViewErrorBag (), function ($ errorBags ) {
123
+ $ errorBags ->put ('default ' , new MessageBag (['name ' => 'The name field is required. ' ]));
124
+ $ errorBags ->put ('example ' , new MessageBag (['email ' => 'The email must be a valid email address. ' ]));
125
+ }));
126
+
127
+ $ errors = Inertia::getShared ('errors ' )();
128
+
129
+ $ this ->assertIsObject ($ errors );
130
+ $ this ->assertSame ('The name field is required. ' , $ errors ->default ['name ' ]);
131
+ $ this ->assertSame ('The email must be a valid email address. ' , $ errors ->example ['email ' ]);
132
+ }
133
+
134
+ public function test_validation_exceptions_will_be_empty_when_an_invalid_value_was_set_to_the_session ()
135
+ {
136
+ Session::put ('errors ' , new Request ());
137
+ $ errors = Inertia::getShared ('errors ' )();
138
+
139
+ $ this ->assertIsObject ($ errors );
140
+ $ this ->assertEmpty (get_object_vars ($ errors ));
81
141
}
82
142
}
0 commit comments