-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Tests\EventListener; | ||
|
||
use App\EventListener\JsonResponseExceptionListener; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class JsonResponseExceptionListenerTest extends TestCase | ||
{ | ||
public function testOnKernelException() | ||
{ | ||
$listener = new JsonResponseExceptionListener(); | ||
|
||
$kernel = $this->createMock(HttpKernelInterface::class); | ||
$request = new Request(); | ||
$request->headers->set('Accept', 'application/json'); | ||
$request->headers->set('Content-Type', 'application/json'); | ||
|
||
$exception = new HttpException(400, 'Bad Request'); | ||
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); | ||
|
||
$listener->onKernelException($event); | ||
|
||
$response = $event->getResponse(); | ||
|
||
$this->assertInstanceOf(JsonResponse::class, $response); | ||
$this->assertEquals(400, $response->getStatusCode()); | ||
$this->assertJson($response->getContent()); | ||
$data = json_decode($response->getContent(), true); | ||
$this->assertArrayHasKey('error', $data); | ||
$this->assertStringContainsString('Bad Request', $data['error']); | ||
} | ||
} |