コード例 #1
0
    /**
     *
     */
    public function testItWillSerializeToJsonApiAComplexObjectAndFilterFields()
    {
        $mappings = HelperMapping::complex();
        $mapper = new Mapper($mappings);
        $expected = <<<JSON
{
   "data":{
      "type":"post",
      "id":"9",
      "attributes":{
         "title":"Hello World"
      },
      "links":{
         "self":{
            "href":"http://example.com/posts/9"
         },
         "comments":{
            "href":"http://example.com/posts/9/comments"
         }
      }
   },
   "links":{
      "self":{
         "href":"http://example.com/posts/9"
      },
      "comments":{
         "href":"http://example.com/posts/9/comments"
      }
   },
   "meta":{
      "author":{
         "name":"Nil Portugués Calderó",
         "email":"*****@*****.**"
      },
      "is_devel":true
   },
   "jsonapi":{
      "version":"1.0"
   }
}
JSON;
        $post = HelperFactory::complexPost();
        $transformer = new JsonApiTransformer($mapper);
        $transformer->setMeta(['author' => ['name' => 'Nil Portugués Calderó', 'email' => '*****@*****.**']]);
        $transformer->addMeta('is_devel', true);
        $fields = new Fields();
        $fields->addField('post', 'title');
        $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($transformer))->serialize($post, $fields), true));
    }
コード例 #2
0
 /**
  *
  */
 public function setUp()
 {
     $this->serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper(HelperMapping::complex())));
     $this->page = new Page(1, null, null, null, 10);
     $this->fields = new Fields();
     $this->sorting = new Sorting();
     $this->included = new Included();
     $this->resource = new ListResource($this->serializer, $this->page, $this->fields, $this->sorting, $this->included, $this->filters);
     $this->totalAmountCallable = function () {
         return 2015;
     };
     $this->resultsCallable = function () {
         $results = [];
         for ($i = 1; $i <= 10; ++$i) {
             $results[] = HelperFactory::complexPost();
         }
         return $results;
     };
 }