Exemplo n.º 1
0
    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."));
        });
    });
});
Exemplo n.º 2
0
        });
    });
    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']]]);
        });
    });
});
Exemplo n.º 3
0
         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');
Exemplo n.º 4
0
         $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']);