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.

JsonHalSerializerTest::testSerializeEmbeddeds()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
rs 9.7251
cc 2
eloc 40
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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