Example #1
0
File: args.php Project: ihor/Nspl
/**
 * @param mixed $arg
 * @param string $hadTo
 * @param int $atPosition
 * @param string|\Throwable $exception
 * @param bool $has
 * @throws \Throwable
 * @throws string
 */
function throwExpectsException($arg, $hadTo, $atPosition = null, $exception = '\\InvalidArgumentException', $has = false)
{
    list($function, $position, $file, $line) = getErrorSource($arg);
    if (!is_object($exception) || !$exception instanceof \Exception) {
        // @todo instanceof \Throwable since PHP 7
        $position = $atPosition !== null ? $atPosition : $position + 1;
        /** @var \InvalidArgumentException $_exception */
        $exception = new $exception(sprintf('Argument %s passed to %s() %s %s%s', $position, $function, $has ? 'has' : 'must', $hadTo, is_scalar($arg) ? ', ' . \nspl\getType($arg) . ' ' . var_export($arg, true) . ' given' : ', ' . \nspl\getType($arg) . (is_array($arg) ? ' ' . json_encode($arg) : '') . ' given'));
    }
    $setter = function ($property, $arg) {
        $this->{$property} = $arg;
    };
    $exceptionSetter = $setter->bindTo($exception, $exception);
    $exceptionSetter('file', $file);
    $exceptionSetter('line', $line);
    $baseExceptionSetter = $setter->bindTo($exception, 'Exception');
    $baseExceptionSetter('trace', a\drop($exception->getTrace(), 2));
    throw $exception;
}
Example #2
0
File: ATest.php Project: ihor/Nspl
 public function testDrop()
 {
     $this->assertEquals([7, 8, 9], drop([1, 2, 3, 4, 5, 6, 7, 8, 9], 6));
     $this->assertEquals([7, 8, 9], drop(new \ArrayIterator([1, 2, 3, 4, 5, 6, 7, 8, 9]), 6));
     $this->assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], drop([1, 2, 3, 4, 5, 6, 7, 8, 9], 0));
     $this->assertEquals([], drop([], 3));
     $this->assertEquals([7, 8, 9], call_user_func(drop, [1, 2, 3, 4, 5, 6, 7, 8, 9], 6));
     $this->assertEquals('\\nspl\\a\\drop', drop);
 }