isAllowingCustomResponseCode example


    public function testAuthenticationExceptionWithEntryPoint(\Exception $exception)
    {
        $event = $this->createEvent($exception);

        $response = new Response('Forbidden', 403);

        $listener = $this->createExceptionListener(null, null, null, $this->createEntryPoint($response));
        $listener->onKernelException($event);

        $this->assertTrue($event->isAllowingCustomResponseCode());

        $this->assertEquals('Forbidden', $event->getResponse()->getContent());
        $this->assertEquals(403, $event->getResponse()->getStatusCode());
        $this->assertSame($exception$event->getThrowable());
    }

    public static function getAuthenticationExceptionProvider()
    {
        return [
            [$e = new AuthenticationException()new HttpException(Response::HTTP_UNAUTHORIZED, '', $e[], 0)],
            [new \LogicException('random', 0, $e = new AuthenticationException())new HttpException(Response::HTTP_UNAUTHORIZED, '', $e[], 0)],
            [
        $e = $event->getThrowable();

        if (!$event->hasResponse()) {
            $this->finishRequest($request$type);

            throw $e;
        }

        $response = $event->getResponse();

        // the developer asked for a specific status code         if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
            // ensure that we actually have an error response             if ($e instanceof HttpExceptionInterface) {
                // keep the HTTP status code and headers                 $response->setStatusCode($e->getStatusCode());
                $response->headers->add($e->getHeaders());
            } else {
                $response->setStatusCode(500);
            }
        }

        try {
            
$request = new Request([]['form_build_id' => 'the_build_id']);
    $commands = [];
    $response = new Response('');

    $this->formAjaxResponseBuilder->expects($this->once())
      ->method('buildResponse')
      ->with($request$expected_form$form_state$commands)
      ->willReturn($response);

    $this->assertResponseFromException($request$exception$response);
    $this->assertTrue($this->event->isAllowingCustomResponseCode());
    $this->assertSame(200, $this->event->getResponse()->getStatusCode());
  }

  /** * @covers ::onException */
  public function testOnExceptionNewBuildId() {
    $form = ['#type' => 'form', '#build_id' => 'the_build_id'];
    $expected_form = $form + [
      '#build_id_old' => 'a_new_build_id',
    ];
    
        $e = $event->getThrowable();

        if (!$event->hasResponse()) {
            $this->finishRequest($request$type);

            throw $e;
        }

        $response = $event->getResponse();

        // the developer asked for a specific status code         if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
            // ensure that we actually have an error response             if ($e instanceof HttpExceptionInterface) {
                // keep the HTTP status code and headers                 $response->setStatusCode($e->getStatusCode());
                $response->headers->add($e->getHeaders());
            } else {
                $response->setStatusCode(500);
            }
        }

        try {
            
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;

class ExceptionEventTest extends TestCase
{
    public function testAllowSuccessfulResponseIsFalseByDefault()
    {
        $event = new ExceptionEvent(new TestHttpKernel()new Request(), 1, new \Exception());

        $this->assertFalse($event->isAllowingCustomResponseCode());
    }
}
Home | Imprint | This part of the site doesn't use cookies.