describe("->counterpart()", function () { it("returns the counterpart relationship for belongsTo/hasMany relations", function () { $relation = Image::definition()->relation('gallery'); expect($relation->counterpart())->toBe(Gallery::definition()->relation('images')); $relation = Gallery::definition()->relation('images'); expect($relation->counterpart())->toBe(Image::definition()->relation('gallery')); }); it("returns the counterpart relationship for belongsTo/hasOne relations", function () { $relation = GalleryDetail::definition()->relation('gallery'); expect($relation->counterpart())->toBe(Gallery::definition()->relation('detail')); $relation = Gallery::definition()->relation('detail'); expect($relation->counterpart())->toBe(GalleryDetail::definition()->relation('gallery')); }); it("returns the counterpart relationship for hasMany/hasMany relations", function () { $relation = Image::definition()->relation('tags'); expect($relation->counterpart())->toBe(Tag::definition()->relation('images')); $relation = Tag::definition()->relation('images'); expect($relation->counterpart())->toBe(Image::definition()->relation('tags')); }); it("throws an exception when the counterpart is ambiguous", function () { $schema = Gallery::definition(); $schema->hasMany('images', Image::class, ['keys' => ['id' => 'gallery_id']]); $schema->hasMany('photos', Image::class, ['keys' => ['id' => 'gallery_id']]); $closure = function () { $relation = Image::definition()->relation('gallery'); $relation->counterpart(); }; expect($closure)->toThrow(new ChaosException("Ambiguous belongsTo counterpart relationship for `Chaos\\Spec\\Fixture\\Model\\Image`. Apply the Single Table Inheritance pattern to get unique models.")); }); }); });
expect(Gallery::class)->toReceive('::all')->with(['conditions' => ['id' => [1, 2]]], ['collector' => null, 'return' => 'array']); $belongsTo->embed($images, ['fetchOptions' => ['return' => 'array']]); foreach ($images as $image) { expect($image['gallery']['id'])->toBe($image['gallery_id']); expect($image['gallery'])->toBeAn('array'); } }); }); describe("->get()", function () { it("returns `null` for unexisting foreign key", function () { $image = Image::create(['id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]); expect($image->gallery)->toBe(null); }); it("lazy loads a belongsTo relation", function () { Stub::on(Gallery::class)->method('::all', function ($options = [], $fetchOptions = []) { $galleries = Gallery::create([['id' => 1, 'name' => 'Foo Gallery']], ['type' => 'set', 'exists' => true, 'collector' => $fetchOptions['collector']]); return $galleries; }); $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]); expect(Gallery::class)->toReceive('::all')->with(['conditions' => ['id' => 1]], ['collector' => $image->collector()]); expect($image->gallery->id)->toBe($image->gallery_id); expect($image->gallery->collector())->toBe($image->collector()); }); }); describe("->broadcast()", function () { it("bails out if no relation data hasn't been setted", function () { $belongsTo = Image::definition()->relation('gallery'); $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200']); expect($belongsTo->broadcast($image))->toBe(true); }); it("saves a belongsTo relationship", function () {
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 () { $hasOne = Gallery::relation('detail'); $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]); $gallery->detail = ['description' => 'Foo Gallery Description']; Stub::on($gallery->detail)->method('save', function () use($gallery) { $gallery->detail->id = 1; return true; }); expect($gallery->detail)->toReceive('save'); expect($hasOne->save($gallery))->toBe(true); expect($gallery->detail->gallery_id)->toBe($gallery->id); }); }); });
it("validates by default", function () { $image = Image::create([]); Image::validator()->rule('name', 'not:empty'); expect($image->broadcast())->toBe(false); expect($image->exists())->toBe(false); }); it("validates direct relationships by default", function () { Gallery::validator()->rule('name', 'not:empty'); $image = Image::create(['name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200', 'gallery' => []]); expect($image->broadcast())->toBe(false); expect($image->exists())->toBe(false); }); }); describe("->hierarchy()", function () { it("returns all included relations and sub-relations with non empty data", function () { $gallery = Gallery::create(['name' => 'Gallery1']); $gallery->detail = ['description' => 'Tech']; $image = Image::create(['title' => 'Amiga 1200']); $image->tags[] = ['name' => 'Computer']; $image->tags[] = ['name' => 'Science']; $image->gallery = $gallery; $gallery->images[] = $image; expect($gallery->hierarchy())->toBe(['detail', 'images_tags.tag', 'images.tags']); }); }); describe("->to('array')", function () { it("exports data using `'array'` formatter handlers", function () { $model = $this->model; $schema = $model::definition(); $schema->column('created', ['type' => 'date']); $entity = $model::create(['title' => 'Hello', 'body' => 'World', 'created' => new DateTime('2014-10-26 00:25:15')]);