Esempio n. 1
0
 afterEach(function () {
     Gallery::reset();
     Image::reset();
 });
 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();
         };
Esempio n. 2
0
             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 () {