/** * @param array $transformed * @param string $type * @return object * @throws \Exception */ protected function revertObject($transformed, $type) { $class = new \ReflectionClass($type); $instance = $class->newInstanceWithoutConstructor(); if (!is_array($transformed)) { throw new \Exception("Error while reverting [{$type}]. Input is not an array: " . ValuePrinter::serialize($transformed)); } $reader = new PropertyReader($this->types, $class->getName()); $properties = []; foreach ($reader->readState() as $property) { $properties[] = $property->name(); if (array_key_exists($property->name(), $transformed)) { $value = new TypedValue($transformed[$property->name()], $property->type()); $transformer = $this->transformers->toRevert($value); $property->set($instance, $transformer->revert($value)); } } foreach ($transformed as $key => $value) { if (!in_array($key, $properties)) { $transformer = $this->transformers->toRevert($value); $instance->{$key} = $transformer->revert($value); } } return $instance; }
function testPrintException() { $this->assertEquals("<Exception>", ValuePrinter::serialize(new \Exception())); $this->assertEquals("<InvalidArgumentException>('Some message')", ValuePrinter::serialize(new \InvalidArgumentException('Some message'))); $this->assertEquals("<Exception>('With code', 42)", ValuePrinter::serialize(new \Exception('With code', 42))); $this->assertEquals("<Exception>('With previous') <- <Exception>('The previous') <- <Exception>", ValuePrinter::serialize(new \Exception('With previous', 0, new \Exception('The previous', 0, new \Exception())))); }
/** * @param Argument[] $arguments * @return string */ private function printArguments($arguments) { return implode(', ', array_map(function (ExactArgument $argument) { return ValuePrinter::serialize($argument->value()); }, array_filter($arguments, function (Argument $argument) { return $argument instanceof ExactArgument; }))); }
/** * @param mixed $value * @return Renderer * @throws \Exception */ public function getRenderer($value) { foreach ($this->renderers as $renderer) { if ($renderer->handles($value)) { return $renderer; } } throw new \Exception("No Renderer found to handle " . ValuePrinter::serialize($value)); }
/** * @param \Exception The expected thrown Exception * @throws AssertionFailedException If thrown exception is not $exception */ public function thrown(\Exception $exception) { if (!$this->call->thrown()) { throw new AssertionFailedException($this->methodCall . ' did not throw ' . ValuePrinter::serialize($exception) . "\n" . ' No exception was thrown.'); } if (get_class($exception) != get_class($this->call->thrown()) || $exception->getMessage() != $this->call->thrown()->getMessage()) { throw new AssertionFailedException($this->methodCall . ' did not throw ' . ValuePrinter::serialize($exception) . "\n" . ' The thrown exception was ' . ValuePrinter::serialize($this->call->thrown())); } }
private function toString($value) { return ValuePrinter::serialize($value); }
protected function export($value) { return ValuePrinter::serialize($value); }
private function printResult(Console $console, ExecutionResult $result) { if ($result instanceof RenderedResult) { $console->writeLine((string) $result->getOutput()); return self::OK; } else { if ($result instanceof MissingParametersResult) { $console->writeLine("Missing parameters " . ValuePrinter::serialize($result->getParameters())); return self::ERROR; } else { if ($result instanceof NotPermittedResult) { $console->writeLine('Permission denied'); return self::ERROR; } else { if ($result instanceof FailedResult) { $console->writeLine("Error: " . $result->getMessage()); $exception = $result->getException(); $console->error($exception->getMessage() . ' ' . '[' . $exception->getFile() . ':' . $exception->getLine() . ']' . "\n" . $exception->getTraceAsString()); return $exception->getCode() ?: self::ERROR; } else { if ($result instanceof NoResult || $result instanceof RedirectResult) { return self::OK; } else { $console->writeLine('Cannot print [' . (new \ReflectionClass($result))->getShortName() . ']'); return self::OK; } } } } } }
private function checkReturnValue($returnValue) { $type = $this->typeHint->getType(); if (!$type->is($returnValue)) { $returned = ValuePrinter::serialize($returnValue); throw new \ReflectionException("[{$this->class}::{$this->name}()] returned [{$returned}] " . "which does not match its return type [{$type}]"); } }