Skip to content

Commit 9846de5

Browse files
committed
Add getters tests
1 parent b9f6858 commit 9846de5

File tree

1 file changed

+129
-3
lines changed

1 file changed

+129
-3
lines changed

tests/feature/FieldGettersTest.php

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,150 @@
1111

1212
namespace Tobyz\Tests\JsonApiServer\feature;
1313

14+
use Tobyz\JsonApiServer\Context;
15+
use Tobyz\JsonApiServer\JsonApi;
16+
use Tobyz\JsonApiServer\Schema\Type;
1417
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
18+
use Tobyz\Tests\JsonApiServer\MockAdapter;
1519

1620
class FieldGettersTest extends AbstractTestCase
1721
{
22+
/**
23+
* @var JsonApi
24+
*/
25+
private $api;
26+
27+
/**
28+
* @var MockAdapter
29+
*/
30+
private $adapter;
31+
1832
public function setUp(): void
1933
{
34+
$this->api = new JsonApi('/');
2035

36+
$this->adapter = new MockAdapter([
37+
'1' => (object) [
38+
'id' => '1',
39+
'test' => 'value',
40+
'animal' => (object) ['id' => '1'],
41+
'animals' => [
42+
(object) ['id' => '1'],
43+
(object) ['id' => '2']
44+
]
45+
]
46+
]);
2147
}
2248

2349
public function test_attribute_values_are_retrieved_via_the_adapter_by_default()
2450
{
25-
$this->markTestIncomplete();
51+
$this->api->resource('users', $this->adapter, function (Type $type) {
52+
$type->attribute('test');
53+
});
54+
55+
$response = $this->api->handle(
56+
$this->buildRequest('GET', '/users/1')
57+
);
58+
59+
$document = json_decode($response->getBody());
60+
61+
$this->assertEquals('value', $document->data->attributes->test ?? null);
2662
}
2763

2864
public function test_attribute_getters_allow_a_custom_value_to_be_used()
2965
{
30-
$this->markTestIncomplete();
66+
$this->api->resource('users', $this->adapter, function (Type $type) {
67+
$type->attribute('test')
68+
->get(function ($model, Context $context) {
69+
return 'custom';
70+
});
71+
});
72+
73+
$response = $this->api->handle(
74+
$this->buildRequest('GET', '/users/1')
75+
);
76+
77+
$document = json_decode($response->getBody());
78+
79+
$this->assertEquals('custom', $document->data->attributes->test ?? null);
80+
}
81+
82+
public function test_has_one_values_are_retrieved_via_the_adapter_by_default()
83+
{
84+
$this->api->resource('users', $this->adapter, function (Type $type) {
85+
$type->hasOne('animal')->withLinkage();
86+
});
87+
88+
$this->api->resource('animals', new MockAdapter);
89+
90+
$response = $this->api->handle(
91+
$this->buildRequest('GET', '/users/1')
92+
);
93+
94+
$document = json_decode($response->getBody());
95+
96+
$this->assertEquals('1', $document->data->relationships->animal->data->id ?? null);
3197
}
3298

33-
// to_one, to_many...
99+
public function test_has_one_getters_allow_a_custom_value_to_be_used()
100+
{
101+
$this->api->resource('users', $this->adapter, function (Type $type) {
102+
$type->hasOne('animal')->withLinkage()
103+
->get(function ($model, Context $context) {
104+
return (object) ['id' => '2'];
105+
});
106+
});
107+
108+
$this->api->resource('animals', new MockAdapter);
109+
110+
$response = $this->api->handle(
111+
$this->buildRequest('GET', '/users/1')
112+
);
113+
114+
$document = json_decode($response->getBody());
115+
116+
$this->assertEquals('2', $document->data->relationships->animal->data->id ?? null);
117+
}
118+
119+
public function test_has_many_values_are_retrieved_via_the_adapter_by_default()
120+
{
121+
$this->api->resource('users', $this->adapter, function (Type $type) {
122+
$type->hasMany('animals')->withLinkage();
123+
});
124+
125+
$this->api->resource('animals', new MockAdapter);
126+
127+
$response = $this->api->handle(
128+
$this->buildRequest('GET', '/users/1')
129+
);
130+
131+
$document = json_decode($response->getBody());
132+
133+
$this->assertEquals('1', $document->data->relationships->animals->data[0]->id ?? null);
134+
$this->assertEquals('2', $document->data->relationships->animals->data[1]->id ?? null);
135+
}
136+
137+
public function test_has_many_getters_allow_a_custom_value_to_be_used()
138+
{
139+
$this->api->resource('users', $this->adapter, function (Type $type) {
140+
$type->hasMany('animals')->withLinkage()
141+
->get(function ($model, Context $context) {
142+
return [
143+
(object) ['id' => '2'],
144+
(object) ['id' => '3']
145+
];
146+
});
147+
});
148+
149+
$this->api->resource('animals', new MockAdapter);
150+
151+
$response = $this->api->handle(
152+
$this->buildRequest('GET', '/users/1')
153+
);
154+
155+
$document = json_decode($response->getBody());
156+
157+
$this->assertEquals('2', $document->data->relationships->animals->data[0]->id ?? null);
158+
$this->assertEquals('3', $document->data->relationships->animals->data[1]->id ?? null);
159+
}
34160
}

0 commit comments

Comments
 (0)