-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocumentTest.php
More file actions
84 lines (72 loc) · 2.08 KB
/
DocumentTest.php
File metadata and controls
84 lines (72 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace FreeElephants\JsonApiToolkit\DTO;
use FreeElephants\JsonApiToolkit\AbstractTestCase;
use Nyholm\Psr7\ServerRequest;
class DocumentTest extends AbstractTestCase
{
public function testFromRequest()
{
$request = new ServerRequest('POST', '/foo');
$request->getBody()->write(<<<JSON
{
"data": {
"id": "123",
"type": "foo",
"attributes": {
"foo": "bar",
"date": "2012-04-23T18:25:43.511Z",
"nested": {
"someNestedStructure": {
"someKey": "someValue"
}
}
},
"relationships": {
"baz": {
"data": {
"type": "bazs",
"id": "baz-id"
}
}
}
}
}
JSON
);
$fooDTO = FooDocument::fromHttpMessage($request);
$this->assertInstanceOf(FooResource::class, $fooDTO->data);
$this->assertInstanceOf(FooAttributes::class, $fooDTO->data->attributes);
$this->assertSame('foo', $fooDTO->data->type);
$this->assertSame('bar', $fooDTO->data->attributes->foo);
$this->assertEquals(new \DateTime('2012-04-23T18:25:43.511Z'), $fooDTO->data->attributes->date);
$this->assertSame('someValue', $fooDTO->data->attributes->nested->someNestedStructure->someKey);
$this->assertSame('baz-id', $fooDTO->data->relationships->baz->data->id);
}
}
class FooDocument extends AbstractDocument
{
public FooResource $data;
}
class FooResource extends AbstractResourceObject
{
public FooAttributes $attributes;
public FooRelationships $relationships;
}
class FooAttributes extends AbstractAttributes
{
public string $foo;
public \DateTime $date;
public Nested $nested;
}
class FooRelationships extends AbstractRelationships
{
public \FreeElephants\JsonApi\DTO\RelationshipToOne $baz;
}
class Nested extends BaseKeyValueStructure
{
public SomeNestedStructure $someNestedStructure;
}
class SomeNestedStructure extends BaseKeyValueStructure
{
public string $someKey;
}