Skip to content

Commit d3bd71e

Browse files
committed
fix tests
1 parent 22b5d9e commit d3bd71e

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

Tests/Unit/Http/CspHeaderMiddlewareTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Unit\Http;
66

7+
use Exception;
78
use Flowpack\ContentSecurityPolicy\Factory\PolicyFactory;
89
use Flowpack\ContentSecurityPolicy\Helpers\TagHelper;
910
use Flowpack\ContentSecurityPolicy\Http\CspHeaderMiddleware;
@@ -20,7 +21,6 @@
2021
use Psr\Log\LoggerInterface;
2122
use ReflectionClass;
2223
use Throwable;
23-
2424
use 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

Comments
 (0)