From 8ebfd7c0b05d507fd189185a3775e7701376b6ab Mon Sep 17 00:00:00 2001 From: n0099 Date: Wed, 25 Sep 2024 13:23:02 +0000 Subject: [PATCH] + class `App\Http\Middleware\DumpJsonResponseTest` & `App\Exceptions\HandlerTest` for testing their namesake classes @ `Tests\Feature` * add return type for method `handle()` @ `App\Exceptions\Handler` @ be * always run steps of uploading `coverage/clover.xml` artifact and running `infection` even when the step `phpunit` exited with non-zero code in the job `phpunit-inflection` @ .github/workflows/be_base.yml --- .github/workflows/be_base.yml | 6 ++- be/app/Http/Middleware/DumpJsonResponse.php | 5 +- .../Feature/App/Exceptions/HandlerTest.php | 51 +++++++++++++++++++ .../Http/Middleware/DumpJsonResponseTest.php | 40 +++++++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 be/tests/Feature/App/Exceptions/HandlerTest.php create mode 100644 be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php diff --git a/.github/workflows/be_base.yml b/.github/workflows/be_base.yml index a27d4ea0..13fa4943 100644 --- a/.github/workflows/be_base.yml +++ b/.github/workflows/be_base.yml @@ -28,12 +28,14 @@ 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 compression-level: 9 - - run: ./vendor/bin/infection --coverage=coverage --skip-initial-tests + - if: always() + run: ./vendor/bin/infection --coverage=coverage --skip-initial-tests phan: runs-on: ${{ inputs.runs-on }} 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..90e11be7 --- /dev/null +++ b/be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php @@ -0,0 +1,40 @@ + JsonResponse::fromJsonString(\Safe\json_encode(['test' => 'test'])); + $sut = new DumpJsonResponse(); + self::assertEquals(<<handle(Request::create('', server: ['HTTP_ACCEPT' => 'application/json']), $next)->getContent()); + + self::assertEquals(<<15 bytes +
+ + + HTML, ($sut)->handle(Request::create(''), $next)->getContent()); + } +}