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.")); }); }); });
$this->schema->formatter('cast', 'date', $handlers['date']); $this->schema->formatter('cast', 'datetime', $handlers['datetime']); $this->schema->formatter('cast', 'boolean', $handlers['boolean']); $this->schema->formatter('cast', 'null', $handlers['null']); $this->schema->formatter('cast', 'string', $handlers['string']); $this->schema->formatter('cast', '_default_', $handlers['string']); }); it("casts a nested entity data", function () { $image = $this->schema->cast(null, ['id' => '1', 'gallery_id' => '2', 'name' => 'image.jpg', 'title' => 'My Image', 'score' => '8.9', 'tags' => [['id' => '1', 'name' => 'landscape'], ['id' => '2', 'name' => 'mountain']]]); expect($image->id)->toBe(1); expect($image->gallery_id)->toBe(2); expect($image->name)->toBe('image.jpg'); expect($image->title)->toBe('My Image'); expect($image->score)->toBe(8.9); expect($image->tags)->toBeAnInstanceOf('Chaos\\Collection\\Through'); expect($image->tags->schema())->toBe(Tag::definition()); expect($image->tags[0]->data())->toEqual(['id' => '1', 'name' => 'landscape']); expect($image->tags[1]->data())->toEqual(['id' => '2', 'name' => 'mountain']); }); }); describe(".format()", function () { beforeEach(function () { $this->schema = new Schema(); $this->schema->column('id', ['type' => 'serial']); $this->schema->column('name', ['type' => 'string']); $this->schema->column('null', ['type' => 'string']); $this->schema->column('value', ['type' => 'integer']); $this->schema->column('double', ['type' => 'float']); $this->schema->column('revenue', ['type' => 'decimal', 'length' => 20, 'precision' => 2]); $this->schema->column('active', ['type' => 'boolean']); $this->schema->column('registered', ['type' => 'date']);