Ejemplo n.º 1
0
 function testUnknownCheckImportFailsWhenIncompleteDisallowed()
 {
     $registry = Registry::standard();
     $registry->check = [];
     $this->setExpectedException(\InvalidArgumentException::class, 'Unknown check');
     $registry->import('check', ['check' => 'whabadoo']);
 }
Ejemplo n.º 2
0
 function testMapFormSchema()
 {
     $registry = \Fulfil\Registry::standard();
     $check = new \Fulfil\Ext\FormSchema(['schemas' => new \Fulfil\Schema(['props' => ['foo' => new \Fulfil\Schema(['props' => ['bar' => true]])]], ['mapper' => new \Fulfil\Mapper\Vars()])], ['registry' => $registry]);
     $in = (object) ['foo' => (object) ['bar' => 'yep']];
     $out = $check->map($in);
     $this->assertEquals(['foo' => ['bar' => 'yep']], $out);
 }
Ejemplo n.º 3
0
 function testImportExport()
 {
     $in = ['check' => 'all', 'checks' => [['check' => 'basic'], ['check' => 'basic']]];
     $check = Registry::standard()->import('check', $in);
     $expected = new Check\All(['checks' => [new Check\Basic(), new Check\Basic()]]);
     $this->assertEquals($expected, $check);
     $this->assertEquals($in, $check->export());
 }
Ejemplo n.º 4
0
 function __construct(\Fulfil\Registry $registry = null)
 {
     if ($registry) {
         $this->registry = $registry;
     } else {
         $this->registry = \Fulfil\Registry::standard();
         $this->registry->specTest = ['test' => TestInfo::class, 'case' => TestCaseInfo::class];
         $this->registry->check['decimal'] = ['class' => \Fulfil\Check\Decimal::class, 'config' => ['emitString' => true]];
     }
 }
Ejemplo n.º 5
0
 function testImportWithClosureDeps()
 {
     $registry = Registry::standard();
     $registry->check['test'] = ['class' => TestDepCheck::class, 'deps' => ['a' => 'a', 'b' => 'b']];
     $registry->deps['a'] = function ($inReg) use($registry) {
         $this->assertSame($registry, $inReg);
         return 'foo';
     };
     $registry->deps['b'] = function () {
         return 'bar';
     };
     $out = $registry->import('check', ['check' => 'test']);
     $this->assertInstanceOf(TestDepCheck::class, $out);
     $this->assertEquals('foo', $out->a);
     $this->assertEquals('bar', $out->b);
 }
Ejemplo n.º 6
0
function main()
{
    global $argv;
    $registry = \Fulfil\Registry::standard();
    $pdo = new \PDO(getenv('JUNK_DB_DSN'), getenv('JUNK_DB_USER'), getenv('JUNK_DB_PASS'), [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
    $reader = new \Fulfil\Ext\MySqlReader($registry, $pdo);
    $schema = $reader->tableSchema($argv[1], $argv[2]);
    var_dump($schema);
    $ctx = new \Fulfil\Context();
    $in = ['foo' => 'a', 'bar' => '2', 'baz' => ['zz', 'yy']];
    var_dump($schema->apply($in, $ctx));
    $msg = new \Fulfil\MessageSet\Developer();
    foreach ($msg->formatContext($ctx) as $text) {
        echo "{$text}\n";
    }
}
Ejemplo n.º 7
0
    function testAnnotatedClass()
    {
        $class = ClassBuilder::i()->registerOne('
            /**
             * :fulfil = {"rules": [{"rule": "equal", "left": "foo", "right": "bar"}]};
             */
            class Pants {
                /** :fulfil = [{"check": "string", "required": true}]; */
                public $foo;

                /** :fulfil = [{"check": "string", "required": true}]; */
                public $bar;
            }
        ');
        $registry = \Fulfil\Registry::standard();
        $anno = new AnnotatedClass(['class' => $class], ['registry' => $registry]);
        $schema = $anno->getSchema();
        $this->assertCount(1, $schema->props['foo']);
        $this->assertInstanceOf(\Fulfil\Check\String_::class, $schema->props['foo'][0]);
        $this->assertCount(1, $schema->props['bar']);
        $this->assertInstanceOf(\Fulfil\Check\String_::class, $schema->props['bar'][0]);
        $this->assertCount(1, $schema->rules);
        $this->assertInstanceOf(\Fulfil\Rule\Equal::class, $schema->rules[0]);
    }
Ejemplo n.º 8
0
$schema = $options['<schema>'];
$data = $options['<data>'];
read:
if ($schema == '-' || $data == '-') {
    $in = file_get_contents('php://stdin');
    if ($schema == '-' && $data == '-') {
        $schema = $in['schema'];
        $data = $in['data'];
    } elseif ($schema == '-') {
        $schema = $in;
    } elseif ($data == '-') {
        $data = $in;
    }
}
apply:
$registry = \Fulfil\Registry::standard();
$schema = json_decode($schema, !!'assoc');
if ($schema === null && ($err = json_last_error())) {
    die("Invalid schema\n");
}
$data = json_decode($data, !!'assoc');
if ($data === null && ($err = json_last_error())) {
    die("Invalid data\n");
}
$schema = $registry->import('check', $schema);
$ctx = new \Fulfil\Context();
$data = $schema->apply($data, $ctx);
$flat = $ctx->flatten();
if ($flat->valid) {
    echo "OK\n";
} else {
Ejemplo n.º 9
0
 private function createFormSchema($check, $config = [])
 {
     $fs = new FormSchema(['schemas' => new \Fulfil\Schema(['props' => ['foo' => $check]])] + $config, ['registry' => \Fulfil\Registry::standard()]);
     return $fs;
 }
Ejemplo n.º 10
0
 function testDeepClone()
 {
     $schema = new \Fulfil\Schema(['props' => ['a' => new \Fulfil\Check\List_(['defaultItem' => new \Fulfil\Check\String_()])]]);
     $registry = \Fulfil\Registry::standard();
     $cloned = $registry->deepClone($schema);
     $this->assertEquals($schema, $cloned);
     $this->assertNotSame($schema, $cloned);
     $this->assertNotSame($schema->props['a'][0], $cloned->props['a'][0]);
     $this->assertNotSame($schema->props['a'][0]->defaultItem, $cloned->props['a'][0]->defaultItem);
 }
Ejemplo n.º 11
0
 function setUp()
 {
     $this->registry = Registry::standard();
     $this->reader = new FormReader($this->registry);
 }
Ejemplo n.º 12
0
<?php

require __DIR__ . '/config.php';
goto defs;
script:
$fr = new Fulfil\Ext\FormReader(\Fulfil\Registry::standard());
$fr->handlePHPBrackets = true;
$forms = $fr->readHTML(form());
if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $schema = $forms[0]->schema;
    $values = $_POST ?: $schema->initial();
    if ($_POST) {
        $ctx = new Fulfil\Context();
        $values = $schema->apply($values, $ctx);
        $flat = $ctx->flatten();
        if (!$flat->valid) {
            echo '<ul>';
            foreach ($flat->messages as $message) {
                echo '<li>';
                echo implode('.', $message->path) . ': ';
                echo $message->msg . "\n";
            }
            echo '</ul>';
        }
    }
    echo "<pre>" . json_encode($values, JSON_PRETTY_PRINT) . "</pre>";
    echo form($values);
    echo "<pre>";
    foreach ($forms as $form) {
        echo json_encode($form->schema->export(), JSON_PRETTY_PRINT) . "\n";
    }