forked from json-api-php/json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceTest.php
120 lines (112 loc) · 3.3 KB
/
ResourceTest.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
<?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;
use JsonApiPhp\JsonApi\Document\Resource\Relationship\Relationship;
use JsonApiPhp\JsonApi\Document\Resource\ResourceObject;
use JsonApiPhp\JsonApi\Document\Resource\ResourceId;
use JsonApiPhp\JsonApi\Test\HasAssertEqualsAsJson;
use PHPUnit\Framework\TestCase;
/**
* Resource Objects
*
* @see http://jsonapi.org/format/#document-resource-objects
*/
class ResourceTest extends TestCase
{
use HasAssertEqualsAsJson;
/**
* @param array $expected
* @param mixed $data
* @dataProvider resourceProvider
*/
public function testSerialization(array $expected, $data)
{
$this->assertEqualsAsJson($expected, $data);
}
public function resourceProvider()
{
return [
[
[
'type' => 'books',
],
new ResourceId('books'),
],
[
[
'type' => 'books',
'id' => '42abc',
],
new ResourceId('books', '42abc'),
],
[
[
'type' => 'books',
'id' => '42abc',
'meta' => [
'foo' => 'bar',
],
],
new ResourceId('books', '42abc', ['foo' => 'bar']),
],
[
[
'type' => 'books',
'id' => '42abc',
'attributes' => [
'attr' => 'val',
],
'relationships' => [
'author' => [
'meta' => [
'a' => 'b',
],
],
],
'links' => [
'self' => 'http://localhost',
],
'meta' => [
'foo' => 'bar',
],
],
(function () {
$resource = new ResourceObject('books', '42abc');
$resource->setMeta('foo', 'bar');
$resource->setAttribute('attr', 'val');
$resource->setLink('self', 'http://localhost');
$resource->setRelationship('author', Relationship::fromMeta(['a' => 'b']));
return $resource;
})()
],
];
}
/**
* @param string $name
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid attribute name
* @dataProvider invalidAttributeNames
*/
public function testAttributeCanNotHaveReservedNames(string $name)
{
$r = new ResourceObject('books', 'abc');
$r->setAttribute($name, 1);
}
public function invalidAttributeNames(): array
{
return [
['id'],
['type'],
];
}
}