예제 #1
0
 public function testManipulatesCheckers()
 {
     $collec = new CheckerCollection();
     $collec->flush();
     $this->assertEquals(0, count($collec));
     $this->assertEquals(0, count($collec->getAll()));
     $this->assertEquals(0, count(CheckerCollection::getInstance()));
     $this->assertEquals(0, count(CheckerCollection::getInstance()->getAll()));
     $checker1 = m::mock('\\checkTypes\\CheckerInterface');
     $this->assertInstanceof('\\checkTypes\\CheckerCollection', $collec->add('foo', $checker1));
     $this->assertEquals(1, count($collec));
     $this->assertEquals(1, count($collec->getAll()));
     $this->assertEquals(1, count(CheckerCollection::getInstance()));
     $this->assertEquals(1, count(CheckerCollection::getInstance()->getAll()));
     $collec->add('foo', $checker1);
     // same name so no change
     $this->assertEquals(1, count($collec));
     $this->assertEquals(1, count($collec->getAll()));
     $this->assertEquals(array('foo' => $checker1), $collec->getAll());
     $checker2 = m::mock('\\checkTypes\\CheckerInterface');
     $collec->add('bar', $checker2);
     $this->assertEquals(2, count($collec));
     $this->assertEquals(2, count($collec->getAll()));
     $this->assertEquals(array('foo' => $checker1, 'bar' => $checker2), $collec->getAll());
     $this->assertEquals($checker1, $collec->get('foo'));
     $this->assertEquals($checker2, $collec->get('bar'));
     $this->assertInstanceof('\\checkTypes\\CheckerCollection', $collec->remove('foo'));
     $this->assertEquals(1, count($collec));
     $this->assertEquals(1, count(CheckerCollection::getInstance()));
     $this->assertNull($collec->get('foo'));
     $collec2 = new CheckerCollection();
     $this->assertEquals(1, count($collec2));
     $this->assertEquals($checker2, $collec2->get('bar'));
 }
예제 #2
0
 protected function setUp()
 {
     $this->handler = new Handler(new CheckerCollection(), ValidatorSingleton::getInstance());
     ValidatorSingleton::getInstance()->setNextValidator(new \checkTypes\Validator\MultiTypeValidator())->setNextValidator(new \checkTypes\Validator\TypeValidator())->setNextValidator(new \checkTypes\Validator\ObjectValidator())->setNextValidator(new \checkTypes\Validator\DefaultValueValidator());
     CheckerCollection::getInstance()->add('mixed', new \checkTypes\Checker\CheckerMixed())->add('boolean', new \checkTypes\Checker\CheckerBoolean())->add('bool', new \checkTypes\Checker\CheckerBoolean())->add('integer', new \checkTypes\Checker\CheckerInteger())->add('int', new \checkTypes\Checker\CheckerInteger())->add('double', new \checkTypes\Checker\CheckerFloat())->add('float', new \checkTypes\Checker\CheckerFloat())->add('string', new \checkTypes\Checker\CheckerString())->add('array', new \checkTypes\Checker\CheckerArray())->add('resource', new \checkTypes\Checker\CheckerResource())->add('callable', new \checkTypes\Checker\CheckerCallable())->add('self', new \checkTypes\Checker\CheckerSelf())->add('parent', new \checkTypes\Checker\CheckerParent());
 }
예제 #3
0
<?php

namespace checkTypes;

use checkTypes\Validator\ValidatorSingleton;
function check($strict = false)
{
    $trace = next(debug_backtrace(0));
    if (!is_array($trace)) {
        throw new \LogicException('Invalid context');
    }
    if (isset($trace['class'])) {
        $reflection = new \ReflectionMethod($trace['class'], $trace['function']);
    } else {
        $reflection = new \ReflectionFunction($trace['function']);
    }
    $handler = new Handler(new CheckerCollection(), ValidatorSingleton::getInstance());
    try {
        $handler->check($reflection, $trace['args'], $strict);
    } catch (\InvalidArgumentException $e) {
        throw new \InvalidArgumentException(sprintf('%s, called in %s on line %d and defined in %s on line %d', $e->getMessage(), $trace['file'], $trace['line'], $reflection->getFileName(), $reflection->getStartLine()));
    }
}
function next($array)
{
    return \next($array);
}
ValidatorSingleton::getInstance()->setNextValidator(new Validator\MultiTypeValidator())->setNextValidator(new Validator\TypeValidator())->setNextValidator(new Validator\ObjectValidator())->setNextValidator(new Validator\DefaultValueValidator());
CheckerCollection::getInstance()->add('mixed', new Checker\CheckerMixed())->add('boolean', new Checker\CheckerBoolean())->add('bool', new Checker\CheckerBoolean())->add('integer', new Checker\CheckerInteger())->add('int', new Checker\CheckerInteger())->add('double', new Checker\CheckerFloat())->add('float', new Checker\CheckerFloat())->add('string', new Checker\CheckerString())->add('array', new Checker\CheckerArray())->add('resource', new Checker\CheckerResource())->add('callable', new Checker\CheckerCallable())->add('self', new Checker\CheckerSelf())->add('parent', new Checker\CheckerParent());