Skip to content

Commit

Permalink
[9.x] Add new TestResponse helper: assertJsonMissingPath (#42361)
Browse files Browse the repository at this point in the history
* add TestResponse::assertJsonMissingPath

* fix styleci
  • Loading branch information
danilopolani authored May 12, 2022
1 parent 17f1974 commit c894325
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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

0 comments on commit c894325

Please sign in to comment.