forked from json-api-php/json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkageTest.php
126 lines (110 loc) · 3.86 KB
/
LinkageTest.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
*
* * This file is part of JSON:API implementation for PHP.
* *
* * (c) Alexey Karapetov <[email protected]>
* *
* * For the full copyright and license information, please view the LICENSE
* * file that was distributed with this source code.
*
*/
declare(strict_types=1);
namespace JsonApiPhp\JsonApi\Test\Document\Resource\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\NullData;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Linkage;
use JsonApiPhp\JsonApi\Document\Resource\ResourceId;
use JsonApiPhp\JsonApi\Test\HasAssertEqualsAsJson;
use PHPUnit\Framework\TestCase;
/**
* Resource Linkage
*
* Resource linkage in a compound document allows a client to link together
* all of the included resource objects without having to GET any URLs via links.
*
* Resource linkage MUST be represented as one of the following:
* - null for empty to-one relationships.
* - an empty array ([]) for empty to-many relationships.
* - a single resource identifier object for non-empty to-one relationships.
* - an array of resource identifier objects for non-empty to-many relationships.
*
* @see http://jsonapi.org/format/#document-resource-object-linkage
* @see LinkageTest::testCanCreateNullLinkage()
* @see LinkageTest::testCanCreateEmptyArrayLinkage()
* @see LinkageTest::testCanCreateFromSingleResourceId()
* @see LinkageTest::testCanCreateFromArrayOfResourceIds()
*/
class LinkageTest extends TestCase
{
use HasAssertEqualsAsJson;
public function testCanCreateNullLinkage()
{
$this->assertEqualsAsJson(
null,
Linkage::nullLinkage()
);
}
public function testCanCreateEmptyArrayLinkage()
{
$this->assertEqualsAsJson(
[],
Linkage::emptyArrayLinkage()
);
}
public function testCanCreateFromSingleResourceId()
{
$this->assertEqualsAsJson(
[
'type' => 'books',
'id' => 'abc',
],
Linkage::fromSingleResourceId(new ResourceId('books', 'abc'))
);
}
public function testCanCreateFromArrayOfResourceIds()
{
$this->assertEqualsAsJson(
[
[
'type' => 'books',
'id' => 'abc',
],
[
'type' => 'squirrels',
'id' => '123',
],
],
Linkage::fromManyResourceIds(new ResourceId('books', 'abc'), new ResourceId('squirrels', '123'))
);
}
public function testNullLinkageIsLinkedToNothing()
{
$apple = new ResourceId('apples', '1');
$this->assertFalse(Linkage::nullLinkage()->isLinkedTo($apple));
$this->assertFalse(Linkage::nullLinkage()->isLinkedTo(new NullData));
}
public function testEmptyArrayLinkageIsLinkedToNothing()
{
$apple = new ResourceId('apples', '1');
$this->assertFalse(Linkage::emptyArrayLinkage()->isLinkedTo($apple));
$this->assertFalse(Linkage::emptyArrayLinkage()->isLinkedTo(new NullData));
}
public function testSingleLinkageIsLinkedOnlyToItself()
{
$apple = new ResourceId('apples', '1');
$orange = new ResourceId('oranges', '1');
$linkage = Linkage::fromSingleResourceId($apple);
$this->assertTrue($linkage->isLinkedTo($apple));
$this->assertFalse($linkage->isLinkedTo($orange));
}
public function testMultiLinkageIsLinkedOnlyToItsMembers()
{
$apple = new ResourceId('apples', '1');
$orange = new ResourceId('oranges', '1');
$banana = new ResourceId('bananas', '1');
$linkage = Linkage::fromManyResourceIds($apple, $orange);
$this->assertTrue($linkage->isLinkedTo($apple));
$this->assertTrue($linkage->isLinkedTo($orange));
$this->assertFalse($linkage->isLinkedTo($banana));
}
}