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