Skip to content
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

[9.x] Make assertPath() accepts Closure #41409

Merged
merged 2 commits into from Mar 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Illuminate/Testing/AssertableJsonString.php
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Testing;

use ArrayAccess;
use Closure;
use Countable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -219,7 +220,11 @@ public function assertMissingExact(array $data)
*/
public function assertPath($path, $expect)
{
PHPUnit::assertSame($expect, $this->json($path));
if ($expect instanceof Closure) {
PHPUnit::assertTrue($expect($this->json($path)));
} else {
PHPUnit::assertSame($expect, $this->json($path));
}

return $this;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Testing/TestResponseTest.php
Expand Up @@ -827,6 +827,27 @@ public function testAssertJsonPathCanFail()
$response->assertJsonPath('0.id', '10');
}

public function testAssertJsonPathWithClosure()
{
$response = TestResponse::fromBaseResponse(new Response([
'data' => ['foo' => 'bar'],
]));

$response->assertJsonPath('data.foo', fn ($value) => $value === 'bar');
}

public function testAssertJsonPathWithClosureCanFail()
{
$response = TestResponse::fromBaseResponse(new Response([
'data' => ['foo' => 'bar'],
]));

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that false is true.');

$response->assertJsonPath('data.foo', fn ($value) => $value === null);
}

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