/** * Display human readable info about URL use case * * @param Spec\UseCase $useCase an URL use case specification * @param OutputInterface * @return string */ public function view(Spec\UseCase $useCase) { $request = $useCase->getRequest(); $output = sprintf("\t<options=bold>%s</options=bold>\n\n", $useCase->getDescription()); if ($useCase->isATemplate()) { $url = $useCase->getExampleUrl(); } else { $url = $useCase->getUrl(); } if ($queryParameters = (string) $request->getQuery()) { $url .= '?' . urldecode($queryParameters); } $output .= sprintf("\t<info>%s %s</info>\n\n", $request->getMethod(), $url); if ($headers = $request->getHeaders()) { foreach ($headers as $headerName => $headerValue) { $output .= sprintf("\t%s: <info>%s</info>\n", $headerName, join('; ', $headerValue)); } $output .= "\n"; } if ($body = (string) $request->getBody()) { $json = json_decode($body); if ($json) { $bodyStr = json_encode($json, JSON_PRETTY_PRINT); } else { $bodyStr = $json; } $output .= sprintf("<info>%s</info>\n\n\n", \RestSpec\Output\indentValue($bodyStr, 1)); } $output .= "\n"; return $output; }
public function dumpAsConsoleText() { $output = ''; $useCaseView = new UseCaseView(); $output .= $useCaseView->view($this->spec); if ($this->getStatusCodeViolation()) { $output .= $this->getStatusCodeViolation() . "\n"; } else { $output .= sprintf("\t\tResponse code is <info>%s</info>\n", $this->getResponse()->getStatusCode()); } if ($this->getHeadersViolations()) { foreach ($this->getHeadersViolations() as $violation) { $output .= $violation . "\n"; } } else { $output .= sprintf("\t\tResponse has following required headers:\n"); foreach ($this->responseSpec->getRequiredHeaders() as $headerName => $headerValue) { $output .= sprintf("\t\t\t<info>%s: %s</info>\n", $headerName, $headerValue); } } $output .= "\n"; if (!$this->getBodyViolations()) { $actualBody = (string) $this->getResponse()->getBody(); $json = json_decode($actualBody); if ($json) { $bodyStr = json_encode($json, JSON_PRETTY_PRINT); } else { $bodyStr = $json; } $output .= "\t\tResponse body is valid:\n\n\n"; $output .= sprintf("<info>%s</info>\n\n\n", \RestSpec\Output\indentValue($bodyStr, 2)); } else { foreach ($this->getBodyViolations() as $violation) { $output .= $violation . "\n"; } } return $output; }