Ejemplo n.º 1
0
        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."));
        });
    });
});
Ejemplo n.º 2
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."));
        });
    });
});
Ejemplo n.º 3
0
            $this->schema->formatter('cast', 'id', $handlers['integer']);
            $this->schema->formatter('cast', 'serial', $handlers['integer']);
            $this->schema->formatter('cast', 'integer', $handlers['integer']);
            $this->schema->formatter('cast', 'float', $handlers['float']);
            $this->schema->formatter('cast', 'decimal', $handlers['decimal']);
            $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']);
            $this->schema->bind('gallery', ['relation' => 'belongsTo', 'to' => Gallery::class, 'keys' => ['gallery_id' => 'id']]);
            $this->schema->bind('images_tags', ['relation' => 'hasMany', 'to' => ImageTag::class, 'keys' => ['id' => 'image_id']]);
            $this->schema->bind('tags', ['relation' => 'hasManyThrough', 'to' => Tag::class, 'through' => 'images_tags', 'using' => 'tag']);
        });
        afterEach(function () {
            Image::reset();
        });
        it("gets/sets the conventions", 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)->toBeAn('integer');
            expect($image->gallery_id)->toBeAn('integer');
            expect($image->name)->toBeA('string');
            expect($image->title)->toBeA('string');
            expect($image->score)->toBeA('float');
            expect($image->tags)->toBeAnInstanceOf('chaos\\collection\\Through');
            expect($image->tags[0])->toBeAnInstanceOf('chaos\\spec\\fixture\\model\\Tag');
            expect($image->tags[1])->toBeAnInstanceOf('chaos\\spec\\fixture\\model\\Tag');
        });
    });
});
Ejemplo n.º 4
0
         $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');
     });
Ejemplo n.º 5
0
                }
            }
        });
    });
    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);
        });
    });
});
Ejemplo n.º 6
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']]]);
        });
    });
});
Ejemplo n.º 7
0
     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);
Ejemplo n.º 8
0
                }
            }
        });
    });
    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("->save()", function () {
        it("bails out on save since it's just an alias", function () {
            $hasManyThrough = Image::relation('tags');
            expect($hasManyThrough->save(null))->toBe(true);
        });
    });
});
Ejemplo n.º 9
0
            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);
        });
    });
});