19
19
use Symfony \Component \HttpFoundation \Request ;
20
20
use Symfony \Component \HttpFoundation \Response ;
21
21
use Symfony \Component \HttpKernel \Tests \Logger ;
22
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
22
23
23
24
/**
24
25
* ExceptionListenerTest.
@@ -126,6 +127,44 @@ public function testSubRequestFormat()
126
127
$ response = $ event ->getResponse ();
127
128
$ this ->assertEquals ('xml ' , $ response ->getContent ());
128
129
}
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
+ }
129
168
}
130
169
131
170
class TestLogger extends Logger implements DebugLoggerInterface
@@ -151,3 +190,11 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
151
190
throw new \RuntimeException ('bar ' );
152
191
}
153
192
}
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