Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3d15fa

Browse files
committed
Add tests for log level overrides
1 parent 838b52a commit c3d15fa

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
2121
use Symfony\Component\HttpKernel\Tests\Logger;
22+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2223

2324
/**
2425
* ExceptionListenerTest.
@@ -126,6 +127,44 @@ public function testSubRequestFormat()
126127
$response = $event->getResponse();
127128
$this->assertEquals('xml', $response->getContent());
128129
}
130+
131+
public function testHttp4xxLogLevel()
132+
{
133+
$logger = new TestLogger();
134+
$l = new ExceptionListener('foo', $logger);
135+
$exception = new \Exception('foo');
136+
$event = new GetResponseForExceptionEvent(new TestKernelThatThrowsHttp4xxException(), Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception);
137+
try {
138+
$l->logKernelException($event);
139+
$l->onKernelException($event);
140+
$this->fail('NotFoundHttpException expected');
141+
} catch (NotFoundHttpException $e) {
142+
$this->assertSame('4xx', $e->getMessage());
143+
$this->assertSame('foo', $e->getPrevious()->getMessage());
144+
}
145+
146+
$this->assertEquals(1, $logger->countErrors());
147+
$this->assertCount(1, $logger->getLogs('warning'));
148+
}
149+
150+
public function testHttpLogLevelOverride()
151+
{
152+
$logger = new TestLogger();
153+
$l = new ExceptionListener('foo', $logger, array(404 => 'notice'));
154+
$exception = new \Exception('foo');
155+
$event = new GetResponseForExceptionEvent(new TestKernelThatThrowsHttp4xxException(), Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception);
156+
try {
157+
$l->logKernelException($event);
158+
$l->onKernelException($event);
159+
$this->fail('NotFoundHttpException expected');
160+
} catch (NotFoundHttpException $e) {
161+
$this->assertSame('4xx', $e->getMessage());
162+
$this->assertSame('foo', $e->getPrevious()->getMessage());
163+
}
164+
165+
$this->assertEquals(1, $logger->countErrors());
166+
$this->assertCount(1, $logger->getLogs('notice'));
167+
}
129168
}
130169

131170
class TestLogger extends Logger implements DebugLoggerInterface
@@ -151,3 +190,11 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
151190
throw new \RuntimeException('bar');
152191
}
153192
}
193+
194+
class TestKernelThatThrowsHttp4xxException implements HttpKernelInterface
195+
{
196+
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
197+
{
198+
throw new NotFoundHttpException('4xx');
199+
}
200+
}

0 commit comments

Comments
 (0)