Example #1
0
 function testSelfChecker()
 {
     $class = ClassBuilder::i()->registerOne('
         class Pants implements \\Fulfil\\Lang\\SelfChecker {
             function check(\\Fulfil\\Context $ctx) {
                 $ctx->addReason($this, ["id"=>"nope"]);
             }
         }
     ');
     $obj = new $class();
     $check = new Instance(['class' => $class]);
     $ret = $check->apply($obj, $ctx = new \Fulfil\Context());
     $this->assertSame($obj, $ret);
     $flat = $ctx->flatten();
     $this->assertFalse($flat->valid);
     $this->assertCount(1, $flat->messages);
     $this->assertEquals(['nope'], $flat->messages[0]->id);
 }
Example #2
0
    function testNoteGetters()
    {
        $class = ClassBuilder::i()->registerOne('
            class PropObj {
                private $foo = "a";
                private $bar = "b";
                private $baz = "c";

                /** :prop = true; */ public function getFoo() { return $this->foo; }
                function setFoo($v) { $this->foo = $v; }

                /** :prop = true; */ public function getBar() { return $this->bar; }
                function setBar($v) { $this->bar = $v; }

                public function getBaz() { return $this->baz; }
            }
        ');
        $o = new $class();
        $n = new \Fulfil\Mapper\Note();
        $this->assertEquals((object) ['foo' => 'a', 'bar' => 'b'], $n->mapObjectToProperties($o));
        $n->populateObject($o, (object) ['foo' => 'z', 'bar' => 'x']);
        $this->assertEquals((object) ['foo' => 'z', 'bar' => 'x'], $n->mapObjectToProperties($o));
    }
Example #3
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]);
    }
Example #4
0
 function testSchemaMapperPopulateWithUnknownProperty()
 {
     $class = ClassBuilder::i()->registerOne('
         class PropObj {
             public $foo = "a";
             public $bar = "b";
             public function populate($in) {
                 foreach ($in as $k=>$v) {
                     if (!isset($this->$k)) {
                         throw new \\Exception();
                     }
                     $this->$k = $v;
                 }
             }
         }
     ');
     $schema = new Schema(['props' => ['foo' => new Check\String_(), 'bar' => new Check\String_()], 'filterUnknownProps' => true]);
     $in = ['foo' => 'yep', 'bar' => 'yep', 'baz' => 'yep'];
     $out = $schema->apply($in, $ctx = new \Fulfil\Context());
     $flat = $ctx->flatten();
     $this->assertFalse($flat->valid);
     $this->assertCount(1, $flat->messages);
     $this->assertEquals(['schema', 'propUnknown'], $flat->messages[0]->id);
     $obj = new $class();
     $obj->populate($out);
 }