Skip to content

Commit 584491a

Browse files
committed
add json assertions on the TestResponseHelper
Signed-off-by: Tonko Mulder <[email protected]>
1 parent 960f2f4 commit 584491a

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Tempest/Framework/Testing/Http/TestResponseHelper.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\Assert;
1010
use Tempest\Http\Cookie\CookieManager;
1111
use Tempest\Http\Response;
12+
use Tempest\Http\Responses\Invalid;
1213
use Tempest\Http\Session\Session;
1314
use Tempest\Http\Status;
1415
use Tempest\Validation\Rule;
@@ -219,9 +220,54 @@ public function assertNotSee(string $search): self
219220
return $this;
220221
}
221222

223+
public function assertJson(array $expected): self
224+
{
225+
Assert::assertNotNull($this->response->body);
226+
Assert::assertEquals($expected, $this->response->body);
227+
228+
return $this;
229+
}
230+
231+
public function assertJsonByKeys(array $expected, array $keys): self
232+
{
233+
$filteredBody = array_reduce(
234+
compact('expected'),
235+
function ($carry) use ($keys) {
236+
foreach ($keys as $key) {
237+
if (isset($this->response->body[$key])) {
238+
$carry[$key] = $this->response->body[$key];
239+
}
240+
241+
if (str_contains($key, '.') && substr_count($key, '.') === 1) {
242+
[$relation, $relatedKey] = explode('.', $key);
243+
$carry[$relation][$relatedKey] = $this->body[$relation][$relatedKey];
244+
}
245+
}
246+
247+
return $carry;
248+
},
249+
[],
250+
);
251+
252+
Assert::assertNotEmpty($filteredBody);
253+
Assert::assertEquals($expected, $filteredBody);
254+
255+
return $this;
256+
}
257+
258+
public function assertHasNoJsonValidationErrors(): self
259+
{
260+
Assert::assertNotInstanceOf(Invalid::class, $this->response);
261+
262+
return $this;
263+
}
264+
222265
public function dd(): void
223266
{
224-
// @phpstan-ignore disallowed.function
267+
/**
268+
* @noinspection ForgottenDebugOutputInspection
269+
* @phpstan-ignore disallowed.function
270+
*/
225271
dd($this->response); // @mago-expect best-practices/no-debug-symbols
226272
}
227273
}

tests/Fixtures/Controllers/ValidationController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace Tests\Tempest\Fixtures\Controllers;
66

77
use Tempest\Http\Response;
8+
use Tempest\Http\Responses\Json;
89
use Tempest\Http\Responses\Ok;
910
use Tempest\Http\Responses\Redirect;
1011
use Tempest\Router\Get;
1112
use Tempest\Router\Post;
13+
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
1214

1315
use function Tempest\uri;
1416

@@ -25,4 +27,19 @@ public function store(RequestForValidationController $request): Response // @mag
2527
{
2628
return new Redirect(uri([self::class, 'get']));
2729
}
30+
31+
#[Get(uri: '/test-validation-responses-json/{book}')]
32+
public function book(Book $book): Response
33+
{
34+
$book->load('author');
35+
36+
return new Json([
37+
'id' => $book->id->id,
38+
'title' => $book->title,
39+
'author' => [
40+
'id' => $book->author->id->id,
41+
'name' => $book->author->name,
42+
],
43+
]);
44+
}
2845
}

tests/Integration/Http/ValidationResponseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
namespace Tests\Tempest\Integration\Http;
66

7+
use Tempest\Database\Migrations\CreateMigrationsTable;
78
use Tempest\Http\Session\Session;
89
use Tests\Tempest\Fixtures\Controllers\ValidationController;
10+
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
11+
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
12+
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
13+
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
914
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1015

1116
use function Tempest\uri;
@@ -43,4 +48,23 @@ public function test_original_values(): void
4348
$this->assertEquals($values, $data);
4449
});
4550
}
51+
52+
public function test_assert_json_by_keys_assertion(): void
53+
{
54+
$this->migrate(
55+
CreateMigrationsTable::class,
56+
CreateBookTable::class,
57+
CreateAuthorTable::class,
58+
);
59+
60+
$author = Author::create(name: 'Homer');
61+
Book::create(title: 'The Odyssee', author: $author);
62+
63+
$this->http
64+
->get(uri([ValidationController::class, 'book'], book: 1))
65+
->assertHasNoJsonValidationErrors()
66+
->assertJson(['id' => 1, 'title' => 'The Odyssee', 'author' => ['id' => 1, 'name' => 'Homer']])
67+
->assertJsonByKeys(['id' => 1, 'title' => 'The Odyssee'], ['id', 'title'])
68+
->assertJsonByKeys(['author' => ['name' => 'Homer']], ['author.name']);
69+
}
4670
}

0 commit comments

Comments
 (0)