Skip to content

Commit

Permalink
more json support
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 18, 2023
1 parent 835c7e9 commit e0582b1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/behaviors/TestableResponseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,15 @@ function assertHeaderMissing(string $name) {
return $this->response;
}

function assertJson() {
// TODO
function assertJson(array $data, $strict = false) {
Assert::assertEqualsCanonicalizing($data, $this->json());

return $this->response;
}

function assertJsonCount() {
// TODO
function assertJsonCount(int $count, ?string $path=null) {
Assert::assertCount($count, data_get($this->json(), $path));

return $this->response;
}

Expand All @@ -459,8 +461,9 @@ function assertJsonFragment() {
return $this->response;
}

function assertJsonMissing() {
// TODO
function assertJsonMissing(string $path) {
Assert::assertNull(data_get($this->json(), $path));

return $this->response;
}

Expand All @@ -474,8 +477,9 @@ function assertJsonMissingValidationErrors() {
return $this->response;
}

function assertJsonPath() {
// TODO
function assertJsonPath(string $path, mixed $value) {
Assert::assertSame($value, data_get($this->json(), $path));

return $this->response;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,29 @@
->assertDownload('file.jpg');

it('asserts json')
->get('/responses/json')
->assertJson(['foo' => 'bar', 'baz' => ['qux']]);

it('asserts exact json')
->get('/responses/json')
->assertExactJson(['foo' => 'bar', 'baz' => ['qux']]);

it('asserts json path')
->get('/responses/json')
->assertJsonPath('foo', 'bar');

it('asserts json missing')
->get('/responses/json')
->assertJsonMissing('qux');

it('asserts json count')
->get('/responses/json')
->assertJsonCount(2);

it('asserts json count with path')
->get('/responses/json')
->assertJsonCount(1, 'baz');

it('asserts 403 Forbidden status code')
->get('/responses/403')
->assertForbidden();
Expand Down
1 change: 1 addition & 0 deletions tests/templates/responses/json.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% header "Content-Type: application/json; charset=UTF-8" %}
{{ {
foo: 'bar',
baz: [
Expand Down

0 comments on commit e0582b1

Please sign in to comment.