Skip to content

Commit 44f585a

Browse files
committed
test: implement more tests
1 parent 91dca6d commit 44f585a

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

tests/Feature/User/ResourceTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Test\Feature\User;
44

5+
use Ark4ne\JsonApi\Support\Config;
56
use DateTimeInterface;
67
use Illuminate\Http\Request;
78
use Test\app\Http\Resources\CommentResource;
@@ -134,12 +135,22 @@ public function testShowMultipleFailures()
134135
]]);
135136
}
136137

138+
public function testShowBasicAutoWhenIncluded()
139+
{
140+
Config::$autoWhenIncluded = true;
141+
$user = $this->dataSeed();
142+
143+
$response = $this->get("user/{$user->id}");
144+
145+
$response->assertExactJson($this->getJsonResult($user, null, null, false));
146+
}
147+
137148
private function dataSeed()
138149
{
139150
return User::first();
140151
}
141152

142-
private function getJsonResult(User $user, ?array $attributes = null, ?array $relationships = null)
153+
private function getJsonResult(User $user, ?array $attributes = null, ?array $relationships = null, bool $withIncluded = true): array
143154
{
144155
$request = new Request(array_merge(
145156
($attributes !== null ? ['fields' => ['user' => implode(',', $attributes)]] : []),
@@ -156,7 +167,7 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
156167
], array_fill_keys($attributes ?? ['name', 'email'], true))),
157168
'relationships' => [
158169
'posts' => array_filter([
159-
'data' => $user->posts->map(fn(Post $post) => ['type' => 'post', 'id' => $post->id])->all(),
170+
'data' => $withIncluded ? $user->posts->map(fn(Post $post) => ['type' => 'post', 'id' => $post->id])->all() : null,
160171
'links' => [
161172
'self' => "https://api.example.com/user/{$user->id}/relationships/posts",
162173
'related' => "https://api.example.com/user/{$user->id}/posts",
@@ -167,7 +178,7 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
167178
]),
168179
'comments' => array_filter([
169180
// when loaded only
170-
'data' => in_array('comments', $relationships ?? [])
181+
'data' => $withIncluded && in_array('comments', $relationships ?? [])
171182
? $user->comments->map(fn(Comment $comment) => [
172183
'type' => 'comment',
173184
'id' => $comment->id

tests/Unit/Descriptors/DescriptorsTraitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Ark4ne\JsonApi\Descriptors\Values\ValueArray;
1010
use Ark4ne\JsonApi\Descriptors\Values\ValueBool;
1111
use Ark4ne\JsonApi\Descriptors\Values\ValueDate;
12+
use Ark4ne\JsonApi\Descriptors\Values\ValueEnum;
1213
use Ark4ne\JsonApi\Descriptors\Values\ValueFloat;
1314
use Ark4ne\JsonApi\Descriptors\Values\ValueInteger;
1415
use Ark4ne\JsonApi\Descriptors\Values\ValueMixed;
@@ -31,6 +32,7 @@ public static function methods()
3132
'string' => [ValueString::class, 'string'],
3233
'date' => [ValueDate::class, 'date'],
3334
'array' => [ValueArray::class, 'array'],
35+
'enum' => [ValueEnum::class, 'enum'],
3436
'mixed' => [ValueMixed::class, 'mixed'],
3537
'one' => [RelationOne::class, 'one', JsonApiResource::class],
3638
'many' => [RelationMany::class, 'many', JsonApiCollection::class],

tests/Unit/Descriptors/ValueTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ public function testWhenFilled(&$model)
188188
);
189189
}
190190

191+
/**
192+
* @dataProvider models
193+
*/
194+
public function testUnless(&$model)
195+
{
196+
data_set($model, 'attr', 'abc');
197+
198+
$unless = (object)['value' => true];
199+
200+
$this->throughRetrieverTest(
201+
$model,
202+
fn(Value $value) => $value->unless(fn() => $unless->value)->valueFor(new Request, $model, 'attr'),
203+
fn() => $unless->value = false,
204+
fn(Value $value) => $value->valueFor(new Request, $model, 'attr'),
205+
'abc'
206+
);
207+
}
208+
191209
/**
192210
* @dataProvider models
193211
*/
@@ -240,10 +258,10 @@ private function throughRetrieverTest(&$model, \Closure $missing, \Closure $upda
240258
$this->assertInstanceOf(MissingValue::class, $missing($valueClosureRetriever));
241259

242260
$update();
243-
244-
$this->assertEquals($expected, $check($valueWithRetriever));
245-
$this->assertEquals($expected, $check($valueNoRetriever));
246-
$this->assertEquals($expected, $check($valueClosureRetriever));
261+
$actual = $check($valueWithRetriever);
262+
$this->assertEquals($expected, $actual);
263+
$this->assertEquals($expected, $actual = $check($valueNoRetriever));
264+
$this->assertEquals($expected, $actual = $check($valueClosureRetriever));
247265
}
248266
}
249267

tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,14 @@ public function testWhenHas()
137137
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'whenHas', 'b', 'abc'));
138138
$this->assertEquals('missing', Reflect::invoke($resource, 'whenHas', 'b', 'abc', 'missing'));
139139
}
140+
141+
public function testUnless()
142+
{
143+
$resource = new class([]) extends JsonResource {
144+
use ConditionallyLoadsAttributes;
145+
};
146+
147+
$this->assertEquals(new MissingValue, Reflect::invoke($resource, 'unless', true, 'a'));
148+
$this->assertEquals('a', Reflect::invoke($resource, 'unless', false, 'a'));
149+
}
140150
}

0 commit comments

Comments
 (0)