Skip to content

Commit d678a2e

Browse files
committed
Properly respond with meta information added to Context instance
1 parent 540d82b commit d678a2e

File tree

8 files changed

+97
-17
lines changed

8 files changed

+97
-17
lines changed

src/Endpoint/Concerns/BuildsMeta.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tobyz\JsonApiServer\Endpoint\Concerns;
13+
14+
use JsonApiPhp\JsonApi\Meta;
15+
use Tobyz\JsonApiServer\Context;
16+
17+
trait BuildsMeta
18+
{
19+
private function buildMeta(Context $context): array
20+
{
21+
$meta = [];
22+
23+
foreach ($context->getMeta() as $item) {
24+
$meta[] = new Meta($item->getName(), $item->getValue()($context));
25+
}
26+
27+
return $meta;
28+
}
29+
}

src/Endpoint/Create.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Http\Message\ResponseInterface;
1515
use Tobyz\JsonApiServer\Context;
16+
use Tobyz\JsonApiServer\Endpoint\Concerns\BuildsMeta;
17+
use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData;
1618
use Tobyz\JsonApiServer\Exception\ForbiddenException;
1719
use Tobyz\JsonApiServer\ResourceType;
1820

@@ -23,7 +25,7 @@
2325

2426
class Create
2527
{
26-
use Concerns\SavesData;
28+
use SavesData;
2729

2830
/**
2931
* @throws ForbiddenException if the resource is not creatable.

src/Endpoint/Delete.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111

1212
namespace Tobyz\JsonApiServer\Endpoint;
1313

14+
use JsonApiPhp\JsonApi\Meta;
15+
use JsonApiPhp\JsonApi\MetaDocument;
1416
use Nyholm\Psr7\Response;
1517
use Psr\Http\Message\ResponseInterface;
1618
use Tobyz\JsonApiServer\Context;
19+
use Tobyz\JsonApiServer\Endpoint\Concerns\BuildsMeta;
1720
use Tobyz\JsonApiServer\Exception\ForbiddenException;
1821
use Tobyz\JsonApiServer\ResourceType;
1922

2023
use function Tobyz\JsonApiServer\evaluate;
24+
use function Tobyz\JsonApiServer\json_api_response;
2125
use function Tobyz\JsonApiServer\run_callbacks;
2226

2327
class Delete
2428
{
29+
use BuildsMeta;
30+
2531
/**
2632
* @throws ForbiddenException if the resource is not deletable.
2733
*/
@@ -47,6 +53,12 @@ public function handle(Context $context, ResourceType $resourceType, $model): Re
4753

4854
run_callbacks($schema->getListeners('deleted'), [&$model, $context]);
4955

56+
if (count($meta = $this->buildMeta($context))) {
57+
return json_api_response(
58+
new MetaDocument(...$meta)
59+
);
60+
}
61+
5062
return new Response(204);
5163
}
5264
}

src/Endpoint/Index.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Psr\Http\Message\ResponseInterface;
1919
use Psr\Http\Message\ServerRequestInterface as Request;
2020
use Tobyz\JsonApiServer\Context;
21+
use Tobyz\JsonApiServer\Endpoint\Concerns\BuildsMeta;
22+
use Tobyz\JsonApiServer\Endpoint\Concerns\IncludesData;
2123
use Tobyz\JsonApiServer\Exception\BadRequestException;
2224
use Tobyz\JsonApiServer\Exception\ForbiddenException;
2325
use Tobyz\JsonApiServer\ResourceType;
@@ -29,7 +31,8 @@
2931

