|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tobyz\Tests\JsonApiServer\feature; |
| 4 | + |
| 5 | +use Tobyz\JsonApiServer\Endpoint\Show; |
| 6 | +use Tobyz\JsonApiServer\JsonApi; |
| 7 | +use Tobyz\JsonApiServer\Schema\Link; |
| 8 | +use Tobyz\Tests\JsonApiServer\AbstractTestCase; |
| 9 | +use Tobyz\Tests\JsonApiServer\MockResource; |
| 10 | + |
| 11 | +class ResourceLinksTest extends AbstractTestCase |
| 12 | +{ |
| 13 | + private JsonApi $api; |
| 14 | + |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + $this->api = new JsonApi('/api'); |
| 18 | + } |
| 19 | + |
| 20 | + public function test_resource_links_are_included_in_response() |
| 21 | + { |
| 22 | + $this->api->resource( |
| 23 | + new MockResource( |
| 24 | + 'articles', |
| 25 | + models: [(object) ['id' => '1', 'title' => 'Hello World', 'slug' => 'hello-world']], |
| 26 | + endpoints: [Show::make()], |
| 27 | + links: [ |
| 28 | + Link::make('describedby')->get( |
| 29 | + fn() => 'https://api.example.com/schemas/articles', |
| 30 | + ), |
| 31 | + |
| 32 | + Link::make('canonical')->get( |
| 33 | + fn($model) => [ |
| 34 | + 'href' => "https://example.com/articles/{$model->slug}", |
| 35 | + 'title' => 'View on website', |
| 36 | + 'type' => 'text/html', |
| 37 | + ], |
| 38 | + ), |
| 39 | + ], |
| 40 | + ), |
| 41 | + ); |
| 42 | + |
| 43 | + $response = $this->api->handle($this->buildRequest('GET', '/api/articles/1')); |
| 44 | + |
| 45 | + $this->assertJsonApiDocumentSubset( |
| 46 | + [ |
| 47 | + 'data' => [ |
| 48 | + 'type' => 'articles', |
| 49 | + 'id' => '1', |
| 50 | + 'links' => [ |
| 51 | + 'describedby' => 'https://api.example.com/schemas/articles', |
| 52 | + 'canonical' => [ |
| 53 | + 'href' => 'https://example.com/articles/hello-world', |
| 54 | + 'title' => 'View on website', |
| 55 | + 'type' => 'text/html', |
| 56 | + ], |
| 57 | + ], |
| 58 | + ], |
| 59 | + ], |
| 60 | + $response->getBody(), |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
0 commit comments