示例#1
0
    /**
     *
     */
    public function testItWillSerializeToHalXmlAnArrayOfObjects()
    {
        $postArray = [new SimplePost(1, 'post title 1', 'post body 1', 4), new SimplePost(2, 'post title 2', 'post body 2', 5)];
        $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']);
        $postMapping->setFilterKeys(['body', 'title']);
        $mapper = new Mapper();
        $mapper->setClassMap([$postMapping->getClassName() => $postMapping]);
        $transformer = new XmlTransformer($mapper);
        $expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<resource>
  <total><![CDATA[2]]></total>
  <embedded>
    <resource href="/post/1">
      <post_id><![CDATA[1]]></post_id>
      <title><![CDATA[post title 1]]></title>
      <body><![CDATA[post body 1]]></body>
      <author_id><![CDATA[4]]></author_id>
      <comments/>
      <links>
        <link rel="self" href="/post/1"/>
      </links>
    </resource>
    <resource href="/post/2">
      <post_id><![CDATA[2]]></post_id>
      <title><![CDATA[post title 2]]></title>
      <body><![CDATA[post body 2]]></body>
      <author_id><![CDATA[5]]></author_id>
      <comments/>
      <links>
        <link rel="self" href="/post/2"/>
      </links>
    </resource>
  </embedded>
</resource>
XML;
        $this->assertEquals($expected, (new HalSerializer($transformer))->serialize($postArray));
    }
示例#2
0
    public function testItIfFilteringOutKeys()
    {
        $post = $this->createSimplePost();
        $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']);
        $postMapping->setFilterKeys(['body']);
        $mapper = new Mapper();
        $mapper->setClassMap([$postMapping->getClassName() => $postMapping]);
        $transformer = new JsonTransformer($mapper);
        $expected = <<<JSON
{
    "post_id": 1,
    "title": "post title",
    "body": "post body",
    "author_id": 2,
    "comments": [
        {
            "comment_id": 10,
            "comment": "I am writing comment no. 1",
            "user_id": "User 5",
            "created_at": "2015-07-19T12:48:00+02:00"
        },
        {
            "comment_id": 20,
            "comment": "I am writing comment no. 2",
            "user_id": "User 10",
            "created_at": "2015-07-20T12:48:00+02:00"
        },
        {
            "comment_id": 30,
            "comment": "I am writing comment no. 3",
            "user_id": "User 15",
            "created_at": "2015-07-21T12:48:00+02:00"
        },
        {
            "comment_id": 40,
            "comment": "I am writing comment no. 4",
            "user_id": "User 20",
            "created_at": "2015-07-22T12:48:00+02:00"
        },
        {
            "comment_id": 50,
            "comment": "I am writing comment no. 5",
            "user_id": "User 25",
            "created_at": "2015-07-23T12:48:00+02:00"
        }
    ],
    "_links":{
        "self":{
            "href":"/post/1"
        }
    }
}
JSON;
        $this->assertEquals(\json_decode($expected, true), \json_decode((new HalSerializer($transformer))->serialize($post), true));
    }
    /**
     *
     */
    public function testItWillSerializeToJsonApiAnArrayOfObjects()
    {
        $postArray = [new SimplePost(1, 'post title 1', 'post body 1', 4), new SimplePost(2, 'post title 2', 'post body 2', 5)];
        $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']);
        $postMapping->setProperties(['postId', 'title', 'body', 'authorId', 'comments']);
        $postMapping->setFilterKeys(['body', 'title']);
        $mapper = new Mapper();
        $mapper->setClassMap([$postMapping->getClassName() => $postMapping]);
        $jsonApiJsonApiSerializer = new JsonApiTransformer($mapper);
        $expected = <<<JSON
{
   "data":[
      {
         "type":"post",
         "id":"1",
         "attributes":{
            "title":"post title 1",
            "body":"post body 1"
         },
         "links":{
            "self":{
               "href":"/post/1"
            }
         }
      },
      {
         "type":"post",
         "id":"2",
         "attributes":{
            "title":"post title 2",
            "body":"post body 2"
         },
         "links":{
            "self":{
               "href":"/post/2"
            }
         }
      }
   ],
   "jsonapi":{
      "version":"1.0"
   }
}
JSON;
        $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($jsonApiJsonApiSerializer))->serialize($postArray), true));
    }
 public function testGetFilterKeys()
 {
     $this->mapping->setFilterKeys(['password']);
     $this->assertEquals(['password'], $this->mapping->getFilterKeys());
 }