@@ -1022,6 +1022,105 @@ protected function responseHasView()
1022
1022
return isset ($ this ->original ) && $ this ->original instanceof View;
1023
1023
}
1024
1024
1025
+ /**
1026
+ * Assert that the given keys do not have validation errors.
1027
+ *
1028
+ * @param array|null $keys
1029
+ * @param string $errorBag
1030
+ * @param string $responseKey
1031
+ * @return $this
1032
+ */
1033
+ public function assertValid ($ keys = null , $ errorBag = 'default ' , $ responseKey = 'errors ' )
1034
+ {
1035
+ if ($ this ->baseResponse ->headers ->get ('Content-Type ' ) === 'application/json ' ) {
1036
+ return $ this ->assertJsonMissingValidationErrors ($ keys , $ responseKey );
1037
+ }
1038
+
1039
+ if ($ this ->session ()->get ('errors ' )) {
1040
+ $ errors = $ this ->session ()->get ('errors ' )->getBag ($ errorBag )->getMessages ();
1041
+ } else {
1042
+ $ errors = [];
1043
+ }
1044
+
1045
+ if (empty ($ errors )) {
1046
+ PHPUnit::assertTrue (true );
1047
+
1048
+ return $ this ;
1049
+ }
1050
+
1051
+ if (is_null ($ keys ) && count ($ errors ) > 0 ) {
1052
+ PHPUnit::fail (
1053
+ 'Response has unexpected validation errors: ' .PHP_EOL .PHP_EOL .
1054
+ json_encode ($ errors , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
1055
+ );
1056
+ }
1057
+
1058
+ foreach (Arr::wrap ($ keys ) as $ key ) {
1059
+ PHPUnit::assertFalse (
1060
+ isset ($ errors [$ key ]),
1061
+ "Found unexpected validation error for key: ' {$ key }' "
1062
+ );
1063
+ }
1064
+
1065
+ return $ this ;
1066
+ }
1067
+
1068
+ /**
1069
+ * Assert that the response has the given validation errors.
1070
+ *
1071
+ * @param array $errors
1072
+ * @param string $errorBag
1073
+ * @param string $responseKey
1074
+ * @return $this
1075
+ */
1076
+ public function assertInvalid ($ errors ,
1077
+ $ errorBag = 'default ' ,
1078
+ $ responseKey = 'errors ' )
1079
+ {
1080
+ if ($ this ->baseResponse ->headers ->get ('Content-Type ' ) === 'application/json ' ) {
1081
+ return $ this ->assertJsonValidationErrors ($ errors , $ responseKey );
1082
+ }
1083
+
1084
+ $ this ->assertSessionHas ('errors ' );
1085
+
1086
+ $ keys = (array ) $ errors ;
1087
+
1088
+ $ sessionErrors = $ this ->session ()->get ('errors ' )->getBag ($ errorBag )->getMessages ();
1089
+
1090
+ $ errorMessage = $ sessionErrors
1091
+ ? 'Response has the following validation errors in the session: ' .
1092
+ PHP_EOL .PHP_EOL .json_encode ($ sessionErrors , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ).PHP_EOL
1093
+ : 'Response does not have validation errors in the session. ' ;
1094
+
1095
+ foreach ($ errors as $ key => $ value ) {
1096
+ PHPUnit::assertArrayHasKey (
1097
+ (is_int ($ key )) ? $ value : $ key ,
1098
+ $ sessionErrors ,
1099
+ "Failed to find a validation error in session for key: ' {$ value }' " .PHP_EOL .PHP_EOL .$ errorMessage
1100
+ );
1101
+
1102
+ if (! is_int ($ key )) {
1103
+ $ hasError = false ;
1104
+
1105
+ foreach (Arr::wrap ($ sessionErrors [$ key ]) as $ sessionErrorMessage ) {
1106
+ if (Str::contains ($ sessionErrorMessage , $ value )) {
1107
+ $ hasError = true ;
1108
+
1109
+ break ;
1110
+ }
1111
+ }
1112
+
1113
+ if (! $ hasError ) {
1114
+ PHPUnit::fail (
1115
+ "Failed to find a validation error for key and message: ' $ key' => ' $ value' " .PHP_EOL .PHP_EOL .$ errorMessage
1116
+ );
1117
+ }
1118
+ }
1119
+ }
1120
+
1121
+ return $ this ;
1122
+ }
1123
+
1025
1124
/**
1026
1125
* Assert that the session has a given value.
1027
1126
*
0 commit comments