コード例 #1
0
                }
            }
        });
    });
    describe("->get()", function () {
        it("lazy loads a belongsTo relation", function () {
            Stub::on(ImageTag::class)->method('::all', function ($options = [], $fetchOptions = []) {
                $imagesTags = ImageTag::create([['id' => 1, 'image_id' => 1, 'tag_id' => 1], ['id' => 2, 'image_id' => 1, 'tag_id' => 3]], ['type' => 'set', 'exists' => true, 'collector' => $fetchOptions['collector']]);
                return $imagesTags;
            });
            Stub::on(Tag::class)->method('::all', function ($options = [], $fetchOptions = []) {
                $tags = Tag::create([['id' => 1, 'name' => 'High Tech'], ['id' => 3, 'name' => 'Computer']], ['type' => 'set', 'exists' => true, 'collector' => $fetchOptions['collector']]);
                return $tags;
            });
            $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]);
            expect(ImageTag::class)->toReceive('::all')->with(['conditions' => ['image_id' => 1]], ['collector' => $image->collector()]);
            expect(Tag::class)->toReceive('::all')->with(['conditions' => ['id' => [1, 3]]], ['collector' => $image->collector()]);
            expect(count($image->tags))->toBe(2);
            expect($image->tags[0]->data())->toBe(['id' => 1, 'name' => 'High Tech']);
            expect($image->tags[1]->data())->toBe(['id' => 3, 'name' => 'Computer']);
            expect($image->tags[0]->collector())->toBe($image->collector());
            expect($image->tags[1]->collector())->toBe($image->collector());
        });
    });
    describe("->save()", function () {
        it("bails out on save since it's just an alias", function () {
            $hasManyThrough = Image::relation('tags');
            expect($hasManyThrough->save(null))->toBe(true);
        });
    });
});
コード例 #2
0
ファイル: HasManySpec.php プロジェクト: ssgonchar/chaos
     expect($gallery->images[0])->toReceive('save');
     expect($toKeep)->toReceive('save');
     expect($toUnset)->toReceive('save');
     expect($hasMany->save($gallery))->toBe(true);
     expect($toUnset->exists())->toBe(true);
     expect($toUnset->gallery_id)->toBe(null);
     expect($gallery->images[0]->gallery_id)->toBe($gallery->id);
 });
 it("assures removed associative entity to be deleted", function () {
     $toDelete = ImageTag::create(['id' => 5, 'image_id' => 4, 'tag_id' => 6], ['exists' => true]);
     $toKeep = ImageTag::create(['id' => 6, 'image_id' => 4, 'tag_id' => 3], ['exists' => true]);
     Stub::on(ImageTag::class)->method('::all', function ($options = [], $fetchOptions = []) use($toDelete, $toKeep) {
         $images = ImageTag::create([$toDelete, $toKeep], ['type' => 'set']);
         return $images;
     });
     $hasMany = Image::relation('images_tags');
     $image = Image::create(['id' => 4, 'gallery_id' => 2, 'title' => 'Silicon Valley'], ['exists' => true]);
     $image->images_tags = [['tag_id' => 1], $toKeep];
     Stub::on($image->images_tags[0])->method('save', function () use($image) {
         $image->images_tags[0]->id = 7;
         return true;
     });
     $schema = ImageTag::schema();
     Stub::on($schema)->method('delete', function () {
         return true;
     });
     expect($image->images_tags[0])->toReceive('save');
     expect($toKeep)->toReceive('save');
     expect($schema)->toReceive('delete')->with(['id' => 5]);
     expect($hasMany->save($image))->toBe(true);
     expect($toDelete->exists())->toBe(false);
コード例 #3
0
ファイル: BelongsToSpec.php プロジェクト: ssgonchar/chaos
        it("bails out if no relation data hasn't been setted", function () {
            $belongsTo = Image::relation('gallery');
            $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200']);
            expect($belongsTo->save($image))->toBe(true);
        });
        it("saves a belongsTo relationship", function () {
            $belongsTo = Image::relation('gallery');
            $image = Image::create(['id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]);
            $image->gallery = ['name' => 'Foo Gallery'];
            Stub::on($image->gallery)->method('save', function () use($image) {
                $image->gallery->id = 1;
                return true;
            });
            expect($image->gallery)->toReceive('save');
            expect($belongsTo->save($image))->toBe(true);
            expect($image->gallery_id)->toBe($image->gallery->id);
        });
        it("throws an exception if the saves relation didn't populate any ID", function () {
            $closure = function () {
                $belongsTo = Image::relation('gallery');
                $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]);
                $image->gallery = ['name' => 'Foo Gallery'];
                Stub::on($image->gallery)->method('save', function () {
                    return true;
                });
                $belongsTo->save($image);
            };
            expect($closure)->toThrow(new ChaosException("The `'id'` key is missing from related data."));
        });
    });
});