These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Tests\Serializer; |
||
4 | |||
5 | use Hateoas\HateoasBuilder; |
||
6 | use Hateoas\Model\Embedded; |
||
7 | use Hateoas\Model\Link; |
||
8 | use Hateoas\Serializer\JsonHalSerializer; |
||
9 | use Hateoas\Tests\Fixtures\AdrienBrault; |
||
10 | use Hateoas\Tests\Fixtures\Foo1; |
||
11 | use Hateoas\Tests\Fixtures\Foo2; |
||
12 | use Hateoas\Tests\Fixtures\Foo3; |
||
13 | use Hateoas\Tests\TestCase; |
||
14 | |||
15 | class JsonHalSerializerTest extends TestCase |
||
16 | { |
||
17 | public function testSerializeLinks() |
||
18 | { |
||
19 | $links = array( |
||
20 | new Link('self', '/users/42', array('awesome' => 'exactly')), |
||
21 | new Link('foo', '/bar'), |
||
22 | new Link('foo', '/baz'), |
||
23 | new Link('bar', '/foo'), |
||
24 | new Link('bar', '/baz'), |
||
25 | new Link('bar', '/buzz'), |
||
26 | ); |
||
27 | |||
28 | $expectedSerializedLinks = array( |
||
29 | 'self' => array( |
||
30 | 'href' => '/users/42', |
||
31 | 'awesome' => 'exactly', |
||
32 | ), |
||
33 | 'foo' => array( |
||
34 | array('href' => '/bar'), |
||
35 | array('href' => '/baz'), |
||
36 | ), |
||
37 | 'bar' => array( |
||
38 | array('href' => '/foo'), |
||
39 | array('href' => '/baz'), |
||
40 | array('href' => '/buzz'), |
||
41 | ), |
||
42 | ); |
||
43 | |||
44 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
45 | |||
46 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
47 | $jsonSerializationVisitorProphecy |
||
48 | ->addData('_links', $expectedSerializedLinks) |
||
49 | ->shouldBeCalledTimes(1) |
||
50 | ; |
||
51 | |||
52 | $jsonHalSerializer = new JsonHalSerializer(); |
||
53 | $jsonHalSerializer->serializeLinks( |
||
54 | $links, |
||
55 | $jsonSerializationVisitorProphecy->reveal(), |
||
56 | $contextProphecy->reveal() |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | public function testSerializeEmbeddeds() |
||
61 | { |
||
62 | $acceptArguments = array( |
||
63 | array('name' => 'John'), |
||
64 | array('name' => 'Bar'), |
||
65 | array('name' => 'Baz'), |
||
66 | array('name' => 'Foo'), |
||
67 | array('name' => 'Baz'), |
||
68 | array('name' => 'Buzz'), |
||
69 | ); |
||
70 | |||
71 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
72 | foreach ($acceptArguments as $arg) { |
||
73 | $contextProphecy |
||
74 | ->accept($arg) |
||
75 | ->willReturnArgument() |
||
76 | ; |
||
77 | } |
||
78 | |||
79 | $embeddeds = array( |
||
80 | new Embedded('friend', array('name' => 'John')), |
||
81 | new Embedded('foo', array('name' => 'Bar')), |
||
82 | new Embedded('foo', array('name' => 'Baz')), |
||
83 | new Embedded('bar', array('name' => 'Foo')), |
||
84 | new Embedded('bar', array('name' => 'Baz')), |
||
85 | new Embedded('bar', array('name' => 'Buzz')), |
||
86 | ); |
||
87 | |||
88 | $expectedEmbeddedded = array( |
||
89 | 'friend' => array('name' => 'John'), |
||
90 | 'foo' => array( |
||
91 | array('name' => 'Bar'), |
||
92 | array('name' => 'Baz'), |
||
93 | ), |
||
94 | 'bar' => array( |
||
95 | array('name' => 'Foo'), |
||
96 | array('name' => 'Baz'), |
||
97 | array('name' => 'Buzz'), |
||
98 | ), |
||
99 | ); |
||
100 | |||
101 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
102 | $jsonSerializationVisitorProphecy |
||
103 | ->addData('_embedded', $expectedEmbeddedded) |
||
104 | ->shouldBeCalledTimes(1) |
||
105 | ; |
||
106 | |||
107 | $jsonHalSerializer = new JsonHalSerializer(); |
||
108 | $jsonHalSerializer->serializeEmbeddeds( |
||
109 | $embeddeds, |
||
110 | $jsonSerializationVisitorProphecy->reveal(), |
||
111 | $contextProphecy->reveal() |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | public function testSerializeCuriesWithOneLinkShouldBeAnArray() |
||
116 | { |
||
117 | $links = array( |
||
118 | new Link('self', '/users/42'), |
||
119 | new Link('curies', '/rels/{rel}', array('name' => 'p')), |
||
120 | ); |
||
121 | |||
122 | $expectedSerializedLinks = array( |
||
123 | 'self' => array( |
||
124 | 'href' => '/users/42', |
||
125 | ), |
||
126 | 'curies' => array( |
||
127 | array( |
||
128 | 'href' => '/rels/{rel}', |
||
129 | 'name' => 'p', |
||
130 | ), |
||
131 | ), |
||
132 | ); |
||
133 | |||
134 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
135 | |||
136 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
137 | $jsonSerializationVisitorProphecy |
||
138 | ->addData('_links', $expectedSerializedLinks) |
||
139 | ->shouldBeCalledTimes(1) |
||
140 | ; |
||
141 | |||
142 | $jsonHalSerializer = new JsonHalSerializer(); |
||
143 | $jsonHalSerializer->serializeLinks( |
||
144 | $links, |
||
145 | $jsonSerializationVisitorProphecy->reveal(), |
||
146 | $contextProphecy->reveal() |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | public function testSerializeCuriesWithMultipleEntriesShouldBeAnArray() |
||
151 | { |
||
152 | $links = array( |
||
153 | new Link('self', '/users/42'), |
||
154 | new Link('curies', '/rels/{rel}', array('name' => 'p')), |
||
155 | new Link('curies', '/foo/rels/{rel}', array('name' => 'foo')), |
||
156 | ); |
||
157 | |||
158 | $expectedSerializedLinks = array( |
||
159 | 'self' => array( |
||
160 | 'href' => '/users/42', |
||
161 | ), |
||
162 | 'curies' => array( |
||
163 | array( |
||
164 | 'href' => '/rels/{rel}', |
||
165 | 'name' => 'p', |
||
166 | ), |
||
167 | array( |
||
168 | 'href' => '/foo/rels/{rel}', |
||
169 | 'name' => 'foo', |
||
170 | ), |
||
171 | ), |
||
172 | ); |
||
173 | |||
174 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
175 | |||
176 | $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); |
||
177 | $jsonSerializationVisitorProphecy |
||
178 | ->addData('_links', $expectedSerializedLinks) |
||
179 | ->shouldBeCalledTimes(1) |
||
180 | ; |
||
181 | |||
182 | $jsonHalSerializer = new JsonHalSerializer(); |
||
183 | $jsonHalSerializer->serializeLinks( |
||
184 | $links, |
||
185 | $jsonSerializationVisitorProphecy->reveal(), |
||
186 | $contextProphecy->reveal() |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | View Code Duplication | public function testSerializeAdrienBrault() |
|
0 ignored issues
–
show
|
|||
191 | { |
||
192 | $hateoas = HateoasBuilder::buildHateoas(); |
||
193 | $adrienBrault = new AdrienBrault(); |
||
194 | |||
195 | $this |
||
196 | ->json($hateoas->serialize($adrienBrault, 'json')) |
||
197 | ->isEqualTo(<<<JSON |
||
198 | { |
||
199 | "first_name": "Adrien", |
||
200 | "last_name": "Brault", |
||
201 | "_links": { |
||
202 | "self": { |
||
203 | "href": "http:\/\/adrienbrault.fr" |
||
204 | }, |
||
205 | "computer": { |
||
206 | "href": "http:\/\/www.apple.com\/macbook-pro\/" |
||
207 | }, |
||
208 | "dynamic-relation": { |
||
209 | "href": "awesome!!!" |
||
210 | } |
||
211 | }, |
||
212 | "_embedded": { |
||
213 | "computer": { |
||
214 | "name": "MacBook Pro" |
||
215 | }, |
||
216 | "broken-computer": { |
||
217 | "name": "Windows Computer" |
||
218 | }, |
||
219 | "smartphone": [ |
||
220 | { |
||
221 | "name": "iPhone 6" |
||
222 | }, |
||
223 | { |
||
224 | "name": "Nexus 5" |
||
225 | } |
||
226 | ], |
||
227 | "dynamic-relation": [ |
||
228 | "wowowow" |
||
229 | ] |
||
230 | } |
||
231 | } |
||
232 | JSON |
||
233 | ); |
||
234 | } |
||
235 | |||
236 | public function testSerializeInlineJson() |
||
237 | { |
||
238 | $foo1 = new Foo1(); |
||
239 | $foo2 = new Foo2(); |
||
240 | $foo3 = new Foo3(); |
||
241 | $foo1->inline = $foo2; |
||
242 | $foo2->inline = $foo3; |
||
243 | |||
244 | $hateoas = HateoasBuilder::buildHateoas(); |
||
245 | |||
246 | $this |
||
247 | ->json($hateoas->serialize($foo1, 'json')) |
||
248 | ->isEqualTo(<<<JSON |
||
249 | { |
||
250 | "_links": { |
||
251 | "self3": { |
||
252 | "href": "foo3" |
||
253 | }, |
||
254 | "self2": { |
||
255 | "href": "foo2" |
||
256 | }, |
||
257 | "self1": { |
||
258 | "href": "foo1" |
||
259 | } |
||
260 | }, |
||
261 | "_embedded": { |
||
262 | "self3": "foo3", |
||
263 | "self2": "foo2", |
||
264 | "self1": "foo1" |
||
265 | } |
||
266 | } |
||
267 | JSON |
||
268 | ); |
||
269 | } |
||
270 | } |
||
271 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.