Skip to content

Commit

Permalink
[8.x] Allow using dot syntax for $responseKey (laravel#36196)
Browse files Browse the repository at this point in the history
* Allow using dot syntax for $responseKey.

* Add tests.

* Update message.

* Formatting.

* Update src/Illuminate/Testing/TestResponse.php

Co-authored-by: Paul A. <[email protected]>

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
paulandroshchuk and taylorotwell authored Feb 10, 2021
1 parent c086fc5 commit fe72373
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,13 @@ public function assertJsonMissingValidationErrors($keys = null, $responseKey = '

$json = $this->json();

if (! array_key_exists($responseKey, $json)) {
PHPUnit::assertArrayNotHasKey($responseKey, $json);
if (! Arr::has($json, $responseKey)) {
PHPUnit::assertTrue(true);

return $this;
}

$errors = $json[$responseKey];
$errors = Arr::get($json, $responseKey, []);

if (is_null($keys) && count($errors) > 0) {
PHPUnit::fail(
Expand Down
46 changes: 46 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,27 @@ public function testAssertJsonMissingValidationErrorsCanFail2()
$response->assertJsonMissingValidationErrors('bar');
}

public function testAssertJsonMissingValidationErrorsCanFail3()
{
$this->expectException(AssertionFailedError::class);

$baseResponse = tap(new Response, function ($response) {
$response->setContent(
json_encode([
'data' => [
'errors' => [
'foo' => ['one'],
],
],
]),
);
});

$response = TestResponse::fromBaseResponse($baseResponse);

$response->assertJsonMissingValidationErrors('foo', 'data.errors');
}

public function testAssertJsonMissingValidationErrorsWithoutArgument()
{
$data = ['status' => 'ok'];
Expand Down Expand Up @@ -1109,6 +1130,31 @@ public function testAssertJsonMissingValidationErrorsCustomErrorsName()
$testResponse->assertJsonMissingValidationErrors('bar', 'data');
}

public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName1()
{
$data = [
'status' => 'ok',
'data' => [
'errors' => ['foo' => 'oops'],
],
];

$testResponse = TestResponse::fromBaseResponse(
(new Response)->setContent(json_encode($data))
);

$testResponse->assertJsonMissingValidationErrors('bar', 'data.errors');
}

public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName2()
{
$testResponse = TestResponse::fromBaseResponse(
(new Response)->setContent(json_encode([]))
);

$testResponse->assertJsonMissingValidationErrors('bar', 'data.errors');
}

public function testMacroable()
{
TestResponse::macro('foo', function () {
Expand Down

0 comments on commit fe72373

Please sign in to comment.