Beispiel #1
0
    public function testSerializeAdrienBraultWithExclusion()
    {
        $hateoas = HateoasBuilder::buildHateoas();
        $adrienBrault = new AdrienBrault();
        $fakeAdrienBrault = new AdrienBrault();
        $fakeAdrienBrault->firstName = 'John';
        $fakeAdrienBrault->lastName = 'Smith';
        $context = SerializationContext::create()->setGroups(array('simple'));
        $context2 = clone $context;
        $this->string($hateoas->serialize($adrienBrault, 'xml', $context))->isEqualTo(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <first_name><![CDATA[Adrien]]></first_name>
  <last_name><![CDATA[Brault]]></last_name>
  <link rel="self" href="http://adrienbrault.fr"/>
  <link rel="computer" href="http://www.apple.com/macbook-pro/"/>
</result>

XML
)->string($hateoas->serialize($fakeAdrienBrault, 'xml', $context2))->isEqualTo(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <first_name><![CDATA[John]]></first_name>
  <last_name><![CDATA[Smith]]></last_name>
  <link rel="computer" href="http://www.apple.com/macbook-pro/"/>
</result>

XML
);
    }
    public function test()
    {
        $hateoas = HateoasBuilder::buildHateoas();
        $halHateoas = HateoasBuilder::create()->setXmlSerializer(new XmlHalSerializer())->build();
        $collection = new SimpleCollection(array('Adrien', 'William'), 'authors');
        $collection->setXmlElementName('users');
        $this->string($hateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<collection>
  <users rel="authors">
    <entry><![CDATA[Adrien]]></entry>
    <entry><![CDATA[William]]></entry>
  </users>
</collection>

XML
)->string($halHateoas->serialize($collection, 'xml'))->isEqualTo(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<collection>
  <resource rel="authors"><![CDATA[Adrien]]></resource>
  <resource rel="authors"><![CDATA[William]]></resource>
</collection>

XML
)->string($halHateoas->serialize($collection, 'json'))->isEqualTo('{"_embedded":{"authors":["Adrien","William"]}}');
    }
Beispiel #3
0
    public function testSerializeAdrienBrault()
    {
        $hateoas = HateoasBuilder::buildHateoas();
        $adrienBrault = new AdrienBrault();
        $this->assertSame(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
  <first_name><![CDATA[Adrien]]></first_name>
  <last_name><![CDATA[Brault]]></last_name>
  <link rel="self" href="http://adrienbrault.fr"/>
  <link rel="computer" href="http://www.apple.com/macbook-pro/"/>
  <link rel="dynamic-relation" href="awesome!!!"/>
  <computer rel="computer">
    <name><![CDATA[MacBook Pro]]></name>
  </computer>
  <computer rel="broken-computer">
    <name><![CDATA[Windows Computer]]></name>
  </computer>
  <smartphone rel="smartphone">
    <name><![CDATA[iPhone 6]]></name>
  </smartphone>
  <smartphone rel="smartphone">
    <name><![CDATA[Nexus 5]]></name>
  </smartphone>
  <entry rel="dynamic-relation">
    <entry><![CDATA[wowowow]]></entry>
  </entry>
</result>

XML
, $hateoas->serialize($adrienBrault, 'xml'));
    }
 public function testSerializeInlineJson()
 {
     $foo1 = new Foo1();
     $foo2 = new Foo2();
     $foo3 = new Foo3();
     $foo1->inline = $foo2;
     $foo2->inline = $foo3;
     $hateoas = HateoasBuilder::buildHateoas();
     $this->string($hateoas->serialize($foo1, 'json'))->isEqualTo('{' . '"_links":{' . '"self3":{"href":"foo3"},' . '"self2":{"href":"foo2"},' . '"self1":{"href":"foo1"}},' . '"_embedded":{' . '"self3":"foo3",' . '"self2":"foo2",' . '"self1":"foo1"' . '}' . '}');
 }
    public function testSerializeInlineJson()
    {
        $foo1 = new Foo1();
        $foo2 = new Foo2();
        $foo3 = new Foo3();
        $foo1->inline = $foo2;
        $foo2->inline = $foo3;
        $hateoas = HateoasBuilder::buildHateoas();
        $this->json($hateoas->serialize($foo1, 'json'))->isEqualTo(<<<JSON
{
    "_links": {
        "self3": {
            "href": "foo3"
        },
        "self2": {
            "href": "foo2"
        },
        "self1": {
            "href": "foo1"
        }
    },
    "_embedded": {
        "self3": "foo3",
        "self2": "foo2",
        "self1": "foo1"
    }
}
JSON
);
    }