expect($gallery['detail']['gallery_id'])->toBe($gallery['id']); expect($gallery['detail'])->toBeAn('array'); } }); }); describe("->get()", function () { it("returns `null` for unexisting foreign key", function () { Stub::on(GalleryDetail::class)->method('::all', function ($options = [], $fetchOptions = []) { return GalleryDetail::create([], ['type' => 'set', 'exists' => true, 'collector' => $fetchOptions['collector']]); }); $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]); expect($gallery->detail)->toBe(null); }); it("lazy loads a hasOne relation", function () { Stub::on(GalleryDetail::class)->method('::all', function ($options = [], $fetchOptions = []) { $details = GalleryDetail::create([['id' => 1, 'description' => 'Foo Gallery Description', 'gallery_id' => 1]], ['type' => 'set', 'exists' => true, 'collector' => $fetchOptions['collector']]); return $details; }); $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]); expect(GalleryDetail::class)->toReceive('::all')->with(['conditions' => ['gallery_id' => 1]], ['collector' => $gallery->collector()]); expect($gallery->detail->gallery_id)->toBe($gallery->id); expect($gallery->detail->collector())->toBe($gallery->collector()); }); }); describe("->save()", function () { it("bails out if no relation data hasn't been setted", function () { $hasOne = Gallery::relation('detail'); $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]); expect($hasOne->save($gallery))->toBe(true); }); it("saves a hasOne relationship", function () {