-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJsonApiParserTest.php
110 lines (103 loc) · 2.93 KB
/
JsonApiParserTest.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
<?php
/**
* @author Anton Tuyakhov <atuyakhov@gmail.com>
*/
namespace tuyakhov\jsonapi\tests;
use tuyakhov\jsonapi\JsonApiParser;
use yii\helpers\Json;
use yii\web\BadRequestHttpException;
class JsonApiParserTest extends TestCase
{
public function testEmptyBody()
{
$parser = new JsonApiParser();
$body = '';
$this->assertEquals([], $parser->parse($body, ''));
}
public function testMissingData()
{
$parser = new JsonApiParser();
$this->expectException(BadRequestHttpException::class);
$body = Json::encode(['incorrect-member']);
$parser->parse($body, '');
}
public function testSingleResource()
{
$parser = new JsonApiParser();
$body = Json::encode([
'data' => [
'type' => 'resource-models',
'attributes' => [
'field1' => 'test',
'field2' => 2,
'first-name' => 'Bob'
],
'relationships' => [
'author' => [
'data' => [
'id' => '321',
'type' => 'resource-models'
]
],
'client' => [
'data' => [
['id' => '321', 'type' => 'resource-models'],
['id' => '123', 'type' => 'resource-models']
]
]
]
]
]);
$this->assertEquals([
'ResourceModel' => [
'field1' => 'test',
'field2' => 2,
'first_name' => 'Bob',
],
'relationships' => [
'author' => [
'ResourceModel' => [
['id' => '321']
]
],
'client' => [
'ResourceModel' => [
['id' => '321'],
['id' => '123']
]
]
]
], $parser->parse($body, ''));
}
public function testMultiple()
{
$parser = new JsonApiParser();
$resourceActual = [
'type' => 'resource-models',
'id' => 12,
'attributes' => [
'field1' => 'test',
'field2' => 2,
'first-name' => 'Bob'
],
];
$resourceExpected = [
'id' => 12,
'field1' => 'test',
'field2' => 2,
'first_name' => 'Bob',
];
$body = Json::encode([
'data' => [
$resourceActual,
$resourceActual
]
]);
$this->assertEquals([
'ResourceModel' => [
$resourceExpected,
$resourceExpected
],
], $parser->parse($body, ''));
}
}