Example #1
0
     Stub::on($gallery->images[0])->method('broadcast', function () use($gallery) {
         $gallery->images[0]->id = 1;
         return true;
     });
     expect($gallery->images[0])->toReceive('broadcast');
     expect($hasMany->broadcast($gallery))->toBe(true);
     expect($gallery->images[0]->gallery_id)->toBe($gallery->id);
 });
 it("assures removed association to be unsetted", function () {
     $toUnset = Image::create(['id' => 2, 'gallery_id' => 1, 'title' => 'Srinivasa Ramanujan'], ['exists' => true]);
     $toKeep = Image::create(['id' => 3, 'gallery_id' => 1, 'title' => 'Las Vegas'], ['exists' => true]);
     Stub::on(Image::class)->method('::all', function ($options = [], $fetchOptions = []) use($toUnset, $toKeep) {
         $images = Image::create([$toUnset, $toKeep], ['type' => 'set']);
         return $images;
     });
     $hasMany = Gallery::definition()->relation('images');
     $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]);
     $gallery->images = [['title' => 'Amiga 1200'], $toKeep];
     Stub::on($gallery->images[0])->method('broadcast', function () use($gallery) {
         $gallery->images[0]->id = 1;
         return true;
     });
     Stub::on($toKeep)->method('broadcast', function () {
         return true;
     });
     Stub::on($toUnset)->method('broadcast', function () use($toUnset) {
         return true;
     });
     expect($gallery->images[0])->toReceive('broadcast');
     expect($toKeep)->toReceive('broadcast');
     expect($toUnset)->toReceive('broadcast');
Example #2
0
    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."));
        });
    });
});
Example #3
0
        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("->broadcast()", function () {
        it("bails out if no relation data hasn't been setted", function () {
            $hasOne = Gallery::definition()->relation('detail');
            $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]);
            expect($hasOne->broadcast($gallery))->toBe(true);
        });
        it("saves a hasOne relationship", function () {
            $hasOne = Gallery::definition()->relation('detail');
            $gallery = Gallery::create(['id' => 1, 'name' => 'Foo Gallery'], ['exists' => true]);
            $gallery->detail = ['description' => 'Foo Gallery Description'];
            Stub::on($gallery->detail)->method('broadcast', function () use($gallery) {
                $gallery->detail->id = 1;
                return true;
            });
            expect($gallery->detail)->toReceive('broadcast');
            expect($hasOne->broadcast($gallery))->toBe(true);
            expect($gallery->detail->gallery_id)->toBe($gallery->id);
        });
    });
});