Skip to content

Commit c73b5b8

Browse files
authored
Merge pull request #28787 from ahinkle/assertJsonValidationError
[5.8] Add message value assertion to assertJsonValidationErrors
2 parents 7ac9663 + 44bec6c commit c73b5b8

2 files changed

Lines changed: 138 additions & 12 deletions

File tree

src/Illuminate/Foundation/Testing/TestResponse.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -631,30 +631,38 @@ public function assertJsonCount(int $count, $key = null)
631631
}
632632

633633
/**
634-
* Assert that the response has the given JSON validation errors for the given keys.
634+
* Assert that the response has the given JSON validation errors.
635635
*
636-
* @param string|array $keys
636+
* @param string|array $errors
637637
* @return $this
638638
*/
639-
public function assertJsonValidationErrors($keys)
639+
public function assertJsonValidationErrors($errors)
640640
{
641-
$keys = Arr::wrap($keys);
641+
$errors = Arr::wrap($errors);
642642

643-
PHPUnit::assertNotEmpty($keys, 'No keys were provided.');
643+
PHPUnit::assertNotEmpty($errors, 'No validation errors were provided.');
644644

645-
$errors = $this->json()['errors'] ?? [];
645+
$jsonErrors = $this->json()['errors'] ?? [];
646646

647-
$errorMessage = $errors
647+
$errorMessage = $jsonErrors
648648
? 'Response has the following JSON validation errors:'.
649-
PHP_EOL.PHP_EOL.json_encode($errors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL
649+
PHP_EOL.PHP_EOL.json_encode($jsonErrors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE).PHP_EOL
650650
: 'Response does not have JSON validation errors.';
651651

652-
foreach ($keys as $key) {
652+
foreach ($errors as $key => $value) {
653653
PHPUnit::assertArrayHasKey(
654-
$key,
655-
$errors,
656-
"Failed to find a validation error in the response for key: '{$key}'".PHP_EOL.PHP_EOL.$errorMessage
654+
(is_int($key)) ? $value : $key,
655+
$jsonErrors,
656+
"Failed to find a validation error in the response for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
657657
);
658+
659+
if (! is_int($key)) {
660+
PHPUnit::assertStringContainsString(
661+
$value,
662+
$jsonErrors[$key],
663+
"Failed to find a validation error in the response for key and message: '$key' => '$value'".PHP_EOL.PHP_EOL.$errorMessage
664+
);
665+
}
658666
}
659667

660668
return $this;

tests/Foundation/FoundationTestResponseTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,124 @@ public function testAssertJsonValidationErrorsFailsWhenGivenAnEmptyArray()
355355
$testResponse->assertJsonValidationErrors([]);
356356
}
357357

358+
public function testAssertJsonValidationErrorsWithArray()
359+
{
360+
$data = [
361+
'status' => 'ok',
362+
'errors' => ['foo' => 'one', 'bar' => 'two'],
363+
];
364+
365+
$testResponse = TestResponse::fromBaseResponse(
366+
(new Response)->setContent(json_encode($data))
367+
);
368+
369+
$testResponse->assertJsonValidationErrors(['foo', 'bar']);
370+
}
371+
372+
public function testAssertJsonValidationErrorMessages()
373+
{
374+
$data = [
375+
'status' => 'ok',
376+
'errors' => ['key' => 'foo'],
377+
];
378+
379+
$testResponse = TestResponse::fromBaseResponse(
380+
(new Response)->setContent(json_encode($data))
381+
);
382+
383+
$testResponse->assertJsonValidationErrors(['key' => 'foo']);
384+
}
385+
386+
public function testAssertJsonValidationErrorContainsMessages()
387+
{
388+
$data = [
389+
'status' => 'ok',
390+
'errors' => ['key' => 'foo bar'],
391+
];
392+
393+
$testResponse = TestResponse::fromBaseResponse(
394+
(new Response)->setContent(json_encode($data))
395+
);
396+
397+
$testResponse->assertJsonValidationErrors(['key' => 'foo']);
398+
}
399+
400+
public function testAssertJsonValidationErrorMessagesCanFail()
401+
{
402+
$this->expectException(AssertionFailedError::class);
403+
404+
$data = [
405+
'status' => 'ok',
406+
'errors' => ['key' => 'foo'],
407+
];
408+
409+
$testResponse = TestResponse::fromBaseResponse(
410+
(new Response)->setContent(json_encode($data))
411+
);
412+
413+
$testResponse->assertJsonValidationErrors(['key' => 'bar']);
414+
}
415+
416+
public function testAssertJsonValidationErrorMessageKeyCanFail()
417+
{
418+
$this->expectException(AssertionFailedError::class);
419+
420+
$data = [
421+
'status' => 'ok',
422+
'errors' => ['foo' => 'value'],
423+
];
424+
425+
$testResponse = TestResponse::fromBaseResponse(
426+
(new Response)->setContent(json_encode($data))
427+
);
428+
429+
$testResponse->assertJsonValidationErrors(['bar' => 'value']);
430+
}
431+
432+
public function testAssertJsonValidationErrorMessagesMultipleMessages()
433+
{
434+
$data = [
435+
'status' => 'ok',
436+
'errors' => ['one' => 'foo', 'two' => 'bar'],
437+
];
438+
439+
$testResponse = TestResponse::fromBaseResponse(
440+
(new Response)->setContent(json_encode($data))
441+
);
442+
443+
$testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']);
444+
}
445+
446+
public function testAssertJsonValidationErrorMessagesMixed()
447+
{
448+
$data = [
449+
'status' => 'ok',
450+
'errors' => ['one' => 'foo', 'two' => 'bar'],
451+
];
452+
453+
$testResponse = TestResponse::fromBaseResponse(
454+
(new Response)->setContent(json_encode($data))
455+
);
456+
457+
$testResponse->assertJsonValidationErrors(['one' => 'foo', 'two']);
458+
}
459+
460+
public function testAssertJsonValidationErrorMessagesMixedCanFail()
461+
{
462+
$this->expectException(AssertionFailedError::class);
463+
464+
$data = [
465+
'status' => 'ok',
466+
'errors' => ['one' => 'foo', 'two' => 'bar'],
467+
];
468+
469+
$testResponse = TestResponse::fromBaseResponse(
470+
(new Response)->setContent(json_encode($data))
471+
);
472+
473+
$testResponse->assertJsonValidationErrors(['one' => 'taylor', 'otwell']);
474+
}
475+
358476
public function testAssertJsonMissingValidationErrors()
359477
{
360478
$baseResponse = tap(new Response, function ($response) {

0 commit comments

Comments
 (0)