3032
class Index
3133
{
32-
use Concerns\IncludesData;
34+
use IncludesData;
35+
use BuildsMeta;
3336

3437
/**
3538
* Handle a request to show a resource listing.
@@ -99,9 +102,7 @@ public function handle(Context $context, ResourceType $resourceType): ResponseIn
99102
$meta[] = new Structure\Meta('total', $total);
100103
}
101104

102-
foreach ($context->getMeta() as $item) {
103-
$meta[] = new Structure\Meta($item->getName(), $item->getValue()($context));
104-
}
105+
$meta = array_merge($meta, $this->buildMeta($context));
105106

106107
return json_api_response(
107108
new Structure\CompoundDocument(

src/Endpoint/Show.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use JsonApiPhp\JsonApi\Included;
1616
use Psr\Http\Message\ResponseInterface;
1717
use Tobyz\JsonApiServer\Context;
18+
use Tobyz\JsonApiServer\Endpoint\Concerns\BuildsMeta;
19+
use Tobyz\JsonApiServer\Endpoint\Concerns\IncludesData;
1820
use Tobyz\JsonApiServer\ResourceType;
1921
use Tobyz\JsonApiServer\Serializer;
2022

@@ -23,7 +25,8 @@
2325

2426
class Show
2527
{
26-
use Concerns\IncludesData;
28+
use IncludesData;
29+
use BuildsMeta;
2730

2831
public function handle(Context $context, ResourceType $resourceType, $model): ResponseInterface
2932
{
@@ -39,7 +42,8 @@ public function handle(Context $context, ResourceType $resourceType, $model): Re
3942
return json_api_response(
4043
new CompoundDocument(
4144
$primary[0],
42-
new Included(...$included)
45+
new Included(...$included),
46+
...$this->buildMeta($context)
4347
)
4448
);
4549
}

src/Endpoint/Update.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Http\Message\ResponseInterface;
1515
use Tobyz\JsonApiServer\Context;
16+
use Tobyz\JsonApiServer\Endpoint\Concerns\BuildsMeta;
17+
use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData;
1618
use Tobyz\JsonApiServer\Exception\ForbiddenException;
1719
use Tobyz\JsonApiServer\ResourceType;
1820

@@ -21,7 +23,7 @@
2123

2224
class Update
2325
{
24-
use Concerns\SavesData;
26+
use SavesData;
2527

2628
/**
2729
* @throws ForbiddenException if the resource is not updatable.

tests/MockAdapter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function query()
3636

3737
public function find($query, string $id)
3838
{
39+
if ($id === '404') {
40+
return null;
41+
}
42+
3943
return $this->models[$id] ?? (object) ['id' => $id];
4044
}
4145

tests/specification/DeletingResourcesTest.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
namespace Tobyz\Tests\JsonApiServer\specification;
1313

14+
use Tobyz\JsonApiServer\Context;
15+
use Tobyz\JsonApiServer\Exception\ResourceNotFoundException;
1416
use Tobyz\JsonApiServer\JsonApi;
17+
use Tobyz\JsonApiServer\Schema\Type;
1518
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
1619
use Tobyz\Tests\JsonApiServer\MockAdapter;
1720

1821
/**
19-
* @see https://jsonapi.org/format/1.0/#crud-deleting
22+
* @see https://jsonapi.org/format/1.1/#crud-deleting
2023
*/
2124
class DeletingResourcesTest extends AbstractTestCase
2225
{
@@ -25,25 +28,48 @@ class DeletingResourcesTest extends AbstractTestCase
2528
*/
2629
private $api;
2730

28-
/**
29-
* @var MockAdapter
30-
*/
31-
private $adapter;
32-
3331
public function setUp(): void
3432
{
3533
$this->api = new JsonApi('http://example.com');
3634

37-
$this->adapter = new MockAdapter();
35+
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
36+
$type->deletable();
37+
});
3838
}
3939

4040
public function test_no_content_response_if_resource_successfully_deleted()
4141
{
42-
$this->markTestIncomplete();
42+
$response = $this->api->handle(
43+
$this->buildRequest('DELETE', '/users/1')
44+
);
45+
46+
$this->assertEquals(204, $response->getStatusCode());
47+
$this->assertEmpty($response->getBody()->getContents());
48+
}
49+
50+
public function test_ok_response_if_meta()
51+
{
52+
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
53+
$type->deletable();
54+
$type->deleting(function ($model, Context $context) {
55+
$context->meta('foo', 'bar');
56+
});
57+
});
58+
59+
$response = $this->api->handle(
60+
$this->buildRequest('DELETE', '/users/1')
61+
);
62+
63+
$this->assertEquals(200, $response->getStatusCode());
64+
$this->assertJsonApiDocumentSubset(['meta' => ['foo' => 'bar']], $response->getBody());
4365
}
4466

4567
public function test_not_found_error_if_resource_does_not_exist()
4668
{
47-
$this->markTestIncomplete();
69+
$this->expectException(ResourceNotFoundException::class);
70+
71+
$this->api->handle(
72+
$this->buildRequest('DELETE', '/users/404')
73+
);
4874
}
4975
}

0 commit comments

Comments
 (0)