From abd94c9e051d64c0a2223bbd25746dc11c8bd606 Mon Sep 17 00:00:00 2001 From: Markus Lilienberg Date: Tue, 1 Oct 2019 14:50:46 +0200 Subject: [PATCH] Strict parameter added to assertJsonPath (#30142) --- .../Foundation/Testing/TestResponse.php | 9 +++++++-- .../Foundation/FoundationTestResponseTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 6d3f985c116..92f4bb25d04 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -484,11 +484,16 @@ protected function assertJsonMessage(array $data) * * @param string $path * @param mixed $expect + * @param bool $strict * @return $this */ - public function assertJsonPath($path, $expect) + public function assertJsonPath($path, $expect, $strict = false) { - PHPUnit::assertEquals($expect, $this->json($path)); + if ($strict) { + PHPUnit::assertSame($expect, $this->json($path)); + } else { + PHPUnit::assertEquals($expect, $this->json($path)); + } return $this; } diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index bf9df7ea47a..2d00e4fdfda 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -349,6 +349,25 @@ public function testAssertJsonPath() $response->assertJsonPath('2.id', 30); } + public function testAssertJsonPathStrict() + { + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub)); + + $response->assertJsonPath('0.id', 10, true); + $response->assertJsonPath('1.id', 20, true); + $response->assertJsonPath('2.id', 30, true); + } + + public function testAssertJsonPathStrictCanFail() + { + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that 10 is identical to \'10\'.'); + + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub)); + + $response->assertJsonPath('0.id', '10', true); + } + public function testAssertJsonFragment() { $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));