Skip to content

Commit 6c17337

Browse files
committed
[Refactor] Add failing test for default include paths
1 parent a8c2851 commit 6c17337

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Contracts/Schema/SchemaProviderInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@ public function getRelationshipSelfLink(object $resource, string $field): LinkIn
105105
* @return LinkInterface
106106
*/
107107
public function getRelationshipRelatedLink(object $resource, string $field): LinkInterface;
108+
109+
/**
110+
* Get schema default include paths.
111+
*
112+
* @return string[]
113+
*/
114+
public function getIncludePaths(): array;
108115
}

src/Schema/SchemaProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ public function getRelationshipRelatedLink(object $resource, string $field): Lin
169169
);
170170
}
171171

172+
/**
173+
* @inheritDoc
174+
*/
175+
public function getIncludePaths(): array
176+
{
177+
return [];
178+
}
179+
172180
/**
173181
* Get the relationship self url.
174182
*

tests/lib/Integration/Eloquent/ResourceTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
namespace CloudCreativity\LaravelJsonApi\Tests\Integration\Eloquent;
1919

2020
use Carbon\Carbon;
21+
use CloudCreativity\LaravelJsonApi\Factories\Factory;
2122
use CloudCreativity\LaravelJsonApi\Tests\Integration\TestCase;
2223
use DummyApp\Comment;
24+
use DummyApp\JsonApi\Posts\Schema;
2325
use DummyApp\Post;
2426
use DummyApp\Tag;
2527
use Illuminate\Support\Facades\Event;
@@ -422,6 +424,45 @@ public function testReadWithEmptyInclude(): void
422424
$response->assertFetchedOne($this->serialize($post));
423425
}
424426

427+
public function testReadWithDefaultInclude(): void
428+
{
429+
$mockSchema = $this
430+
->getMockBuilder(Schema::class)
431+
->onlyMethods(['getIncludePaths'])
432+
->setConstructorArgs([$this->app->make(Factory::class)])
433+
->getMock();
434+
435+
$mockSchema->method('getIncludePaths')->willReturn(['author', 'tags', 'comments']);
436+
437+
$this->app->instance(Schema::class, $mockSchema);
438+
439+
$model = $this->createPost();
440+
$tag = $model->tags()->create(['name' => 'Important']);
441+
442+
$expected = $this->serialize($model);
443+
444+
$expected['relationships']['author']['data'] = [
445+
'type' => 'users',
446+
'id' => (string) $model->author_id,
447+
];
448+
449+
$expected['relationships']['tags']['data'] = [
450+
['type' => 'tags', 'id' => $tag->uuid],
451+
];
452+
453+
$expected['relationships']['comments']['data'] = [];
454+
455+
$response = $this
456+
->withoutExceptionHandling()
457+
->jsonApi()
458+
->get(url('/api/v1/posts', $model));
459+
460+
$response
461+
->assertFetchedOne($expected)
462+
->assertIsIncluded('users', $model->author)
463+
->assertIsIncluded('tags', $tag);
464+
}
465+
425466
/**
426467
* @see https://github.com/cloudcreativity/laravel-json-api/issues/194
427468
*/

0 commit comments

Comments
 (0)