public function testGetType() { $this->assertEquals('NULL', getType(null)); $this->assertEquals('boolean', getType(true)); $this->assertEquals('integer', getType(1)); $this->assertEquals('double', getType(1.0)); $this->assertEquals('array', getType([])); $this->assertEquals('string', getType('hello world')); $this->assertEquals('stdClass', getType(new \StdClass())); }
/** * @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) ? ', ' . ds\getType($arg) . ' ' . var_export($arg, true) . ' given' : ', ' . ds\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; }