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\XmlSerializer; |
||
9 | use Hateoas\Tests\Fixtures\AdrienBrault; |
||
10 | use Hateoas\Tests\TestCase; |
||
11 | use JMS\Serializer\XmlSerializationVisitor; |
||
12 | |||
13 | class XmlSerializerTest extends TestCase |
||
14 | { |
||
15 | View Code Duplication | public function testSerializeLinks() |
|
16 | { |
||
17 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
18 | |||
19 | $xmlSerializer = new XmlSerializer(); |
||
20 | $xmlSerializationVisitor = $this->createXmlSerializationVisitor(); |
||
21 | |||
22 | $links = array( |
||
23 | new Link('self', '/users/42'), |
||
24 | new Link('foo', '/bar', array('type' => 'magic')), |
||
25 | ); |
||
26 | |||
27 | $xmlSerializer->serializeLinks( |
||
28 | $links, |
||
29 | $xmlSerializationVisitor, |
||
30 | $contextProphecy->reveal() |
||
31 | ); |
||
32 | |||
33 | $this |
||
34 | ->string($xmlSerializationVisitor->getResult()) |
||
35 | ->isEqualTo(<<<XML |
||
36 | <?xml version="1.0" encoding="UTF-8"?> |
||
37 | <root> |
||
38 | <link rel="self" href="/users/42"/> |
||
39 | <link rel="foo" href="/bar" type="magic"/> |
||
40 | </root> |
||
41 | |||
42 | XML |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | public function testSerializeEmbeddeds() |
|
47 | { |
||
48 | $contextProphecy = $this->prophesize('JMS\Serializer\SerializationContext'); |
||
49 | |||
50 | $embeddeds = array( |
||
51 | new Embedded('friend', array('name' => 'John'), 'person'), |
||
52 | ); |
||
53 | |||
54 | $xmlSerializationVisitor = $this->createXmlSerializationVisitor(); |
||
55 | |||
56 | $xmlSerializer = new XmlSerializer(); |
||
57 | $xmlSerializer->serializeEmbeddeds( |
||
58 | $embeddeds, |
||
59 | $xmlSerializationVisitor, |
||
60 | $contextProphecy->reveal() |
||
61 | ); |
||
62 | |||
63 | $this |
||
64 | ->string($xmlSerializationVisitor->getResult()) |
||
65 | ->isEqualTo(<<<XML |
||
66 | <?xml version="1.0" encoding="UTF-8"?> |
||
67 | <root> |
||
68 | <person rel="friend"> |
||
69 | <entry/> |
||
70 | </person> |
||
71 | </root> |
||
72 | |||
73 | XML |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | View Code Duplication | public function testSerializeAdrienBrault() |
|
0 ignored issues
–
show
|
|||
78 | { |
||
79 | $hateoas = HateoasBuilder::buildHateoas(); |
||
80 | $adrienBrault = new AdrienBrault(); |
||
81 | |||
82 | $this |
||
83 | ->string($hateoas->serialize($adrienBrault, 'xml')) |
||
84 | ->isEqualTo(<<<XML |
||
85 | <?xml version="1.0" encoding="UTF-8"?> |
||
86 | <result> |
||
87 | <first_name><![CDATA[Adrien]]></first_name> |
||
88 | <last_name><![CDATA[Brault]]></last_name> |
||
89 | <link rel="self" href="http://adrienbrault.fr"/> |
||
90 | <link rel="computer" href="http://www.apple.com/macbook-pro/"/> |
||
91 | <link rel="dynamic-relation" href="awesome!!!"/> |
||
92 | <computer rel="computer"> |
||
93 | <name><![CDATA[MacBook Pro]]></name> |
||
94 | </computer> |
||
95 | <computer rel="broken-computer"> |
||
96 | <name><![CDATA[Windows Computer]]></name> |
||
97 | </computer> |
||
98 | <smartphone rel="smartphone"> |
||
99 | <name><![CDATA[iPhone 6]]></name> |
||
100 | </smartphone> |
||
101 | <smartphone rel="smartphone"> |
||
102 | <name><![CDATA[Nexus 5]]></name> |
||
103 | </smartphone> |
||
104 | <entry rel="dynamic-relation"> |
||
105 | <entry><![CDATA[wowowow]]></entry> |
||
106 | </entry> |
||
107 | </result> |
||
108 | |||
109 | XML |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | private function createXmlSerializationVisitor() |
||
114 | { |
||
115 | $xmlSerializationVisitor = new XmlSerializationVisitor( |
||
116 | $this->prophesize('JMS\Serializer\Naming\PropertyNamingStrategyInterface')->reveal() |
||
117 | ); |
||
118 | $xmlSerializationVisitorClass = new \ReflectionClass('JMS\Serializer\XmlSerializationVisitor'); |
||
119 | $stackProperty = $xmlSerializationVisitorClass->getProperty('stack'); |
||
120 | $stackProperty->setAccessible('true'); |
||
121 | $stackProperty->setValue($xmlSerializationVisitor, new \SplStack()); |
||
122 | |||
123 | $xmlSerializationVisitor->document = $xmlSerializationVisitor->createDocument(null, null, false); |
||
124 | $xmlRootNode = $xmlSerializationVisitor->document->createElement('root'); |
||
125 | $xmlSerializationVisitor->document->appendChild($xmlRootNode); |
||
126 | $xmlSerializationVisitor->setCurrentNode($xmlRootNode); |
||
127 | |||
128 | return $xmlSerializationVisitor; |
||
129 | } |
||
130 | } |
||
131 |
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.