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.")); }); }); });
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.")); }); }); });
expect($entity->body)->toBe('World'); expect($entity->created)->toBe($date); expect($entity)->toHaveLength(3); }); it("sets an array of values", function () { $date = new DateTime('2014-10-26 00:25:15'); $model = $this->model; $entity = $model::create(); expect($entity->set(['title' => 'Hello', 'body' => 'World', 'created' => $date]))->toBe($entity); expect($entity->title)->toBe('Hello'); expect($entity->body)->toBe('World'); expect($entity->created)->toBe($date); expect($entity)->toHaveLength(3); }); it("sets nested arbitraty value in cascade when locked is `false`", function () { Image::definition()->locked(false); $image = Image::create(); $image->set('a.nested.value', 'hello'); expect($image->data())->toEqual(['a' => ['nested' => ['value' => 'hello']]]); }); it("sets a single belongsTo relation", function () { $image = Image::create(); $image->set('gallery', ['id' => '1', 'name' => 'MyGallery']); expect($image->get('gallery') instanceof Gallery)->toBe(true); expect($image->get('gallery')->data())->toEqual(['id' => 1, 'name' => 'MyGallery']); }); it("sets a single hasMany relation", function () { $image = Image::create(); $image->set('images_tags.0', ['id' => '1', 'image_id' => '1', 'tag_id' => '1']); expect($image->get('images_tags') instanceof Collection)->toBe(true); expect($image->get('images_tags.0') instanceof ImageTag)->toBe(true);
} } }); }); 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'); expect($hasManyThrough->broadcast(null))->toBe(true); }); }); });
expect($gallery->images[0])->toReceive('broadcast'); expect($toKeep)->toReceive('broadcast'); expect($toUnset)->toReceive('broadcast'); expect($hasMany->broadcast($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::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');
it("returns defaults", function () { $this->schema->column('name', ['type' => 'string', 'default' => 'Enter The Name Here']); $this->schema->column('title', ['type' => 'string', 'default' => 'Enter The Title Here', 'length' => 50]); expect($this->schema->defaults())->toBe(['name' => 'Enter The Name Here', 'title' => 'Enter The Title Here']); }); describe("->type()", function () { it("returns a field type", function () { expect($this->schema->type('id'))->toBe('serial'); }); }); describe("->column()", function () { beforeEach(function () { $this->schema = new Schema(); }); it("gets the field", function () { $schema = Image::definition(); expect($schema->column('id'))->toBe(['type' => 'serial', 'array' => false, 'null' => false]); expect($schema->column('gallery_id'))->toBe(['type' => 'id', 'array' => false, 'null' => true]); expect($schema->column('name'))->toBe(['type' => 'string', 'array' => false, 'null' => true]); expect($schema->column('title'))->toBe(['type' => 'string', 'length' => 50, 'array' => false, 'null' => true]); expect($schema->column('score'))->toBe(['type' => 'float', 'array' => false, 'null' => true]); }); it("sets a field with a specific type", function () { $this->schema->column('age', ['type' => 'integer']); expect($this->schema->column('age'))->toBe(['type' => 'integer', 'array' => false, 'null' => true]); }); it("sets a field with a specific type using the array syntax", function () { $this->schema->column('age', ['integer']); expect($this->schema->column('age'))->toBe(['type' => 'integer', 'array' => false, 'null' => true]); }); it("sets a field with a specific type using the string syntax", function () {