Completed
Push — master ( 1978d2...e2f699 )
by William
03:55
created

CollectionRepresentationTest::testSerialize()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 50
Code Lines 20

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 50
rs 9.3333
cc 1
eloc 20
nc 1
nop 1
1
<?php
2
3
namespace Hateoas\Tests\Representation;
4
5
use Hateoas\Configuration\Relation;
6
use Hateoas\Configuration\Route;
7
use Hateoas\Representation\CollectionRepresentation;
8
9
class CollectionRepresentationTest extends RepresentationTestCase
10
{
11
    /**
12
     * @dataProvider getTestSerializeData
13
     */
14
    public function testSerialize($resources)
15
    {
16
        $collection = new CollectionRepresentation(
17
            $resources,
18
            'authors'
19
        );
20
        $collection->setXmlElementName('users');
21
22
        $this->assertSame(
23
            <<<XML
24
<?xml version="1.0" encoding="UTF-8"?>
25
<collection>
26
  <users rel="authors">
27
    <entry><![CDATA[Adrien]]></entry>
28
    <entry><![CDATA[William]]></entry>
29
  </users>
30
</collection>
31
32
XML
33
            ,
34
            $this->hateoas->serialize($collection, 'xml')
35
        );
36
        $this->assertSame(
37
            <<<XML
38
<?xml version="1.0" encoding="UTF-8"?>
39
<collection>
40
  <resource rel="authors"><![CDATA[Adrien]]></resource>
41
  <resource rel="authors"><![CDATA[William]]></resource>
42
</collection>
43
44
XML
45
            ,
46
            $this->halHateoas->serialize($collection, 'xml')
47
        );
48
49
        $this->assertSame(
50
            <<<JSON
51
{
52
    "_embedded": {
53
        "authors": [
54
            "Adrien",
55
            "William"
56
        ]
57
    }
58
}
59
JSON
60
            ,
61
            $this->json($this->halHateoas->serialize($collection, 'json'))
62
        );
63
    }
64
65
    public function getTestSerializeData()
66
    {
67
        return array(
68
            array(
69
                array(
70
                    'Adrien',
71
                    'William',
72
                )
73
            ),
74
            array(
75
                new \ArrayIterator(array(
76
                    'Adrien',
77
                    'William',
78
                ))
79
            ),
80
        );
81
    }
82
83
    public function testEmbeddedRelationIsMergedWithCustomRelations()
84
    {
85
        $collection = new CollectionRepresentation(
86
            array(
87
                'Adrien',
88
                'William',
89
            ),
90
            'authors',
91
            null,
92
            null,
93
            null,
94
            array(
95
                new Relation(
96
                    'custom',
97
                    new Route('/custom')
98
                ),
99
            )
100
        );
101
        $collection->setXmlElementName('users');
102
103
        $this->assertSame(
104
            <<<XML
105
<?xml version="1.0" encoding="UTF-8"?>
106
<collection>
107
  <link rel="custom" href="/custom"/>
108
  <users rel="authors">
109
    <entry><![CDATA[Adrien]]></entry>
110
    <entry><![CDATA[William]]></entry>
111
  </users>
112
</collection>
113
114
XML
115
            ,
116
            $this->hateoas->serialize($collection, 'xml')
117
        );
118
        $this->assertSame(
119
            <<<XML
120
<?xml version="1.0" encoding="UTF-8"?>
121
<collection>
122
  <link rel="custom" href="/custom"/>
123
  <resource rel="authors"><![CDATA[Adrien]]></resource>
124
  <resource rel="authors"><![CDATA[William]]></resource>
125
</collection>
126
127
XML
128
            ,
129
            $this->halHateoas->serialize($collection, 'xml')
130
        );
131
132
        $this->assertSame(
133
            <<<JSON
134
{
135
    "_links": {
136
        "custom": {
137
            "href": "\/custom"
138
        }
139
    },
140
    "_embedded": {
141
        "authors": [
142
            "Adrien",
143
            "William"
144
        ]
145
    }
146
}
147
JSON
148
            ,
149
            $this->json($this->halHateoas->serialize($collection, 'json'))
150
        );
151
    }
152
}
153