Наследование: extends variable, implements arrayAccess
Пример #1
0
 protected function matches($actual)
 {
     if ($this->analyzer->isInteger($this->expected) === false) {
         throw new \PHPUnit_Framework_Exception('Expected value of ' . __CLASS__ . ' must be an integer');
     }
     switch (true) {
         case $this->analyzer->isArray($actual):
             $asserter = new asserters\phpArray(null, $this->analyzer);
             $asserter->setWith($actual)->hasSize($this->expected);
             break;
         case $actual instanceof \countable:
             $asserter = new asserters\sizeOf(null, $this->analyzer);
             $asserter->setWith($actual)->isEqualTo($this->expected);
             break;
         case $actual instanceof \iteratorAggregate:
             $asserter = new asserters\integer(null, $this->analyzer);
             $asserter->setWith(iterator_count($actual->getIterator()))->isEqualTo($this->expected);
             break;
         case $actual instanceof \traversable:
         case $actual instanceof \iterator:
             $asserter = new asserters\integer(null, $this->analyzer);
             $asserter->setWith(iterator_count($actual))->isEqualTo($this->expected);
             break;
         default:
             throw new \PHPUnit_Framework_Exception('Actual value of ' . __CLASS__ . ' must be an array, a countable object or a traversable object');
     }
 }
Пример #2
0
 protected function matches($actual)
 {
     if ($this->analyzer->isArray($actual) === false) {
         throw new \PHPUnit_Framework_Exception('Actual value of ' . __CLASS__ . ' must be an array');
     }
     $patched = array_replace_recursive($actual, $this->expected);
     $asserter = new asserters\phpArray(null, $this->analyzer);
     $asserter->setWith($actual);
     if ($this->strict) {
         $asserter->isIdenticalTo($patched);
     } else {
         $asserter->isEqualTo($patched);
     }
 }
Пример #3
0
 public function setWith($value)
 {
     parent::setWith($value, false);
     $fail = false;
     $this->adapter->set_error_handler(function () use(&$fail) {
         $fail = true;
     });
     switch (true) {
         case $this->value instanceof \Iterator:
             $value = iterator_to_array($this->value);
             break;
         case $this->getAnalyzer()->isString($value):
             $value = $this->adapter->str_split($value);
             $fail = $value === false;
             break;
         default:
             $value = (array) $value;
     }
     $this->adapter->restore_error_handler();
     if ($fail) {
         $this->fail($this->getLocale()->_('%s could not be converted to array', $this->getTypeOf($this->value)));
     }
     $this->value = $value;
     return $this->pass();
 }
Пример #4
0
 protected function matches($actual)
 {
     if (is_null($this->expected)) {
         throw new \PHPUnit_Framework_Exception('Expected value of ' . __CLASS__ . ' must not be null');
     }
     if ($this->analyzer->isArray($actual) === false && $actual instanceof \arrayAccess === false) {
         throw new \PHPUnit_Framework_Exception('Actual value of ' . __CLASS__ . ' must be an array or an arrayAccess instance');
     }
     if ($this->analyzer->isArray($actual)) {
         $asserter = new asserters\phpArray(null, $this->analyzer);
         $asserter->setWith($actual)->hasKey($this->expected);
     } else {
         $asserter = new asserters\boolean(null, $this->analyzer);
         try {
             $asserter->setWith(isset($actual[$this->expected]))->isTrue();
         } catch (exception $exception) {
             throw new exception($asserter, $this->analyzer->getTypeOf($actual) . ' has no key ' . $this->analyzer->getTypeOf($this->expected));
         }
     }
 }
