runErrorHandlerTest(new UnexpectedValueException(), 400); } public function testItCovertsToForbidden() { $this->runErrorHandlerTest(new AccessDeniedException(), 403); } public function testItCovertsToNotFound() { $this->runErrorHandlerTest(new NotFoundException(), 404); } public function testItCovertsToConflict() { $this->runErrorHandlerTest(new ConflictException(), 409); } public function testItCovertsToServerError() { $this->runErrorHandlerTest(new RuntimeException(), 500); $this->runErrorHandlerTest(new InvalidStateException(), 500); } private function runErrorHandlerTest(Exception $exception, int $expectedCode) { $errorHandler = new ErrorHandler(new WPFunctions()); $response = $errorHandler->convertToResponse($exception->withErrors([ 'key' => 'value', ])); expect($response)->isInstanceOf(ErrorResponse::class); expect($response->status)->equals($expectedCode); expect($response->errors)->equals([['error' => 'key', 'message' => 'value']]); } }