From 176ff65f963252f5f0953212b8b972c49e071dd5 Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Tue, 22 Apr 2025 20:23:38 +0200 Subject: [PATCH 1/4] Add Enum support for assertJsonPath in AssertableJsonString.php --- .../Testing/AssertableJsonString.php | 2 ++ tests/Testing/TestResponseTest.php | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index 0bf221bea0da..7a4f7bd034dd 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -237,6 +237,8 @@ public function assertPath($path, $expect) { if ($expect instanceof Closure) { PHPUnit::assertTrue($expect($this->json($path))); + } elseif ($expect instanceof \BackedEnum){ + PHPUnit::assertSame($expect->value, $this->json($path)); } else { PHPUnit::assertSame($expect, $this->json($path)); } diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index 9de88ddff3f3..cae497bdd133 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -1437,6 +1437,27 @@ public function testAssertJsonPathWithClosureCanFail() $response->assertJsonPath('data.foo', fn ($value) => $value === null); } + public function testAssertJsonPathWithEnum() + { + $response = TestResponse::fromBaseResponse(new Response([ + 'data' => ['status' => 'booked'], + ])); + + $response->assertJsonPath('data.status', TestStatus::Booked); + } + + public function testAssertJsonPathWithEnumCanFail() + { + $response = TestResponse::fromBaseResponse(new Response([ + 'data' => ['status' => 'failed'], + ])); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage('Failed asserting that two strings are identical.'); + + $response->assertJsonPath('data.status', TestStatus::Booked); + } + public function testAssertJsonPathCanonicalizing() { $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub)); @@ -2954,3 +2975,8 @@ class AnotherTestModel extends Model { protected $guarded = []; } + +enum TestStatus: string +{ + case Booked = 'booked'; +} From a09f5c30e5f09878d5d7dc10950e92a12c87e7e7 Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Tue, 22 Apr 2025 20:42:47 +0200 Subject: [PATCH 2/4] Fix code style in assertJsonPath --- 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 7a4f7bd034dd..75506fe606cb 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -237,7 +237,7 @@ public function assertPath($path, $expect) { if ($expect instanceof Closure) { PHPUnit::assertTrue($expect($this->json($path))); - } elseif ($expect instanceof \BackedEnum){ + } elseif ($expect instanceof \BackedEnum) { PHPUnit::assertSame($expect->value, $this->json($path)); } else { PHPUnit::assertSame($expect, $this->json($path)); From 530c6fa8e2c9b876eb858ba2f12aca3b0ccd64f7 Mon Sep 17 00:00:00 2001 From: Azim Kordpour Date: Wed, 23 Apr 2025 11:17:01 +0200 Subject: [PATCH 3/4] Use enum_value in assertPath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> --- src/Illuminate/Testing/AssertableJsonString.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index 75506fe606cb..d144e5e5d91f 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -237,10 +237,8 @@ public function assertPath($path, $expect) { if ($expect instanceof Closure) { PHPUnit::assertTrue($expect($this->json($path))); - } elseif ($expect instanceof \BackedEnum) { - PHPUnit::assertSame($expect->value, $this->json($path)); } else { - PHPUnit::assertSame($expect, $this->json($path)); + PHPUnit::assertSame(enum_value($expect), $this->json($path)); } return $this; From fa8c77bbb560339c715b64644be50327e7ae3f3c Mon Sep 17 00:00:00 2001 From: Mojtaba Date: Wed, 23 Apr 2025 11:19:26 +0200 Subject: [PATCH 4/4] Import enum_value in AssertableJsonString --- src/Illuminate/Testing/AssertableJsonString.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Testing/AssertableJsonString.php b/src/Illuminate/Testing/AssertableJsonString.php index d144e5e5d91f..d089ab071dc3 100644 --- a/src/Illuminate/Testing/AssertableJsonString.php +++ b/src/Illuminate/Testing/AssertableJsonString.php @@ -12,6 +12,8 @@ use Illuminate\Testing\Assert as PHPUnit; use JsonSerializable; +use function Illuminate\Support\enum_value; + class AssertableJsonString implements ArrayAccess, Countable { /**