public function createResponse(ApiProblem $apiProblem)
 {
     $response = new Response('php://memory', $apiProblem->getDetail('status'), ['Content-Type' => 'application/problem+json']);
     $json = Json::toJson($apiProblem);
     $response->getBody()->write($json);
     return $response;
 }
 public function testCreateFromApiProblemException()
 {
     $apiProblem = ApiProblem::createFromScalar(404, 'Entity not found');
     $exception = new ApiProblemException($apiProblem);
     $apiProblem = ApiProblem::createFromException($exception);
     $this->assertApiProblem($apiProblem, ['title' => 'Not Found', 'status' => 404, 'detail' => 'Entity not found', 'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html']);
 }
예제 #3
0
 public function __invoke($error, ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if ($error instanceof \Exception) {
         $apiProblem = ApiProblem::createFromException($error);
         return $this->factory->createResponse($apiProblem);
     }
     return $next($error, $request, $response);
 }
 public function testCreate()
 {
     $apiProblem = ApiProblem::createFromScalar(400, 'You are wrong');
     $response = $this->factory->createResponse($apiProblem);
     $this->assertInstanceOf(ResponseInterface::class, $response);
     $this->assertSame('application/problem+json', $response->getHeaderLine('Content-type'));
     $this->assertSame(400, $response->getStatusCode());
     $json = '{"title":"Bad Request","status":400,"detail":"You are wrong","type":"http:\\/\\/www.w3.org\\/Protocols\\/rfc2616\\/rfc2616-sec10.html"}';
     $this->assertSame($json, (string) $response->getBody());
 }
 protected function setUp()
 {
     $apiProblem = ApiProblem::createFromScalar(404, 'Boo');
     $prevException = new \LogicException('Not found entity');
     $this->exception = new ApiProblemException($apiProblem, $prevException);
 }