$r->resource('Tag'); }); $this->connection = $connection; $this->fixtures = new Fixtures(['connection' => $connection, 'fixtures' => ['gallery' => 'Lead\\Resource\\Spec\\Fixture\\Schema\\Gallery', 'gallery_detail' => 'Lead\\Resource\\Spec\\Fixture\\Schema\\GalleryDetail', 'image' => 'Lead\\Resource\\Spec\\Fixture\\Schema\\Image', 'image_tag' => 'Lead\\Resource\\Spec\\Fixture\\Schema\\ImageTag', 'tag' => 'Lead\\Resource\\Spec\\Fixture\\Schema\\Tag']]); }); afterEach(function () { $this->fixtures->drop(); $this->fixtures->reset(); }); it("loads a resource", function () { $this->fixtures->populate('gallery'); $r = $this->router; $route = $r->route('gallery/1', 'GET'); $route->dispatch($this->response); expect($route->params)->toBe(['relation' => null, 'rid' => null, 'resource' => 'gallery', 'id' => '1', 'action' => null]); $data = $route->dispatched->data(); $expected = Gallery::load(1)->first()->data(); expect($data['gallery']->data())->toBe($expected); }); it("loads some related resource", function () { $this->fixtures->populate('gallery'); $this->fixtures->populate('image'); $r = $this->router; $route = $r->route('gallery/1/image', 'GET'); $route->dispatch($this->response); expect($route->params)->toBe(['relation' => 'gallery', 'rid' => '1', 'resource' => 'image', 'id' => null, 'action' => null]); $data = $route->dispatched->data(); $expected = Image::find(['conditions' => ['gallery_id' => 1]])->all()->data(); expect($data['image']->data())->toBe($expected); }); });
}); it("serializes existing entities", function () { $this->fixtures->populate('gallery'); $this->fixtures->populate('image'); $this->fixtures->populate('image_tag'); $this->fixtures->populate('tag'); $image = Image::load(1, ['embed' => ['gallery', 'tags']]); $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));