|
15 | 15 | use App\Models\Post;
|
16 | 16 | use App\Models\Tag;
|
17 | 17 | use Closure;
|
| 18 | +use LaravelJsonApi\Core\Document\Links; |
18 | 19 | use LaravelJsonApi\Core\Facades\JsonApi;
|
19 | 20 | use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
|
20 | 21 | use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;
|
@@ -60,7 +61,7 @@ protected function setUp(): void
|
60 | 61 | /**
|
61 | 62 | * @return array[]
|
62 | 63 | */
|
63 |
| - public static function scenarioProvider(): array |
| 64 | + public static function relationshipProvider(): array |
64 | 65 | {
|
65 | 66 | return [
|
66 | 67 | 'hidden' => [
|
@@ -99,39 +100,81 @@ static function (PostSchema $schema, Post $post) {
|
99 | 100 | /**
|
100 | 101 | * @param Closure $scenario
|
101 | 102 | * @return void
|
102 |
| - * @dataProvider scenarioProvider |
| 103 | + * @dataProvider relationshipProvider |
103 | 104 | */
|
104 |
| - public function testRelated(Closure $scenario): void |
| 105 | + public function testRelationship(Closure $scenario): void |
105 | 106 | {
|
106 | 107 | $expected = $scenario($this->schema, $this->post);
|
107 | 108 |
|
108 | 109 | $response = $this
|
109 | 110 | ->withoutExceptionHandling()
|
110 | 111 | ->jsonApi('tags')
|
111 |
| - ->get(url('/api/v1/posts', [$this->post, 'tags'])); |
| 112 | + ->get(url('/api/v1/posts', [$this->post, 'relationships', 'tags'])); |
112 | 113 |
|
113 |
| - $response->assertFetchedMany([$this->tag]); |
| 114 | + $response->assertFetchedToMany([$this->tag]); |
114 | 115 |
|
115 | 116 | if (is_array($expected)) {
|
116 | 117 | $response->assertLinks($expected);
|
117 | 118 | }
|
118 | 119 | }
|
119 | 120 |
|
| 121 | + |
| 122 | + /** |
| 123 | + * @return array[] |
| 124 | + */ |
| 125 | + public static function relatedProvider(): array |
| 126 | + { |
| 127 | + return [ |
| 128 | + 'hidden' => [ |
| 129 | + static function (PostSchema $schema) { |
| 130 | + $schema->relationship('tags')->hidden(); |
| 131 | + return null; |
| 132 | + }, |
| 133 | + ], |
| 134 | + 'no links' => [ |
| 135 | + static function (PostSchema $schema) { |
| 136 | + $schema->relationship('tags')->serializeUsing( |
| 137 | + static fn($relation) => $relation->withoutLinks() |
| 138 | + ); |
| 139 | + return null; |
| 140 | + }, |
| 141 | + ], |
| 142 | + 'no self link' => [ |
| 143 | + static function (PostSchema $schema, Post $post) { |
| 144 | + $schema->relationship('tags')->serializeUsing( |
| 145 | + static fn($relation) => $relation->withoutSelfLink() |
| 146 | + ); |
| 147 | + // related becomes self. |
| 148 | + return ['self' => url('/api/v1/posts', [$post, 'tags'])]; |
| 149 | + }, |
| 150 | + ], |
| 151 | + 'no related link' => [ |
| 152 | + static function (PostSchema $schema, Post $post) { |
| 153 | + $schema->relationship('tags')->serializeUsing( |
| 154 | + static fn($relation) => $relation->withoutRelatedLink() |
| 155 | + ); |
| 156 | + // related becomes self, but it's missing so we can't do that. |
| 157 | + return null; |
| 158 | + }, |
| 159 | + ], |
| 160 | + ]; |
| 161 | + } |
| 162 | + |
120 | 163 | /**
|
121 | 164 | * @param Closure $scenario
|
122 | 165 | * @return void
|
123 |
| - * @dataProvider scenarioProvider |
| 166 | + * @dataProvider relatedProvider |
124 | 167 | */
|
125 |
| - public function testSelf(Closure $scenario): void |
| 168 | + public function testRelated(Closure $scenario): void |
126 | 169 | {
|
127 | 170 | $expected = $scenario($this->schema, $this->post);
|
128 | 171 |
|
129 | 172 | $response = $this
|
130 | 173 | ->withoutExceptionHandling()
|
131 | 174 | ->jsonApi('tags')
|
132 |
| - ->get(url('/api/v1/posts', [$this->post, 'relationships', 'tags'])); |
| 175 | + ->get(url('/api/v1/posts', [$this->post, 'tags'])); |
133 | 176 |
|
134 |
| - $response->assertFetchedToMany([$this->tag]); |
| 177 | + $response->assertFetchedMany([$this->tag]); |
135 | 178 |
|
136 | 179 | if (is_array($expected)) {
|
137 | 180 | $response->assertLinks($expected);
|
|
0 commit comments