Skip to content

Commit 573684f

Browse files
[12.x] Add Enum support for assertJsonPath in AssertableJsonString.php (#55516)
* Add Enum support for assertJsonPath in AssertableJsonString.php * Fix code style in assertJsonPath * Use enum_value in assertPath Co-authored-by: Sebastian Hädrich <[email protected]> * Import enum_value in AssertableJsonString --------- Co-authored-by: Sebastian Hädrich <[email protected]>
1 parent 63b4d72 commit 573684f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Illuminate/Testing/AssertableJsonString.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Illuminate\Testing\Assert as PHPUnit;
1313
use JsonSerializable;
1414

15+
use function Illuminate\Support\enum_value;
16+
1517
class AssertableJsonString implements ArrayAccess, Countable
1618
{
1719
/**
@@ -238,7 +240,7 @@ public function assertPath($path, $expect)
238240
if ($expect instanceof Closure) {
239241
PHPUnit::assertTrue($expect($this->json($path)));
240242
} else {
241-
PHPUnit::assertSame($expect, $this->json($path));
243+
PHPUnit::assertSame(enum_value($expect), $this->json($path));
242244
}
243245

244246
return $this;

tests/Testing/TestResponseTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,27 @@ public function testAssertJsonPathWithClosureCanFail()
14371437
$response->assertJsonPath('data.foo', fn ($value) => $value === null);
14381438
}
14391439

1440+
public function testAssertJsonPathWithEnum()
1441+
{
1442+
$response = TestResponse::fromBaseResponse(new Response([
1443+
'data' => ['status' => 'booked'],
1444+
]));
1445+
1446+
$response->assertJsonPath('data.status', TestStatus::Booked);
1447+
}
1448+
1449+
public function testAssertJsonPathWithEnumCanFail()
1450+
{
1451+
$response = TestResponse::fromBaseResponse(new Response([
1452+
'data' => ['status' => 'failed'],
1453+
]));
1454+
1455+
$this->expectException(AssertionFailedError::class);
1456+
$this->expectExceptionMessage('Failed asserting that two strings are identical.');
1457+
1458+
$response->assertJsonPath('data.status', TestStatus::Booked);
1459+
}
1460+
14401461
public function testAssertJsonPathCanonicalizing()
14411462
{
14421463
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
@@ -2954,3 +2975,8 @@ class AnotherTestModel extends Model
29542975
{
29552976
protected $guarded = [];
29562977
}
2978+
2979+
enum TestStatus: string
2980+
{
2981+
case Booked = 'booked';
2982+
}

0 commit comments

Comments
 (0)