-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ 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
- Loading branch information
Showing
4 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
be/tests/Feature/App/Http/Middleware/DumpJsonResponseTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |