diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index a05179e71b1d..10f1f2350975 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -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. * diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index 1a6ded081b29..48cdd7f0a7f6 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -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));