Skip to content

Commit

Permalink
+ class App\Http\Middleware\DumpJsonResponseTest & `App\Exceptions\…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
n0099 committed Sep 25, 2024
1 parent 9c43ae1 commit 6fc05ae
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/be_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 3 additions & 2 deletions be/app/Http/Middleware/DumpJsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions be/tests/Feature/App/Exceptions/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Feature\App\Exceptions;

use App\Exceptions\Handler;
use Illuminate\Container\Container;
use Illuminate\Support\Facades\App;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Factory;
use ReflectionMethod;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;

class HandlerTest extends TestCase
{
private Handler $sut;
private Factory $validatorFactory;
private ReflectionMethod $convertValidationExceptionToResponse;

protected function setUp(): void
{
parent::setUp();
$this->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]);
}
}
40 changes: 40 additions & 0 deletions be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Feature\App\Http\Middleware;

use App\Http\Middleware\DumpJsonResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;

class DumpJsonResponseTest extends TestCase
{
public function testHandle(): void
{
$next = static fn() => JsonResponse::fromJsonString(\Safe\json_encode(['test' => 'test']));
$sut = new DumpJsonResponse();
self::assertEquals(<<<JSON
{
"test": "test"
}
JSON, $sut->handle(Request::create('', server: ['HTTP_ACCEPT' => 'application/json']), $next)->getContent());

self::assertEquals(<<<HTML
<h4>15 bytes</h4>
<div id="root"></div>
<script type="module">
import ReactJsonView from 'http://localhost/be/assets/react-json-view.js';
import { createElement } from 'http://localhost/be/assets/react.js';
import { createRoot } from 'http://localhost/be/assets/react-dom.js';
const root = createRoot(document.getElementById('root'));
root.render(createElement(ReactJsonView.default, { src: {"test":"test"}, quotesOnKeys: false }));
</script>
<style>
.object-content {
content-visibility: auto;
}
</style>
HTML, ($sut)->handle(Request::create(''), $next)->getContent());
}
}

0 comments on commit 6fc05ae

Please sign in to comment.