From 305a3dac5fdf6e0e8b842afba29b0887c8cbcde2 Mon Sep 17 00:00:00 2001 From: Artur Khasanov Date: Thu, 29 Sep 2022 14:32:30 +0300 Subject: [PATCH 1/3] Added JSON_UNESCAPED_UNICODE in json_encode to make error messages mo readable. --- .../Testing/AssertableJsonString.php | 56 ++++++++++--------- tests/Testing/TestResponseTest.php | 22 ++++++++ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index 0d3fb2fe9bec..5ffc8b109c98 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -113,11 +113,12 @@ public function assertExact(array $data) */ public function assertSimilar(array $data) { - $actual = json_encode(Arr::sortRecursive( - (array) $this->decoded - )); + $actual = json_encode( + Arr::sortRecursive((array)$this->decoded), + JSON_UNESCAPED_UNICODE + ); - PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data)), $actual); + PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data), JSON_UNESCAPED_UNICODE), $actual); return $this; } @@ -130,18 +131,19 @@ public function assertSimilar(array $data) */ public function assertFragment(array $data) { - $actual = json_encode(Arr::sortRecursive( - (array) $this->decoded - )); + $actual = json_encode( + Arr::sortRecursive((array)$this->decoded), + JSON_UNESCAPED_UNICODE + ); foreach (Arr::sortRecursive($data) as $key => $value) { $expected = $this->jsonSearchStrings($key, $value); PHPUnit::assertTrue( Str::contains($actual, $expected), - 'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL. - '['.json_encode([$key => $value]).']'.PHP_EOL.PHP_EOL. - 'within'.PHP_EOL.PHP_EOL. + 'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . + '[' . json_encode([$key => $value]) . ']' . PHP_EOL . PHP_EOL . + 'within' . PHP_EOL . PHP_EOL . "[{$actual}]." ); } @@ -162,18 +164,19 @@ public function assertMissing(array $data, $exact = false) return $this->assertMissingExact($data); } - $actual = json_encode(Arr::sortRecursive( - (array) $this->decoded - )); + $actual = json_encode( + Arr::sortRecursive((array)$this->decoded), + JSON_UNESCAPED_UNICODE + ); foreach (Arr::sortRecursive($data) as $key => $value) { $unexpected = $this->jsonSearchStrings($key, $value); PHPUnit::assertFalse( Str::contains($actual, $unexpected), - 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. - '['.json_encode([$key => $value]).']'.PHP_EOL.PHP_EOL. - 'within'.PHP_EOL.PHP_EOL. + 'Found unexpected JSON fragment: ' . PHP_EOL . PHP_EOL . + '[' . json_encode([$key => $value], JSON_UNESCAPED_UNICODE) . ']' . PHP_EOL . PHP_EOL . + 'within' . PHP_EOL . PHP_EOL . "[{$actual}]." ); } @@ -189,22 +192,23 @@ public function assertMissing(array $data, $exact = false) */ public function assertMissingExact(array $data) { - $actual = json_encode(Arr::sortRecursive( - (array) $this->decoded - )); + $actual = json_encode( + Arr::sortRecursive((array)$this->decoded), + JSON_UNESCAPED_UNICODE + ); foreach (Arr::sortRecursive($data) as $key => $value) { $unexpected = $this->jsonSearchStrings($key, $value); - if (! Str::contains($actual, $unexpected)) { + if (!Str::contains($actual, $unexpected)) { return $this; } } PHPUnit::fail( - 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. - '['.json_encode($data).']'.PHP_EOL.PHP_EOL. - 'within'.PHP_EOL.PHP_EOL. + 'Found unexpected JSON fragment: ' . PHP_EOL . PHP_EOL . + '[' . json_encode($data, JSON_UNESCAPED_UNICODE) . ']' . PHP_EOL . PHP_EOL . + 'within' . PHP_EOL . PHP_EOL . "[{$actual}]." ); @@ -322,9 +326,9 @@ protected function reorderAssocKeys(array $data) */ protected function assertJsonMessage(array $data) { - $expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - $actual = json_encode($this->decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $actual = json_encode($this->decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); return 'Unable to find JSON: '.PHP_EOL.PHP_EOL. "[{$expected}]".PHP_EOL.PHP_EOL. @@ -341,7 +345,7 @@ protected function assertJsonMessage(array $data) */ protected function jsonSearchStrings($key, $value) { - $needle = substr(json_encode([$key => $value]), 1, -1); + $needle = substr(json_encode([$key => $value], JSON_UNESCAPED_UNICODE), 1, -1); return [ $needle.']', diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 0c6ccd9ac06c..d1cbf0183ba9 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -865,6 +865,16 @@ public function testAssertJsonFragmentCanFail() $response->assertJsonFragment(['id' => 1]); } + public function testAssertJsonFragmentUnicodeCanFail() + { + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessageMatches('/Привет|Мир/'); + + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithUnicodeStub)); + + $response->assertJsonFragment(['id' => 1]); + } + public function testAssertJsonStructure() { $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); @@ -1951,6 +1961,18 @@ public function jsonSerialize(): array } } +class JsonSerializableSingleResourceWithUnicodeStub implements JsonSerializable +{ + public function jsonSerialize(): array + { + return [ + ['id' => 10, 'foo' => 'bar'], + ['id' => 20, 'foo' => 'Привет'], + ['id' => 30, 'foo' => 'Мир'], + ]; + } +} + class TestModel extends Model { protected $guarded = []; From 3bd9ceb7ca2cf5884fb2b5e26a55a424f4dd56b6 Mon Sep 17 00:00:00 2001 From: Artur Khasanov Date: Fri, 30 Sep 2022 14:55:17 +0300 Subject: [PATCH 2/3] Removed redundant spaces --- .../Testing/AssertableJsonString.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index 5ffc8b109c98..a8b30b4457f6 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -114,7 +114,7 @@ public function assertExact(array $data) public function assertSimilar(array $data) { $actual = json_encode( - Arr::sortRecursive((array)$this->decoded), + Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); @@ -132,7 +132,7 @@ public function assertSimilar(array $data) public function assertFragment(array $data) { $actual = json_encode( - Arr::sortRecursive((array)$this->decoded), + Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); @@ -141,9 +141,9 @@ public function assertFragment(array $data) PHPUnit::assertTrue( Str::contains($actual, $expected), - 'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . - '[' . json_encode([$key => $value]) . ']' . PHP_EOL . PHP_EOL . - 'within' . PHP_EOL . PHP_EOL . + 'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL. + '['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. + 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); } @@ -165,7 +165,7 @@ public function assertMissing(array $data, $exact = false) } $actual = json_encode( - Arr::sortRecursive((array)$this->decoded), + Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); @@ -174,9 +174,9 @@ public function assertMissing(array $data, $exact = false) PHPUnit::assertFalse( Str::contains($actual, $unexpected), - 'Found unexpected JSON fragment: ' . PHP_EOL . PHP_EOL . - '[' . json_encode([$key => $value], JSON_UNESCAPED_UNICODE) . ']' . PHP_EOL . PHP_EOL . - 'within' . PHP_EOL . PHP_EOL . + 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. + '['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. + 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); } @@ -193,22 +193,22 @@ public function assertMissing(array $data, $exact = false) public function assertMissingExact(array $data) { $actual = json_encode( - Arr::sortRecursive((array)$this->decoded), + Arr::sortRecursive((array) $this->decoded), JSON_UNESCAPED_UNICODE ); foreach (Arr::sortRecursive($data) as $key => $value) { $unexpected = $this->jsonSearchStrings($key, $value); - if (!Str::contains($actual, $unexpected)) { + if (! Str::contains($actual, $unexpected)) { return $this; } } PHPUnit::fail( - 'Found unexpected JSON fragment: ' . PHP_EOL . PHP_EOL . - '[' . json_encode($data, JSON_UNESCAPED_UNICODE) . ']' . PHP_EOL . PHP_EOL . - 'within' . PHP_EOL . PHP_EOL . + 'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL. + '['.json_encode($data, JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL. + 'within'.PHP_EOL.PHP_EOL. "[{$actual}]." ); From bfa672e0e66b675ac0309a6915a4fb51f2eb401c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 4 Oct 2022 11:43:25 -0500 Subject: [PATCH 3/3] formatting --- src/Illuminate/Testing/AssertableJsonString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index a8b30b4457f6..6d40d50b17bb 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -345,7 +345,7 @@ protected function assertJsonMessage(array $data) */ protected function jsonSearchStrings($key, $value) { - $needle = substr(json_encode([$key => $value], JSON_UNESCAPED_UNICODE), 1, -1); + $needle = Str::substr(json_encode([$key => $value], JSON_UNESCAPED_UNICODE), 1, -1); return [ $needle.']',