Exemple #1
0
            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);
            expect($image->images_tags[0]->image_id)->toBe($image->id);
        });
    });
});
         $images = $images->data();
         expect(ImageTag::class)->toReceive('::all')->with(['conditions' => ['image_id' => [1, 2, 3, 4, 5]]], ['collector' => null, 'return' => 'array']);
         expect(Tag::class)->toReceive('::all')->with(['conditions' => ['id' => [1, 3, 5, 6]]], ['collector' => null, 'return' => 'array']);
         $hasManyThrough->embed($images, ['fetchOptions' => ['return' => 'array']]);
         foreach ($images as $image) {
             foreach ($image['images_tags'] as $index => $image_tag) {
                 expect($image_tag['tag'])->toBe($image['tags'][$index]);
                 expect($image['tags'][$index])->toBeAn('array');
             }
         }
     });
 });
 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());
     });
Exemple #3
0
        });
        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::definition()->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('broadcast', function () use($image) {
                $image->images_tags[0]->id = 7;
                return true;
            });
            $schema = ImageTag::definition();
            Stub::on($toKeep)->method('broadcast', function () {
                return true;
            });
            Stub::on($schema)->method('truncate', function () {
                return true;
            });
            expect($image->images_tags[0])->toReceive('broadcast');
            expect($toKeep)->toReceive('broadcast');
            expect($schema)->toReceive('truncate')->with(['id' => 5]);
            expect($hasMany->broadcast($image))->toBe(true);
            expect($toDelete->exists())->toBe(false);
            expect($image->images_tags[0]->image_id)->toBe($image->id);
        });
    });
});