You are here

class ExceptionListenerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Tests/EventListener/ExceptionListenerTest.php \Symfony\Component\HttpKernel\Tests\EventListener\ExceptionListenerTest

ExceptionListenerTest.

@author Robert Schönthal <seroscho@googlemail.com>

Hierarchy

  • class \Symfony\Component\HttpKernel\Tests\EventListener\ExceptionListenerTest extends \Symfony\Component\HttpKernel\Tests\EventListener\PHPUnit_Framework_TestCase

Expanded class hierarchy of ExceptionListenerTest

File

vendor/symfony/http-kernel/Tests/EventListener/ExceptionListenerTest.php, line 27

Namespace

Symfony\Component\HttpKernel\Tests\EventListener
View source
class ExceptionListenerTest extends \PHPUnit_Framework_TestCase {
  public function testConstruct() {
    $logger = new TestLogger();
    $l = new ExceptionListener('foo', $logger);
    $_logger = new \ReflectionProperty(get_class($l), 'logger');
    $_logger
      ->setAccessible(true);
    $_controller = new \ReflectionProperty(get_class($l), 'controller');
    $_controller
      ->setAccessible(true);
    $this
      ->assertSame($logger, $_logger
      ->getValue($l));
    $this
      ->assertSame('foo', $_controller
      ->getValue($l));
  }

  /**
   * @dataProvider provider
   */
  public function testHandleWithoutLogger($event, $event2) {
    $this
      ->iniSet('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
    $l = new ExceptionListener('foo');
    $l
      ->onKernelException($event);
    $this
      ->assertEquals(new Response('foo'), $event
      ->getResponse());
    try {
      $l
        ->onKernelException($event2);
      $this
        ->fail('RuntimeException expected');
    } catch (\RuntimeException $e) {
      $this
        ->assertSame('bar', $e
        ->getMessage());
      $this
        ->assertSame('foo', $e
        ->getPrevious()
        ->getMessage());
    }
  }

  /**
   * @dataProvider provider
   */
  public function testHandleWithLogger($event, $event2) {
    $logger = new TestLogger();
    $l = new ExceptionListener('foo', $logger);
    $l
      ->onKernelException($event);
    $this
      ->assertEquals(new Response('foo'), $event
      ->getResponse());
    try {
      $l
        ->onKernelException($event2);
      $this
        ->fail('RuntimeException expected');
    } catch (\RuntimeException $e) {
      $this
        ->assertSame('bar', $e
        ->getMessage());
      $this
        ->assertSame('foo', $e
        ->getPrevious()
        ->getMessage());
    }
    $this
      ->assertEquals(3, $logger
      ->countErrors());
    $this
      ->assertCount(3, $logger
      ->getLogs('critical'));
  }
  public function provider() {
    if (!class_exists('Symfony\\Component\\HttpFoundation\\Request')) {
      return array(
        array(
          null,
          null,
        ),
      );
    }
    $request = new Request();
    $exception = new \Exception('foo');
    $event = new GetResponseForExceptionEvent(new TestKernel(), $request, 'foo', $exception);
    $event2 = new GetResponseForExceptionEvent(new TestKernelThatThrowsException(), $request, 'foo', $exception);
    return array(
      array(
        $event,
        $event2,
      ),
    );
  }
  public function testSubRequestFormat() {
    $listener = new ExceptionListener('foo', $this
      ->getMock('Psr\\Log\\LoggerInterface'));
    $kernel = $this
      ->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $kernel
      ->expects($this
      ->once())
      ->method('handle')
      ->will($this
      ->returnCallback(function (Request $request) {
      return new Response($request
        ->getRequestFormat());
    }));
    $request = Request::create('/');
    $request
      ->setRequestFormat('xml');
    $event = new GetResponseForExceptionEvent($kernel, $request, 'foo', new \Exception('foo'));
    $listener
      ->onKernelException($event);
    $response = $event
      ->getResponse();
    $this
      ->assertEquals('xml', $response
      ->getContent());
  }

}

Members