it("bails out if no relation data hasn't been setted", function () { $belongsTo = Image::definition()->relation('gallery'); $image = Image::create(['id' => 1, 'gallery_id' => 1, 'title' => 'Amiga 1200']); expect($belongsTo->broadcast($image))->toBe(true); }); it("saves a belongsTo relationship", function () { $belongsTo = Image::definition()->relation('gallery'); $image = Image::create(['id' => 1, 'title' => 'Amiga 1200'], ['exists' => true]); $image->gallery = ['name' => 'Foo Gallery']; Stub::on($image->gallery)->method('broadcast', function () use($image) { $image->gallery->id = 1; return true; }); expect($image->gallery)->toReceive('broadcast'); expect($belongsTo->broadcast($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::definition()->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('broadcast', function () { return true; }); $belongsTo->broadcast($image); }; expect($closure)->toThrow(new ChaosException("The `'id'` key is missing from related data.")); }); }); });
$model = $this->model; $schema = $model::definition(); $schema->column('created', ['type' => 'date']); $entity = $model::create(['title' => 'Hello', 'body' => 'World', 'created' => new DateTime('2014-10-26 00:25:15')]); expect($entity->data())->toBe(['title' => 'Hello', 'body' => 'World', 'created' => '2014-10-26']); }); it("supports recursive structures", function () { $data = ['name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200', 'tags' => [['name' => 'tag1']]]; $image = Image::create($data); foreach ($image->tags as $tag) { $tag->images[] = $image; } expect($image->data())->toBe(['name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200', 'images_tags' => [['tag' => ['name' => 'tag1']]], 'tags' => [['name' => 'tag1']]]); }); it("supports the `'embed'` option", function () { $image = Image::create(['title' => 'Amiga 1200']); $image->tags[] = ['name' => 'Computer']; $image->tags[] = ['name' => 'Science']; $image->gallery = ['name' => 'Gallery 1']; expect($image->to('array'))->toBe(['title' => 'Amiga 1200', 'gallery' => ['name' => 'Gallery 1'], 'images_tags' => [['tag' => ['name' => 'Computer']], ['tag' => ['name' => 'Science']]], 'tags' => [['name' => 'Computer'], ['name' => 'Science']]]); expect($image->to('array', ['embed' => ['gallery']]))->toBe(['title' => 'Amiga 1200', 'gallery' => ['name' => 'Gallery 1']]); expect($image->to('array', ['embed' => false]))->toBe(['title' => 'Amiga 1200']); }); }); describe("->__toString()", function () { it("returns the title field", function () { $data = ['id' => 1, 'title' => 'test record']; $model = $this->model; $entity = $model::create($data); expect((string) $entity)->toBe('test record'); });
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); expect($image->images_tags[0]->image_id)->toBe($image->id);
}); }); 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']]]); }); }); });
expect($this->schema->format('cast', 'unexisting', 123))->toBe(123); }); it("formats according default `'array'` handlers", function () { expect($this->schema->format('array', 'id', 123))->toBe(123); expect($this->schema->format('array', 'value', 123))->toBe(123); expect($this->schema->format('array', 'double', 12.3))->toBe(12.3); expect($this->schema->format('array', 'revenue', 12.3))->toBe('12.3'); $date = DateTime::createFromFormat('Y-m-d', '2014-11-21'); expect($this->schema->format('array', 'registered', $date))->toBe('2014-11-21'); expect($this->schema->format('array', 'registered', '2014-11-21'))->toBe('2014-11-21'); $datetime = DateTime::createFromFormat('Y-m-d H:i:s', '2014-11-21 10:20:45'); expect($this->schema->format('array', 'created', $datetime))->toBe('2014-11-21 10:20:45'); expect($this->schema->format('array', 'created', '2014-11-21 10:20:45'))->toBe('2014-11-21 10:20:45'); expect($this->schema->format('array', 'active', true))->toBe(true); expect($this->schema->format('array', 'active', false))->toBe(false); expect($this->schema->format('array', 'null', null))->toBe(null); expect($this->schema->format('array', 'name', 'abc'))->toBe('abc'); expect($this->schema->format('array', 'unexisting', 123))->toBe('123'); }); }); describe("->save()", function () { it("saves an entity", function () { $data = ['name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200']; $image = Image::create($data); allow($image->schema())->toReceive('bulkInsert')->andReturn(true); allow($image->schema())->toReceive('bulkUpdate')->andReturn(true); expect($image)->toReceive('broadcast')->with(['custom' => 'option', 'embed' => false]); expect($image->save(['custom' => 'option']))->toBe(true); }); }); });