1 | <?php |
||
11 | class DataArraySerializerTest extends PHPUnit_Framework_TestCase |
||
12 | { |
||
13 | public function testSerializingItemResource() |
||
14 | { |
||
15 | $manager = new Manager(); |
||
16 | $manager->parseIncludes('author'); |
||
17 | $manager->setSerializer(new DataArraySerializer()); |
||
18 | |||
19 | $bookData = [ |
||
20 | 'title' => 'Foo', |
||
21 | 'year' => '1991', |
||
22 | '_author' => [ |
||
23 | 'name' => 'Dave', |
||
24 | ], |
||
25 | ]; |
||
26 | |||
27 | // Try without metadata |
||
28 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
29 | $scope = new Scope($manager, $resource); |
||
30 | |||
31 | $expected = [ |
||
32 | 'data' => [ |
||
33 | 'title' => 'Foo', |
||
34 | 'year' => 1991, |
||
35 | 'author' => [ |
||
36 | 'data' => [ |
||
37 | 'name' => 'Dave', |
||
38 | ], |
||
39 | ], |
||
40 | ], |
||
41 | ]; |
||
42 | |||
43 | $this->assertSame($expected, $scope->toArray()); |
||
44 | |||
45 | // Same again with metadata |
||
46 | $resource = new Item($bookData, new GenericBookTransformer(), 'book'); |
||
47 | $resource->setMetaValue('foo', 'bar'); |
||
48 | |||
49 | $scope = new Scope($manager, $resource); |
||
50 | |||
51 | |||
52 | $expected = [ |
||
53 | 'data' => [ |
||
54 | 'title' => 'Foo', |
||
55 | 'year' => 1991, |
||
56 | 'author' => [ |
||
57 | 'data' => [ |
||
58 | 'name' => 'Dave', |
||
59 | |||
60 | ], |
||
61 | ], |
||
62 | ], |
||
63 | 'meta' => [ |
||
64 | 'foo' => 'bar', |
||
65 | ], |
||
66 | ]; |
||
67 | |||
68 | $this->assertSame($expected, $scope->toArray()); |
||
69 | } |
||
70 | |||
71 | public function testSerializingCollectionResource() |
||
72 | { |
||
73 | $manager = new Manager(); |
||
74 | $manager->parseIncludes('author'); |
||
75 | $manager->setSerializer(new DataArraySerializer()); |
||
76 | |||
77 | $booksData = [ |
||
78 | [ |
||
79 | 'title' => 'Foo', |
||
80 | 'year' => '1991', |
||
81 | '_author' => [ |
||
82 | 'name' => 'Dave', |
||
83 | ], |
||
84 | ], |
||
85 | [ |
||
86 | 'title' => 'Bar', |
||
87 | 'year' => '1997', |
||
88 | '_author' => [ |
||
89 | 'name' => 'Bob', |
||
90 | ], |
||
91 | ], |
||
92 | ]; |
||
93 | |||
94 | // Try without metadata |
||
95 | $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); |
||
96 | |||
97 | $scope = new Scope($manager, $resource); |
||
98 | |||
99 | $expected = [ |
||
100 | 'data' => [ |
||
101 | [ |
||
102 | 'title' => 'Foo', |
||
103 | 'year' => 1991, |
||
104 | 'author' => [ |
||
105 | 'data' => [ |
||
106 | 'name' => 'Dave', |
||
107 | ], |
||
108 | ], |
||
109 | ], |
||
110 | [ |
||
111 | 'title' => 'Bar', |
||
112 | 'year' => 1997, |
||
113 | 'author' => [ |
||
114 | 'data' => [ |
||
115 | 'name' => 'Bob', |
||
116 | ], |
||
117 | ], |
||
118 | ], |
||
119 | ], |
||
120 | ]; |
||
121 | |||
122 | $this->assertSame($expected, $scope->toArray()); |
||
123 | |||
124 | $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}]}'; |
||
125 | $this->assertSame($expectedJson, $scope->toJson()); |
||
126 | |||
127 | // Same again with meta |
||
128 | $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); |
||
129 | $resource->setMetaValue('foo', 'bar'); |
||
130 | |||
131 | $scope = new Scope($manager, $resource); |
||
132 | |||
133 | |||
134 | $expected = [ |
||
135 | 'data' => [ |
||
136 | [ |
||
137 | 'title' => 'Foo', |
||
138 | 'year' => 1991, |
||
139 | 'author' => [ |
||
140 | 'data' => [ |
||
141 | 'name' => 'Dave', |
||
142 | ], |
||
143 | ], |
||
144 | ], |
||
145 | [ |
||
146 | 'title' => 'Bar', |
||
147 | 'year' => 1997, |
||
148 | 'author' => [ |
||
149 | 'data' => [ |
||
150 | 'name' => 'Bob', |
||
151 | |||
152 | ], |
||
153 | ], |
||
154 | ], |
||
155 | ], |
||
156 | 'meta' => [ |
||
157 | 'foo' => 'bar', |
||
158 | ], |
||
159 | ]; |
||
160 | |||
161 | $this->assertSame($expected, $scope->toArray()); |
||
162 | |||
163 | $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}],"meta":{"foo":"bar"}}'; |
||
164 | $this->assertSame($expectedJson, $scope->toJson()); |
||
165 | } |
||
166 | |||
167 | public function testSerializingNullResource() |
||
204 | |||
205 | public function tearDown() |
||
206 | { |
||
209 | } |
||
210 |