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()); }); }); describe("->broadcast()", function () { it("bails out on save since it's just an alias", function () { $hasManyThrough = Image::definition()->relation('tags');
}); }); describe("->validates()", function () { it("returns `true` when no validation error occur", function () { $image = Image::create(); $image->tags[] = Tag::create(); $image->tags[] = Tag::create(); expect($image->tags->validates())->toBe(true); }); it("returns `false` when a validation error occurs", function () { $validator = Tag::validator(); $validator->rule('name', 'not:empty'); $image = Image::create(); $image->tags[] = Tag::create(); $image->tags[] = Tag::create(); expect($image->tags->validates())->toBe(false); expect($image->tags->errors())->toBe([['name' => ['is required']], ['name' => ['is required']]]); }); }); describe("->errors()", function () { it("returns errors", function () { $validator = Tag::validator(); $validator->rule('name', 'not:empty'); $image = Image::create(); $image->tags[] = Tag::create(); $image->tags[] = Tag::create(); expect($image->validates())->toBe(false); expect($image->tags->errors())->toBe([['name' => ['is required']], ['name' => ['is required']]]); }); }); });