예제 #1
0
use Bravesheep\Dogmatist\Builder;
use Bravesheep\Dogmatist\Factory;
use Bravesheep\Dogmatist\Field;
use Bravesheep\Dogmatist\Guesser\FieldNameGuesser;
use Bravesheep\Dogmatist\Guesser\TypeResolver;
describe("FieldNameGuesser", function () {
    beforeEach(function () {
        $this->guesser = new FieldNameGuesser();
        $this->dogmatist = Factory::create(\Faker\Factory::DEFAULT_LOCALE, $this->guesser);
    });
    it("should correctly identify the fields", function () {
        $cb = function () {
            return 0;
        };
        TypeResolver::registerMapping('addresses', Field::TYPE_FAKE, ['streetAddress'], [2, 10]);
        TypeResolver::registerMappings(['link_test' => [Field::TYPE_LINK, false, ['test']], 'relation_test' => [Field::TYPE_RELATION, false, ['object']], 'value_test' => [Field::TYPE_VALUE, false, ['value']], 'callback_test' => [Field::TYPE_CALLBACK, false, [$cb]], 'none_test' => [Field::TYPE_NONE, false, []], 'select_test' => [Field::TYPE_SELECT, false, [['a', 'b']]]]);
        /** @var Builder $builder */
        $builder = $this->dogmatist->create('Bravesheep\\Spec\\Named\\NamedTest');
        expect($builder->getFields())->toHaveLength(9);
        expect($builder->get('addresses')->getFakedType())->toBe('streetAddress');
        expect($builder->get('firstName')->getFakedType())->toBe('firstName');
        expect($builder->get('last_name')->getFakedType())->toBe('lastName');
        expect($builder->get('linkTest')->isType(Field::TYPE_LINK))->toBe(true);
        expect($builder->get('relationTest')->isType(Field::TYPE_RELATION))->toBe(true);
        expect($builder->get('valueTest')->isType(Field::TYPE_VALUE))->toBe(true);
        expect($builder->get('callbackTest')->isType(Field::TYPE_CALLBACK))->toBe(true);
        expect($builder->get('noneTest')->isType(Field::TYPE_NONE))->toBe(true);
        expect($builder->get('selectTest')->isType(Field::TYPE_SELECT))->toBe(true);
    });
    it("should not map an unknown field type", function () {
예제 #2
0
<?php

use Bravesheep\Dogmatist\Field;
use Bravesheep\Dogmatist\Guesser\TypeResolver;
describe("TypeResolver", function () {
    it("should register a new mapping", function () {
        TypeResolver::registerMapping('abc', Field::TYPE_FAKE, []);
        expect(TypeResolver::resolve('abc'))->toBe([Field::TYPE_FAKE, false, []]);
    });
    it("should register multiple new mappings", function () {
        TypeResolver::registerMappings(['xyz' => [Field::TYPE_FAKE, false, []], 'wyz' => [Field::TYPE_VALUE, false, ['test']]]);
        expect(TypeResolver::resolve('xyz'))->toBe([Field::TYPE_FAKE, false, []]);
        expect(TypeResolver::resolve('wyz'))->toBe([Field::TYPE_VALUE, false, ['test']]);
    });
    it("should not resolve an unknown type", function () {
        expect(TypeResolver::resolve('some_nonexistant_type'))->toBe(false);
    });
    it("should resolve types starting with is or has to be booleans", function () {
        expect(TypeResolver::resolve('isTrue'))->toBe([Field::TYPE_FAKE, false, ['boolean']]);
        expect(TypeResolver::resolve('has_value'))->toBe([Field::TYPE_FAKE, false, ['boolean']]);
    });
    it("should resolve types ending in at to be datetimes", function () {
        expect(TypeResolver::resolve("updatedAt"))->toBe([Field::TYPE_FAKE, false, ['dateTime']]);
        expect(TypeResolver::resolve("created_at"))->toBe([Field::TYPE_FAKE, false, ['dateTime']]);
        expect(TypeResolver::resolve("updatedat"))->toBe(false);
    });
    it("should resolve different styles of first name to the same type", function () {
        expect(TypeResolver::resolve('firstname'))->toBe([Field::TYPE_FAKE, false, ['firstName']]);
        expect(TypeResolver::resolve('first_name'))->toBe([Field::TYPE_FAKE, false, ['firstName']]);
        expect(TypeResolver::resolve('firstName'))->toBe([Field::TYPE_FAKE, false, ['firstName']]);
    });