diff --git a/.github/workflows/be_base.yml b/.github/workflows/be_base.yml index a27d4ea0..1320c0cf 100644 --- a/.github/workflows/be_base.yml +++ b/.github/workflows/be_base.yml @@ -28,7 +28,8 @@ jobs: --coverage-xml=coverage/coverage-xml --log-junit=coverage/junit.xml # https://infection.github.io/guide/command-line-options.html#coverage - - uses: actions/upload-artifact@v4 + - if: always() + uses: actions/upload-artifact@v4 with: name: coverage-${{ inputs.runs-on }} path: be/coverage/clover.xml diff --git a/be/app/Http/Middleware/DumpJsonResponse.php b/be/app/Http/Middleware/DumpJsonResponse.php index e190a45e..8880820c 100644 --- a/be/app/Http/Middleware/DumpJsonResponse.php +++ b/be/app/Http/Middleware/DumpJsonResponse.php @@ -4,11 +4,12 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Symfony\Component\HttpFoundation\Response; class DumpJsonResponse { - /** @param \Closure(Request): (\Symfony\Component\HttpFoundation\Response) $next */ - public function handle(Request $request, \Closure $next): mixed + /** @param \Closure(Request): (Response) $next */ + public function handle(Request $request, \Closure $next): Response { $response = $next($request); if ($response instanceof JsonResponse) { diff --git a/be/tests/Feature/App/Exceptions/HandlerTest.php b/be/tests/Feature/App/Exceptions/HandlerTest.php new file mode 100644 index 00000000..f05f71ec --- /dev/null +++ b/be/tests/Feature/App/Exceptions/HandlerTest.php @@ -0,0 +1,51 @@ +sut = new Handler(Container::getInstance()); + $this->validatorFactory = App::make(Factory::class); + $this->convertValidationExceptionToResponse = new ReflectionMethod( + Handler::class, + 'convertValidationExceptionToResponse', + ); + } + + private function invokeConvertValidationExceptionToResponse(ValidationException $exception): Response + { + return $this->convertValidationExceptionToResponse->invoke($this->sut, $exception, null); + } + + public function testNotConvertValidationExceptionToResponse(): void + { + $exception = new ValidationException($this->validatorFactory->make([], []), new Response('test')); + $response = $this->invokeConvertValidationExceptionToResponse($exception); + self::assertEquals('test', $response->getContent()); + } + + public function testConvertValidationExceptionToResponse(): void + { + $exception = new ValidationException($this->validatorFactory->make(['test' => 'int'], ['test' => 'int'])); + $response = $this->invokeConvertValidationExceptionToResponse($exception); + $responseJSON = \Safe\json_decode($response->getContent()); + self::assertEquals(40000, $responseJSON->errorCode); + self::assertEquals('The test field must be an integer.', $responseJSON->errorInfo->test[0]); + } +} diff --git a/be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php b/be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php new file mode 100644 index 00000000..3bbecce4 --- /dev/null +++ b/be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php @@ -0,0 +1,39 @@ + JsonResponse::fromJsonString(\Safe\json_encode(['test' => 'test'])); + $sut = new DumpJsonResponse(); + self::assertEquals(<<handle(Request::create('', server: ['HTTP_ACCEPT' => 'application/json']), $next)->getContent()); + + self::assertEquals(<<a15 bytes +
+ + + HTML, ($sut)->handle(Request::create(''), $next)->getContent()); + } +}