diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index bf5649753202..3739d750eca8 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -277,6 +277,26 @@ public function assertDontSeeText($value) return $this; } + /** + * Assert that the response has validation errors for the given keys. + * + * @param string|array $keys + * @return $this + */ + public function assertValidationErrors($keys) + { + $errors = $this->json()['errors']; + + foreach (array_wrap($keys) as $key) { + PHPUnit::assertTrue( + isset($errors[$key]), + "Failed to find a validation error in the response for key: '{$key}'" + ); + } + + return $this; + } + /** * Assert that the response is a superset of the given JSON. * @@ -293,6 +313,32 @@ public function assertJson(array $data, $strict = false) return $this; } + /** + * Assert that the response json has the expected count. + * + * @param int $count + * @param string|null $key + * @return $this + */ + public function assertJsonCount(int $count, $key = null) + { + if ($key) { + PHPUnit::assertCount($count, + $this->json()[$key], + "Failed to assert that the response count matched the expected {$count}" + ); + + return $this; + } + + PHPUnit::assertCount($count, + $this->json(), + "Failed to assert that the response count matched the expected {$count}" + ); + + return $this; + } + /** * Get the assertion message for assertJson. *