Skip to content

Commit 3e76543

Browse files
committed
Add some additional tests
1 parent 1fe4ad2 commit 3e76543

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tobyz\Tests\JsonApiServer\feature;
4+
5+
use Tobyz\JsonApiServer\Endpoint\CollectionAction;
6+
use Tobyz\JsonApiServer\JsonApi;
7+
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
8+
use Tobyz\Tests\JsonApiServer\MockResource;
9+
10+
class CollectionActionTest extends AbstractTestCase
11+
{
12+
private JsonApi $api;
13+
14+
public function setUp(): void
15+
{
16+
$this->api = new JsonApi();
17+
}
18+
19+
public function test_collection_action()
20+
{
21+
$called = false;
22+
23+
$this->api->resource(
24+
new MockResource(
25+
'users',
26+
endpoints: [
27+
CollectionAction::make('test', function () use (&$called) {
28+
$called = true;
29+
})->method('POST'),
30+
],
31+
),
32+
);
33+
34+
$response = $this->api->handle($this->buildRequest('POST', '/users/test'));
35+
36+
$this->assertEquals(204, $response->getStatusCode());
37+
$this->assertTrue($called);
38+
}
39+
}

tests/feature/ResourceActionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tobyz\Tests\JsonApiServer\feature;
4+
5+
use Tobyz\JsonApiServer\Endpoint\ResourceAction;
6+
use Tobyz\JsonApiServer\JsonApi;
7+
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
8+
use Tobyz\Tests\JsonApiServer\MockResource;
9+
10+
class ResourceActionTest extends AbstractTestCase
11+
{
12+
private JsonApi $api;
13+
14+
public function setUp(): void
15+
{
16+
$this->api = new JsonApi();
17+
}
18+
19+
public function test_collection_action()
20+
{
21+
$called = false;
22+
23+
$this->api->resource(
24+
new MockResource(
25+
'users',
26+
models: [(object) ['id' => '1']],
27+
endpoints: [
28+
ResourceAction::make('test', function () use (&$called) {
29+
$called = true;
30+
})->method('POST'),
31+
],
32+
),
33+
);
34+
35+
$response = $this->api->handle($this->buildRequest('POST', '/users/1/test'));
36+
37+
$this->assertJsonApiDocumentSubset(
38+
['data' => ['type' => 'users', 'id' => '1']],
39+
$response->getBody(),
40+
);
41+
42+
$this->assertTrue($called);
43+
}
44+
}

tests/specification/SparseFieldsetsTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function setUp(): void
4444
fields: [
4545
Attribute::make('title'),
4646
Attribute::make('body'),
47-
Attribute::make('exclude'),
47+
Attribute::make('exclude')->sparse(),
4848
ToOne::make('author')
4949
->type('users')
5050
->includable(),
@@ -90,4 +90,13 @@ public function test_sparse_fieldsets()
9090
$this->assertArrayNotHasKey('exclude', $document['data']['attributes']);
9191
$this->assertArrayNotHasKey('color', $document['included'][0]['attributes']);
9292
}
93+
94+
public function test_sparse_by_default()
95+
{
96+
$response = $this->api->handle($this->buildRequest('GET', '/articles/1'));
97+
98+
$document = json_decode($response->getBody(), true);
99+
100+
$this->assertArrayNotHasKey('exclude', $document['data']['attributes']);
101+
}
93102
}

tests/unit/StrTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ class StrTest extends AbstractTestCase
1212
{
1313
public static function serializationProvider(): array
1414
{
15-
return [[Str::make(), 'string', 'string'], [Str::make(), 1, '1'], [Str::make(), null, '']];
15+
return [
16+
[Str::make(), 'string', 'string'],
17+
[Str::make(), 1, '1'],
18+
[Str::make(), null, ''],
19+
[Str::make(), StrTestEnum::A, 'a'],
20+
];
1621
}
1722

1823
#[DataProvider('serializationProvider')]
@@ -33,6 +38,10 @@ public static function validationProvider(): array
3338
[Str::make()->maxLength(1), 'aa', false],
3439
[Str::make()->pattern('\d+'), '1', true],
3540
[Str::make()->pattern('\d+'), 'a', false],
41+
[Str::make()->enum(['a', 'b']), 'a', true],
42+
[Str::make()->enum(['a', 'b']), 'c', false],
43+
[Str::make()->enum(StrTestEnum::cases()), 'a', true],
44+
[Str::make()->enum(StrTestEnum::cases()), 'c', false],
3645
];
3746
}
3847

@@ -50,3 +59,9 @@ public function test_validation(Type $type, mixed $value, bool $valid)
5059
$type->validate($value, $fail);
5160
}
5261
}
62+
63+
enum StrTestEnum: string
64+
{
65+
case A = 'a';
66+
case B = 'b';
67+
}

0 commit comments

Comments
 (0)