Skip to content

Commit 4899c53

Browse files
[10.x] Add assertJsonPathCanonicalizing method (#48117)
* Add assertJsonPathCanonicalizing method * Fix linting issue * Update AssertableJsonString.php * Update TestResponse.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 66b8ac1 commit 4899c53

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Illuminate/Testing/AssertableJsonString.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ public function assertPath($path, $expect)
246246
return $this;
247247
}
248248

249+
/**
250+
* Assert that the given path in the response contains all of the expected values without looking at the order.
251+
*
252+
* @param string $path
253+
* @param array $expect
254+
* @return $this
255+
*/
256+
public function assertPathCanonicalizing($path, $expect)
257+
{
258+
PHPUnit::assertEqualsCanonicalizing($expect, $this->json($path));
259+
260+
return $this;
261+
}
262+
249263
/**
250264
* Assert that the response has a given JSON structure.
251265
*

src/Illuminate/Testing/TestResponse.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,20 @@ public function assertJsonPath($path, $expect)
662662
return $this;
663663
}
664664

665+
/**
666+
* Assert that the given path in the response contains all of the expected values without looking at the order.
667+
*
668+
* @param string $path
669+
* @param array $expect
670+
* @return $this
671+
*/
672+
public function assertJsonPathCanonicalizing($path, array $expect)
673+
{
674+
$this->decodeResponseJson()->assertPathCanonicalizing($path, $expect);
675+
676+
return $this;
677+
}
678+
665679
/**
666680
* Assert that the response has the exact given JSON.
667681
*

tests/Testing/TestResponseTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,29 @@ public function testAssertJsonPathWithClosureCanFail()
12191219
$response->assertJsonPath('data.foo', fn ($value) => $value === null);
12201220
}
12211221

1222+
public function testAssertJsonPathCanonicalizing()
1223+
{
1224+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
1225+
1226+
$response->assertJsonPathCanonicalizing('*.foo', ['foo 0', 'foo 1', 'foo 2', 'foo 3']);
1227+
$response->assertJsonPathCanonicalizing('*.foo', ['foo 1', 'foo 0', 'foo 3', 'foo 2']);
1228+
1229+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
1230+
1231+
$response->assertJsonPathCanonicalizing('*.id', [10, 20, 30]);
1232+
$response->assertJsonPathCanonicalizing('*.id', [30, 10, 20]);
1233+
}
1234+
1235+
public function testAssertJsonPathCanonicalizingCanFail()
1236+
{
1237+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
1238+
1239+
$this->expectException(AssertionFailedError::class);
1240+
$this->expectExceptionMessage('Failed asserting that two arrays are equal.');
1241+
1242+
$response->assertJsonPathCanonicalizing('*.foo', ['foo 0', 'foo 2', 'foo 3']);
1243+
}
1244+
12221245
public function testAssertJsonFragment()
12231246
{
12241247
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

0 commit comments

Comments
 (0)