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

[6.x] Adds assertJsonPath to TestResponse #29957

Merged
merged 3 commits into from
Sep 13, 2019
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
14 changes: 14 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,20 @@ protected function assertJsonMessage(array $data)
"[{$actual}].".PHP_EOL.PHP_EOL;
}

/**
* Assert that the expected value exists at the given path in the response.
*
* @param string $path
* @param mixed $expect
* @return $this
*/
public function assertJsonPath($path, $expect)
{
PHPUnit::assertEquals($expect, $this->json($path));

return $this;
}

/**
* Assert that the response has the exact given JSON.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,39 @@ public function testAssertJsonWithMixed()
$response->assertExactJson($resource->jsonSerialize());
}

public function testAssertJsonPath()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

$response->assertJsonPath('0.foo', 'foo 0');

$response->assertJsonPath('0.foo', 'foo 0');
$response->assertJsonPath('0.bar', 'bar 0');
$response->assertJsonPath('0.foobar', 'foobar 0');

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

$response->assertJsonPath('foo', 'bar');

$response->assertJsonPath('foobar.foobar_foo', 'foo');
$response->assertJsonPath('foobar.foobar_bar', 'bar');

$response->assertJsonPath('foobar.foobar_foo', 'foo')->assertJsonPath('foobar.foobar_bar', 'bar');

$response->assertJsonPath('bars', [
['foo' => 'bar 0', 'bar' => 'foo 0'],
['foo' => 'bar 1', 'bar' => 'foo 1'],
['foo' => 'bar 2', 'bar' => 'foo 2'],
]);
$response->assertJsonPath('bars.0', ['foo' => 'bar 0', 'bar' => 'foo 0']);

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

$response->assertJsonPath('0.id', 10);
$response->assertJsonPath('1.id', 20);
$response->assertJsonPath('2.id', 30);
}

public function testAssertJsonFragment()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
Expand Down