Ejemplo n.º 1
0
         $payload = new Payload();
         $payload->set($image);
         expect($payload->isCollection())->toBe(false);
         expect($payload->data())->toBe(['type' => 'image', 'id' => 1, 'attributes' => ['gallery_id' => 1, 'name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200'], 'relationships' => ['gallery' => ['data' => ['type' => 'gallery', 'id' => 1]], 'tags' => ['data' => [['type' => 'tag', 'id' => 1], ['type' => 'tag', 'id' => 3]]]]]);
         expect($payload->included())->toBe([['type' => 'gallery', 'id' => 1, 'attributes' => ['name' => 'Foo Gallery']], ['type' => 'image_tag', 'id' => 1, 'attributes' => ['image_id' => 1, 'tag_id' => 1]], ['type' => 'image_tag', 'id' => 2, 'attributes' => ['image_id' => 1, 'tag_id' => 3]], ['type' => 'tag', 'id' => 1, 'attributes' => ['name' => 'High Tech']], ['type' => 'tag', 'id' => 3, 'attributes' => ['name' => 'Computer']]]);
     });
     it("serializes collections", function () {
         $this->fixtures->populate('image');
         $images = Image::find()->where(['id' => [1, 2]])->all();
         $images->meta(['count' => 10]);
         $payload = new Payload();
         $payload->set($images);
         expect($payload->isCollection())->toBe(true);
         expect($payload->data())->toBe([['type' => 'image', 'id' => 1, 'attributes' => ['gallery_id' => 1, 'name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200']], ['type' => 'image', 'id' => 2, 'attributes' => ['gallery_id' => 1, 'name' => 'srinivasa_ramanujan.jpg', 'title' => 'Srinivasa Ramanujan']]]);
         expect($payload->included())->toBe([]);
         expect($payload->meta())->toBe(['count' => 10]);
     });
     it("serializes parsed JSON-API payload", function () {
         $json = file_get_contents($this->fixture . 'collection.json');
         $payload = Payload::parse($json);
         expect($payload->serialize())->toEqual(json_decode($json, true));
         $json = file_get_contents($this->fixture . 'item.json');
         $payload = Payload::parse($json);
         expect($payload->serialize())->toEqual(json_decode($json, true));
     });
 });
 describe("::parse()", function () {
     it("parses JSON-API payload", function () {
         $json = file_get_contents($this->fixture . 'collection.json');
         $payload = Payload::parse($json);
         expect($payload->jsonapi())->toBe(['version' => '1.0']);