Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add new TestResponse helper: assertJsonMissingPath #42361

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/Testing/AssertableJsonString.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public function assertMissingExact(array $data)
return $this;
}

/**
* Assert that the response does not contain the given path.
*
* @param string $path
* @return $this
*/
public function assertMissingPath($path)
{
PHPUnit::assertFalse(Arr::has($this->json(), $path));

return $this;
}

/**
* Assert that the expected value and type exists at the given path in the response.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,19 @@ public function assertJsonMissingExact(array $data)
return $this;
}

/**
* Assert that the response does not contain the given path.
*
* @param array $data
* @return $this
*/
public function assertJsonMissingPath(string $path)
{
$this->decodeResponseJson()->assertMissingPath($path);

return $this;
}

/**
* Assert that the response has a given JSON structure.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,45 @@ public function testAssertJsonMissingExactCanFail2()
$response->assertJsonMissingExact(['id' => 20, 'foo' => 'bar']);
}

public function testAssertJsonMissingPath()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));

// With simple key
$response->assertJsonMissingPath('missing');

// With nested key
$response->assertJsonMissingPath('foobar.missing');
$response->assertJsonMissingPath('numeric_keys.0');
}

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

$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));

$response->assertJsonMissingPath('foo');
}

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

$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));

$response->assertJsonMissingPath('foobar.foobar_foo');
}

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

$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));

$response->assertJsonMissingPath('numeric_keys.3');
}

public function testAssertJsonValidationErrors()
{
$data = [
Expand Down