55namespace Longman \LaravelLodash \Testing ;
66
77use Arr ;
8- use Illuminate \Testing \Assert ;
98use Illuminate \Testing \TestResponse ;
109use PHPUnit \Framework \Assert as PHPUnit ;
1110
1211use function __ ;
12+ use function array_merge ;
1313use function json_decode ;
1414
1515class Response extends TestResponse
@@ -81,17 +81,48 @@ public function assertJsonDataItemStructure(array $data): self
8181 return $ this ;
8282 }
8383
84- public function assertJsonErrorStructure (): self
84+ public function assertJsonErrorStructure (? string $ message = null , bool $ includeMeta = false ): self
8585 {
86- $ this ->assertJsonStructure (self ::$ errorResponseStructure );
86+ $ structure = self ::$ errorResponseStructure ;
87+ if (! $ includeMeta ) {
88+ $ structure = Arr::except ($ structure , 'meta ' );
89+ }
90+ $ this ->assertJsonStructure ($ structure );
91+ $ this ->assertJson (['status ' => 'error ' ]);
92+ if ($ message ) {
93+ $ this ->assertJson (['message ' => $ message ]);
94+ }
95+
96+ return $ this ;
97+ }
98+
99+ public function assertJsonValidationErrorStructure (array $ errors = [], bool $ includeMeta = false ): self
100+ {
101+ $ structure = self ::$ errorResponseStructure ;
102+ if (! $ includeMeta ) {
103+ $ structure = Arr::except ($ structure , 'meta ' );
104+ }
105+ $ structure = array_merge ($ structure , ['errors ' ]);
106+ $ this ->assertJsonStructure ($ structure );
107+ $ this ->assertJson (['message ' => __ ('validation.error ' ), 'status ' => 'error ' ]);
108+ if ($ errors ) {
109+ $ this ->assertJsonValidationErrors ($ errors );
110+ }
87111
88112 return $ this ;
89113 }
90114
91- public function assertJsonSuccessStructure (string $ message = ' ok ' ): self
115+ public function assertJsonSuccessStructure (? string $ message = null , bool $ includeMeta = false ): self
92116 {
93- $ this ->assertJsonStructure (self ::$ successResponseStructure );
94- $ this ->assertJson (['status ' => $ message ]);
117+ $ structure = self ::$ successResponseStructure ;
118+ if (! $ includeMeta ) {
119+ $ structure = Arr::except ($ structure , 'meta ' );
120+ }
121+ $ this ->assertJsonStructure ($ structure );
122+ $ this ->assertJson (['status ' => 'ok ' ]);
123+ if ($ message ) {
124+ $ this ->assertJson (['message ' => $ message ]);
125+ }
95126
96127 return $ this ;
97128 }
@@ -117,54 +148,11 @@ public function assertNotFound(): Response
117148 {
118149 parent ::assertNotFound ();
119150
120- //$this->assertJsonErrorStructure();
121151 $ this ->assertJson (['status ' => 'error ' , 'message ' => __ ('app.item_not_found ' )]);
122152
123153 return $ this ;
124154 }
125155
126- public function assertIsInvalidItem (): Response
127- {
128- Assert::assertTrue (
129- $ this ->isInvalidData (),
130- 'Response status code [ ' . $ this ->getStatusCode () . '] is not a invalid data status code. ' ,
131- );
132-
133- return $ this ;
134- }
135-
136- public function assertInvalidData (): Response
137- {
138- Assert::assertTrue (
139- $ this ->isInvalidData (),
140- 'Response status code [ ' . $ this ->getStatusCode () . '] is not a invalid data status code. ' ,
141- );
142- $ this ->assertJsonErrorStructure ();
143-
144- return $ this ;
145- }
146-
147- public function isInvalidData (): bool
148- {
149- return $ this ->getStatusCode () === 422 ;
150- }
151-
152- public function assertIsError (): void
153- {
154- $ this ->assertJsonStructure (self ::$ errorResponseStructure );
155- $ this ->assertJson (['status ' => 'error ' ]);
156- }
157-
158- public function assertIsOk (string $ message = 'ok ' , bool $ includeMeta = false ): void
159- {
160- $ structure = self ::$ successResponseStructure ;
161- if (! $ includeMeta ) {
162- $ structure = Arr::except ($ structure , 'meta ' );
163- }
164- $ this ->assertJsonStructure ($ structure );
165- $ this ->assertJson (['status ' => $ message ]);
166- }
167-
168156 public function assertOk (): Response
169157 {
170158 parent ::assertOk ();
0 commit comments