Completed
Push — master ( 835d6a...f13adc )
by William
02:52
created

HateoasBuilderTest::testCyclicalReferences()   B

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 40
Code Lines 24

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace Hateoas\Tests;
4
5
use Hateoas\HateoasBuilder;
6
use Hateoas\Tests\Fixtures\CircularReference1;
7
use Hateoas\Tests\Fixtures\CircularReference2;
8
use Hateoas\UrlGenerator\CallableUrlGenerator;
9
use JMS\Serializer\SerializationContext;
10
use Hateoas\Tests\Fixtures\AdrienBrault;
11
use Hateoas\Tests\Fixtures\WithAlternativeRouter;
12
13
/**
14
 * Contains functional tests
15
 */
16
class HateoasBuilderTest extends TestCase
17
{
18
    public function testBuild()
19
    {
20
        $hateoasBuilder = new HateoasBuilder();
21
        $hateoas = $hateoasBuilder->build();
22
23
        $this->assertInstanceOf('Hateoas\Hateoas', $hateoas);
24
    }
25
26
    public function testSerializeAdrienBraultWithExclusion()
27
    {
28
        $hateoas = HateoasBuilder::buildHateoas();
29
30
        $adrienBrault     = new AdrienBrault();
31
        $fakeAdrienBrault = new AdrienBrault();
32
        $fakeAdrienBrault->firstName = 'John';
33
        $fakeAdrienBrault->lastName = 'Smith';
34
35
        $context  = SerializationContext::create()->setGroups(array('simple'));
36
        $context2 = clone $context;
37
38
        $this->assertSame(
39
            <<<XML
40
<?xml version="1.0" encoding="UTF-8"?>
41
<result>
42
  <first_name><![CDATA[Adrien]]></first_name>
43
  <last_name><![CDATA[Brault]]></last_name>
44
  <link rel="self" href="http://adrienbrault.fr"/>
45
  <link rel="computer" href="http://www.apple.com/macbook-pro/"/>
46
</result>
47
48
XML
49
            ,
50
            $hateoas->serialize($adrienBrault, 'xml', $context)
51
        );
52
        $this->assertSame(
53
            <<<XML
54
<?xml version="1.0" encoding="UTF-8"?>
55
<result>
56
  <first_name><![CDATA[John]]></first_name>
57
  <last_name><![CDATA[Smith]]></last_name>
58
  <link rel="computer" href="http://www.apple.com/macbook-pro/"/>
59
</result>
60
61
XML
62
            ,
63
            $hateoas->serialize($fakeAdrienBrault, 'xml', $context2)
64
        );
65
    }
66
67
    public function testAlternativeUrlGenerator()
68
    {
69
        $brokenUrlGenerator = new CallableUrlGenerator(function ($name, $parameters) {
70
            return $name . '?' . http_build_query($parameters);
71
        });
72
73
        $hateoas = HateoasBuilder::create()
74
            ->setUrlGenerator('my_generator', $brokenUrlGenerator)
75
            ->build()
76
        ;
77
78
        $this->assertSame(
79
            <<<XML
80
<?xml version="1.0" encoding="UTF-8"?>
81
<result>
82
  <link rel="search" href="/search?query=hello"/>
83
</result>
84
85
XML
86
            ,
87
            $hateoas->serialize(new WithAlternativeRouter(), 'xml')
88
        );
89
    }
90
91
    public function testCyclicalReferences()
92
    {
93
        $hateoas = HateoasBuilder::create()->build();
94
95
        $reference1 = new CircularReference1();
96
        $reference2 = new CircularReference2();
97
        $reference1->setReference2($reference2);
98
        $reference2->setReference1($reference1);
99
100
        $this->assertSame(
101
            <<<XML
102
<?xml version="1.0" encoding="UTF-8"?>
103
<result>
104
  <name><![CDATA[reference1]]></name>
105
  <entry rel="reference2">
106
    <name><![CDATA[reference2]]></name>
107
    <entry rel="reference1"/>
108
  </entry>
109
</result>
110
111
XML
112
            ,
113
            $hateoas->serialize($reference1, 'xml')
114
        );
115
116
        $this->assertSame(
117
            '{'
118
                .'"name":"reference1",'
119
                .'"_embedded":{'
120
                    .'"reference2":{'
121
                        .'"name":"reference2",'
122
                        .'"_embedded":{'
123
                            .'"reference1":null'
124
                        .'}'
125
                    .'}'
126
                .'}'
127
            .'}',
128
            $hateoas->serialize($reference1, 'json')
129
        );
130
    }
131
}
132