Commit 049b378e authored by Emma's avatar Emma 🦉

give AjaxListener a proper unit test

parent 23d7e675
Pipeline #20294437 passed with stage
in 4 minutes and 55 seconds
......@@ -2,26 +2,114 @@
namespace App\Tests\EventListener;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\EventListener\AjaxListener;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Serializer\Serializer;
/**
* @covers \App\EventListener\AjaxListener
*/
class AjaxListenerTest extends WebTestCase {
public function test403sOnAuthenticationFailure() {
$client = $this->createClient([], [
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
]);
class AjaxListenerTest extends KernelTestCase {
/**
* @var Serializer
*/
private $serializer;
$client->request('POST', '/cv/1.json');
/**
* @var AjaxListener
*/
private $listener;
$this->assertSame(403, $client->getResponse()->getStatusCode());
protected function setUp() {
static::bootKernel();
$this->serializer = self::$kernel->getContainer()->get('serializer');
$this->listener = new AjaxListener($this->serializer);
}
public function testRedirectsToLoginWithoutXhr() {
$client = $this->createClient();
$client->request('POST', '/cv/1.json');
$this->assertTrue($client->getResponse()->isRedirect());
public function testDoesNotSetResponseOnNotXhrRequests(): void {
$request = new Request();
$event = $this->createEvent($request, new AccessDeniedException());
$this->listener->onKernelException($event);
$this->assertNull($event->getResponse());
}
/**
* @dataProvider fourOhThreeOnExceptionWithSerializedBodyProvider
*/
public function test403sOnExceptionWithSerializedBody(Request $request, \Exception $e): void {
$event = $this->createEvent($request, $e);
$this->listener->onKernelException($event);
$this->assertInstanceOf(Response::class, $event->getResponse());
$this->assertEquals(403, $event->getResponse()->getStatusCode());
$this->assertEquals(
$this->serializer->serialize(
['error' => $e->getMessage()],
$request->getRequestFormat()
),
$event->getResponse()->getContent()
);
}
/**
* @dataProvider fourOhThreeOnExceptionWithPlainBodyProvider
*/
public function test403sOnExceptionWithPlainBody(Request $request, \Exception $e): void {
$event = $this->createEvent($request, $e);
$this->listener->onKernelException($event);
$this->assertInstanceOf(Response::class, $event->getResponse());
$this->assertEquals(403, $event->getResponse()->getStatusCode());
$this->assertEquals($e->getMessage(), $event->getResponse()->getContent());
}
public function fourOhThreeOnExceptionWithSerializedBodyProvider() {
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$request->setRequestFormat('json');
$exception = new AuthenticationException('foo');
yield [$request, $exception];
$request->setRequestFormat('xml');
yield [$request, $exception];
$exception = new AccessDeniedException('aaa');
yield [$request, $exception];
}
public function fourOhThreeOnExceptionWithPlainBodyProvider() {
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$exception = new AuthenticationException('sheep');
yield [$request, $exception];
$exception = new AccessDeniedException('cow');
yield [$request, $exception];
}
private function createEvent(Request $request, \Exception $e): GetResponseForExceptionEvent {
return new GetResponseForExceptionEvent(
self::$kernel,
$request,
HttpKernelInterface::MASTER_REQUEST,
$e
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment