Skip to content

Commit

Permalink
[10.x] Add assertJsonPathCanonicalizing method (#48117)
Browse files Browse the repository at this point in the history
* Add assertJsonPathCanonicalizing method

* Fix linting issue

* Update AssertableJsonString.php

* Update TestResponse.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
gdebrauwer and taylorotwell committed Aug 21, 2023
1 parent 66b8ac1 commit 4899c53
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Testing/AssertableJsonString.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ public function assertPath($path, $expect)
return $this;
}

/**
* Assert that the given path in the response contains all of the expected values without looking at the order.
*
* @param string $path
* @param array $expect
* @return $this
*/
public function assertPathCanonicalizing($path, $expect)
{
PHPUnit::assertEqualsCanonicalizing($expect, $this->json($path));

return $this;
}

/**
* Assert that the response has a given JSON structure.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,20 @@ public function assertJsonPath($path, $expect)
return $this;
}

/**
* Assert that the given path in the response contains all of the expected values without looking at the order.
*
* @param string $path
* @param array $expect
* @return $this
*/
public function assertJsonPathCanonicalizing($path, array $expect)
{
$this->decodeResponseJson()->assertPathCanonicalizing($path, $expect);

return $this;
}

/**
* Assert that the response has the exact given JSON.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,29 @@ public function testAssertJsonPathWithClosureCanFail()
$response->assertJsonPath('data.foo', fn ($value) => $value === null);
}

public function testAssertJsonPathCanonicalizing()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

$response->assertJsonPathCanonicalizing('*.foo', ['foo 0', 'foo 1', 'foo 2', 'foo 3']);
$response->assertJsonPathCanonicalizing('*.foo', ['foo 1', 'foo 0', 'foo 3', 'foo 2']);

$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));

$response->assertJsonPathCanonicalizing('*.id', [10, 20, 30]);
$response->assertJsonPathCanonicalizing('*.id', [30, 10, 20]);
}

public function testAssertJsonPathCanonicalizingCanFail()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that two arrays are equal.');

$response->assertJsonPathCanonicalizing('*.foo', ['foo 0', 'foo 2', 'foo 3']);
}

public function testAssertJsonFragment()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
Expand Down

0 comments on commit 4899c53

Please sign in to comment.