Пример #5
0
 protected function matches($actual)
 {
     $expected = $this->expected;
     switch (true) {
         case $this->analyzer->isArray($actual):
             $asserter = new asserters\phpArray(null, $this->analyzer);
             $asserter->setWith($actual);
             break;
         case $actual instanceof \iterator:
             $asserter = new asserters\iterator(null, $this->analyzer);
             $asserter = $asserter->setWith($actual)->toArray;
             break;
         case $this->analyzer->isString($actual):
             $asserter = new asserters\phpString(null, $this->analyzer);
             if ($this->ignoreCase) {
                 $actual = strtolower($actual);
                 $expected = strtolower($expected);
             }
             $asserter->setWith($actual);
             break;
         default:
             throw new \PHPUnit_Framework_Exception('Actual value of ' . __CLASS__ . ' must be an array, a string or a traversable object');
     }
     try {
         if ($this->analyzer->isObject($expected)) {
             if ($this->checkForObjectIdentity) {
                 $asserter->strictlyContains($expected);
             } else {
                 $asserter->contains($expected);
             }
         } else {
             if ($this->checkForNonObjectIdentity) {
                 $asserter->strictlyContains($expected);
             } else {
                 $asserter->contains($expected);
             }
         }
     } catch (exception $exception) {
         throw new exception($asserter, $this->analyzer->getTypeOf($actual) . ' does not contain ' . $this->analyzer->getTypeOf($expected));
     }
 }
Пример #6
0
 public function testIsNotIdenticalTo()
 {
     $this->if($asserter = new sut($generator = new asserter\generator()))->then->exception(function () use($asserter) {
         $asserter->isNotIdenticalTo(new \mock\object());
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Array is undefined')->if($asserter->setWith($array = array(1, 2)))->then->object($asserter->isNotIdenticalTo(array('1', 2)))->isIdenticalTo($asserter)->exception(function () use($asserter, $array) {
         $asserter->isNotIdenticalTo($array);
     })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('%s is identical to %s'), $asserter, $asserter->getTypeOf($array)))->if($asserter->integer)->then->object($asserter->isNotIdenticalTo(array('1', 2)))->isIdenticalTo($asserter)->if($asserter->integer[1])->then->object($asserter->isEqualTo(2))->isIdenticalTo($asserter)->object($asserter->isEqualTo(2)->isNotEqualTo(5))->isIdenticalTo($asserter)->if($diff = new diffs\variable())->then->exception(function () use($asserter, &$expectedValue) {
         $asserter->isEqualTo($expectedValue = rand(3, PHP_INT_MAX));
     })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('%s is not equal to %s'), $asserter->getTypeOf($asserter->getInnerValue()), $asserter->getTypeOf($expectedValue)) . PHP_EOL . $diff->setExpected($expectedValue)->setActual(2));
 }
Пример #7
0
 public function testHasKeys()
 {
     $this->if($asserter = new asserters\phpArray($generator = new asserter\generator()))->then->boolean($asserter->wasSet())->isFalse()->exception(function () use($asserter) {
         $asserter->hasSize(rand(0, PHP_INT_MAX));
     })->isInstanceOf('mageekguy\\atoum\\exceptions\\logic')->hasMessage('Array is undefined')->if($asserter->setWith(array()))->then->exception(function () use($asserter) {
         $asserter->hasKeys(array(0));
     })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('%s should have keys %s'), $asserter, $asserter->getTypeOf(array(0))))->if($asserter->setWith(array(uniqid(), uniqid(), uniqid(), uniqid(), uniqid())))->then->exception(function () use($asserter) {
         $asserter->hasKeys(array(0, 'first', 2, 'second'));
     })->isInstanceOf('mageekguy\\atoum\\asserter\\exception')->hasMessage(sprintf($generator->getLocale()->_('%s should have keys %s'), $asserter, $asserter->getTypeOf(array('first', 'second'))))->object($asserter->hasKeys(array(0, 2, 4)))->isIdenticalTo($asserter);
 }
Пример #8
0
 public function __construct(asserters\phpArray $parent = null)
 {
     parent::__construct($parent->getGenerator(), $parent->getAnalyzer(), $parent->getLocale());
     $this->setWithParent($parent);
 }