Skip to content

[12.x] Add Enum support for assertJsonPath in AssertableJsonString.php #55516

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

Merged
merged 4 commits into from
Apr 23, 2025
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
4 changes: 3 additions & 1 deletion src/Illuminate/Testing/AssertableJsonString.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Illuminate\Testing\Assert as PHPUnit;
use JsonSerializable;

use function Illuminate\Support\enum_value;

class AssertableJsonString implements ArrayAccess, Countable
{
/**
Expand Down Expand Up @@ -238,7 +240,7 @@ public function assertPath($path, $expect)
if ($expect instanceof Closure) {
PHPUnit::assertTrue($expect($this->json($path)));
} else {
PHPUnit::assertSame($expect, $this->json($path));
PHPUnit::assertSame(enum_value($expect), $this->json($path));
}

return $this;
Expand Down
26 changes: 26 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -2954,3 +2975,8 @@ class AnotherTestModel extends Model
{
protected $guarded = [];
}

enum TestStatus: string
{
case Booked = 'booked';
}