GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#246)
by Asmir
04:23
created

JsonHalSerializerTest::testGh236()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 9

Duplication

Lines 26
Ratio 83.87 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 26
loc 31
rs 8.8571
cc 1
eloc 9
nc 1
nop 0
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\Representation\CollectionRepresentation;
9
use Hateoas\Serializer\JsonHalSerializer;
10
use Hateoas\Serializer\Metadata\EmbeddedPropertyMetadata;
11
use Hateoas\Tests\Fixtures\AdrienBrault;
12
use Hateoas\Tests\Fixtures\Foo1;
13
use Hateoas\Tests\Fixtures\Foo2;
14
use Hateoas\Tests\Fixtures\Foo3;
15
use Hateoas\Tests\Fixtures\Gh236Foo;
16
use Hateoas\Tests\TestCase;
17
use JMS\Serializer\SerializationContext;
18
use JMS\Serializer\SerializerBuilder;
19
use Prophecy\Argument;
20
21
class JsonHalSerializerTest extends TestCase
22
{
23
    public function testSerializeLinks()
24
    {
25
        $links = array(
26
            new Link('self', '/users/42', array('awesome' => 'exactly')),
27
            new Link('foo', '/bar'),
28
            new Link('foo', '/baz'),
29
            new Link('bar', '/foo'),
30
            new Link('bar', '/baz'),
31
            new Link('bar', '/buzz'),
32
        );
33
34
        $expectedSerializedLinks = array(
35
            'self' => array(
36
                'href' => '/users/42',
37
                'awesome' => 'exactly',
38
            ),
39
            'foo' => array(
40
                array('href' => '/bar'),
41
                array('href' => '/baz'),
42
            ),
43
            'bar' => array(
44
                array('href' => '/foo'),
45
                array('href' => '/baz'),
46
                array('href' => '/buzz'),
47
            ),
48
        );
49
50
        $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext');
51
52
        $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor');
53
        $jsonSerializationVisitorProphecy
54
            ->addData('_links', $expectedSerializedLinks)
55
            ->shouldBeCalledTimes(1)
56
        ;
57
58
        $jsonHalSerializer = new JsonHalSerializer();
59
        $jsonHalSerializer->serializeLinks(
60
            $links,
61
            $jsonSerializationVisitorProphecy->reveal(),
62
            $contextProphecy->reveal()
63
        );
64
    }
65
66
    public function testSerializeEmbeddeds()
67
    {
68
        $acceptArguments = array(
69
            array('name' => 'John'),
70
            array('name' => 'Bar'),
71
            array('name' => 'Baz'),
72
            array('name' => 'Foo'),
73
            array('name' => 'Baz'),
74
            array('name' => 'Buzz'),
75
        );
76
77
        $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext');
78
        foreach ($acceptArguments as $arg) {
79
            $contextProphecy
80
                ->accept($arg)
81
                ->willReturnArgument()
82
            ;
83
        }
84
        $contextProphecy->pushPropertyMetadata(Argument::type('Hateoas\Serializer\Metadata\RelationPropertyMetadata'))->shouldBeCalled();
85
        $contextProphecy->popPropertyMetadata()->shouldBeCalled();
86
87
        $embeddeds = array(
88
            new Embedded('friend', array('name' => 'John')),
89
            new Embedded('foo', array('name' => 'Bar')),
90
            new Embedded('foo', array('name' => 'Baz')),
91
            new Embedded('bar', array('name' => 'Foo')),
92
            new Embedded('bar', array('name' => 'Baz')),
93
            new Embedded('bar', array('name' => 'Buzz')),
94
        );
95
96
        $expectedEmbeddedded = array(
97
            'friend' => array('name' => 'John'),
98
            'foo' => array(
99
                array('name' => 'Bar'),
100
                array('name' => 'Baz'),
101
            ),
102
            'bar' => array(
103
                array('name' => 'Foo'),
104
                array('name' => 'Baz'),
105
                array('name' => 'Buzz'),
106
            ),
107
        );
108
109
        $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor');
110
        $jsonSerializationVisitorProphecy
111
            ->addData('_embedded', $expectedEmbeddedded)
112
            ->shouldBeCalledTimes(1)
113
        ;
114
115
        $jsonHalSerializer = new JsonHalSerializer();
116
        $jsonHalSerializer->serializeEmbeddeds(
117
            $embeddeds,
118
            $jsonSerializationVisitorProphecy->reveal(),
119
            $contextProphecy->reveal()
120
        );
121
    }
122
123
    public function testSerializeCuriesWithOneLinkShouldBeAnArray()
124
    {
125
        $links = array(
126
            new Link('self',   '/users/42'),
127
            new Link('curies', '/rels/{rel}', array('name' => 'p')),
128
        );
129
130
        $expectedSerializedLinks = array(
131
            'self' => array(
132
                'href' => '/users/42',
133
            ),
134
            'curies' => array(
135
                array(
136
                    'href' => '/rels/{rel}',
137
                    'name' => 'p',
138
                ),
139
            ),
140
        );
141
142
        $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext');
143
144
        $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor');
145
        $jsonSerializationVisitorProphecy
146
            ->addData('_links', $expectedSerializedLinks)
147
            ->shouldBeCalledTimes(1)
148
        ;
149
150
        $jsonHalSerializer = new JsonHalSerializer();
151
        $jsonHalSerializer->serializeLinks(
152
            $links,
153
            $jsonSerializationVisitorProphecy->reveal(),
154
            $contextProphecy->reveal()
155
        );
156
    }
157
158
    public function testSerializeCuriesWithMultipleEntriesShouldBeAnArray()
159
    {
160
        $links = array(
161
            new Link('self',   '/users/42'),
162
            new Link('curies', '/rels/{rel}', array('name' => 'p')),
163
            new Link('curies', '/foo/rels/{rel}', array('name' => 'foo')),
164
        );
165
166
        $expectedSerializedLinks = array(
167
            'self' => array(
168
                'href' => '/users/42',
169
            ),
170
            'curies' => array(
171
                array(
172
                    'href' => '/rels/{rel}',
173
                    'name' => 'p',
174
                ),
175
                array(
176
                    'href' => '/foo/rels/{rel}',
177
                    'name' => 'foo',
178
                ),
179
            ),
180
        );
181
182
        $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext');
183
184
        $jsonSerializationVisitorProphecy = $this->prophesize('JMS\Serializer\JsonSerializationVisitor');
185
        $jsonSerializationVisitorProphecy
186
            ->addData('_links', $expectedSerializedLinks)
187
            ->shouldBeCalledTimes(1)
188
        ;
189
190
        $jsonHalSerializer = new JsonHalSerializer();
191
        $jsonHalSerializer->serializeLinks(
192
            $links,
193
            $jsonSerializationVisitorProphecy->reveal(),
194
            $contextProphecy->reveal()
195
        );
196
    }
197
198
    public function testSerializeAdrienBrault()
199
    {
200
        $hateoas      = HateoasBuilder::buildHateoas();
201
        $adrienBrault = new AdrienBrault();
202
203
        $this->assertSame(
204
            <<<JSON
205
{
206
    "first_name": "Adrien",
207
    "last_name": "Brault",
208
    "_links": {
209
        "self": {
210
            "href": "http:\/\/adrienbrault.fr"
211
        },
212
        "computer": {
213
            "href": "http:\/\/www.apple.com\/macbook-pro\/"
214
        },
215
        "dynamic-relation": {
216
            "href": "awesome!!!"
217
        }
218
    },
219
    "_embedded": {
220
        "computer": {
221
            "name": "MacBook Pro"
222
        },
223
        "broken-computer": {
224
            "name": "Windows Computer"
225
        },
226
        "smartphone": [
227
            {
228
                "name": "iPhone 6"
229
            },
230
            {
231
                "name": "Nexus 5"
232
            }
233
        ],
234
        "dynamic-relation": [
235
            "wowowow"
236
        ]
237
    }
238
}
239
JSON
240
            ,
241
            $this->json($hateoas->serialize($adrienBrault, 'json'))
242
        );
243
    }
244
245
    public function testSerializeInlineJson()
246
    {
247
        $foo1 = new Foo1();
248
        $foo2 = new Foo2();
249
        $foo3 = new Foo3();
250
        $foo1->inline = $foo2;
251
        $foo2->inline = $foo3;
252
253
        $hateoas = HateoasBuilder::buildHateoas();
254
255
        $this->assertSame(
256
            <<<JSON
257
{
258
    "_links": {
259
        "self3": {
260
            "href": "foo3"
261
        },
262
        "self2": {
263
            "href": "foo2"
264
        },
265
        "self1": {
266
            "href": "foo1"
267
        }
268
    },
269
    "_embedded": {
270
        "self3": "foo3",
271
        "self2": "foo2",
272
        "self1": "foo1"
273
    }
274
}
275
JSON
276
            ,
277
            $this->json($hateoas->serialize($foo1, 'json'))
278
        );
279
    }
280
281 View Code Duplication
    public function testGh236()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
282
    {
283
        $data = new CollectionRepresentation([new Gh236Foo()]);
284
285
        $hateoas = HateoasBuilder::buildHateoas();
286
287
        $this->assertSame(
288
            <<<JSON
289
{
290
    "_embedded": {
291
        "items": [
292
            {
293
                "a": {
294
                    "xxx": "yyy"
295
                },
296
                "_embedded": {
297
                    "b_embed": {
298
                        "xxx": "zzz"
299
                    }
300
                }
301
            }
302
        ]
303
    }
304
}
305
JSON
306
            ,
307
            $this->json(
308
                $hateoas->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks())
309
            )
310
        );
311
    }
312
}
313