示例#1
0
use Chaos\ChaosException;
use Chaos\Model;
use Chaos\Relationship;
use Chaos\Relationship\HasOne;
use Chaos\Conventions;
use Kahlan\Plugin\Stub;
use Chaos\Spec\Fixture\Model\Gallery;
use Chaos\Spec\Fixture\Model\GalleryDetail;
describe("HasOne", function () {
    beforeEach(function () {
        $this->conventions = new Conventions();
        $this->key = $this->conventions->apply('key');
    });
    afterEach(function () {
        Gallery::reset();
        GalleryDetail::reset();
    });
    describe("->__construct()", function () {
        it("creates a hasOne relationship", function () {
            $relation = new HasOne(['from' => Gallery::class, 'to' => GalleryDetail::class]);
            expect($relation->name())->toBe($this->conventions->apply('field', GalleryDetail::class));
            $foreignKey = $this->conventions->apply('reference', Gallery::class);
            expect($relation->keys())->toBe([$this->key => $foreignKey]);
            expect($relation->from())->toBe(Gallery::class);
            expect($relation->to())->toBe(GalleryDetail::class);
            expect($relation->link())->toBe(Relationship::LINK_KEY);
            expect($relation->fields())->toBe(true);
            expect($relation->conventions())->toBeAnInstanceOf('chaos\\Conventions');
        });
        it("throws an exception if `'from'` is missing", function () {
            $closure = function () {