44
55namespace Unit \Http ;
66
7+ use Exception ;
78use Flowpack \ContentSecurityPolicy \Factory \PolicyFactory ;
89use Flowpack \ContentSecurityPolicy \Helpers \TagHelper ;
910use Flowpack \ContentSecurityPolicy \Http \CspHeaderMiddleware ;
2021use Psr \Log \LoggerInterface ;
2122use ReflectionClass ;
2223use Throwable ;
23-
2424use function PHPUnit \Framework \once ;
2525
2626#[CoversClass(CspHeaderMiddleware::class)]
@@ -187,9 +187,20 @@ public function testProcessThrowsOnInvalidMatchUriPattern(): void
187187 $ this ->requestMock ->expects ($ this ->once ())->method ('getUri ' )->willReturn ($ this ->uriMock );
188188 $ this ->uriMock ->expects ($ this ->once ())->method ('getPath ' )->willReturn ('/neos ' );
189189
190- $ this ->expectException (\InvalidArgumentException::class);
191-
192- $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
190+ /*
191+ * preg_match emmits a warning which makes phpunit fail, so we convert warnings to errors and expect an exception
192+ * as we cannot expect warnings
193+ */
194+ set_error_handler (static function (int $ errorCode , string $ errorString ): never {
195+ throw new Exception ($ errorString , $ errorCode );
196+ }, E_WARNING );
197+ $ this ->expectExceptionMessage ('Compilation failed ' );
198+
199+ try {
200+ $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
201+ } finally {
202+ restore_error_handler ();
203+ }
193204 }
194205
195206 public function testProcessLogsInvalidMatchUriPatternInProduction (): void
@@ -211,6 +222,18 @@ public function testProcessLogsInvalidMatchUriPatternInProduction(): void
211222 $ this ->policyMock ->expects ($ this ->once ())->method ('hasNonceDirectiveValue ' )->willReturn (false );
212223 $ this ->responseMock ->expects ($ this ->once ())->method ('withAddedHeader ' )->willReturnSelf ();
213224
214- $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
225+ /*
226+ * preg_match emmits a warning which makes phpunit fail, so we suppress the warning that would make phpunit
227+ * fail
228+ */
229+ set_error_handler (static function (): bool {
230+ return true ;
231+ }, E_WARNING );
232+
233+ try {
234+ $ this ->middleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
235+ } finally {
236+ restore_error_handler ();
237+ }
215238 }
216239}
0 commit comments