55namespace Flowpack \SeoRouting \Tests \Unit ;
66
77use Flowpack \SeoRouting \Enum \TrailingSlashModeEnum ;
8+ use Flowpack \SeoRouting \Exceptions \Http \Exception as HttpException ;
89use Flowpack \SeoRouting \Helper \BlocklistHelper ;
910use Flowpack \SeoRouting \Helper \ConfigurationHelper ;
1011use Flowpack \SeoRouting \Helper \LowerCaseHelper ;
2122use Psr \Http \Message \ServerRequestInterface ;
2223use Psr \Http \Server \RequestHandlerInterface ;
2324use ReflectionClass ;
25+ use Throwable ;
2426
2527#[CoversClass(RoutingMiddleware::class)]
28+ #[CoversClass(HttpException::class)]
2629class RoutingMiddlewareTest extends TestCase
2730{
2831 private readonly RoutingMiddleware $ routingMiddleware ;
@@ -72,6 +75,11 @@ protected function setUp(): void
7275 }
7376
7477
78+ /**
79+ * @param class-string<Throwable>|null $expectedException
80+ * @throws Exception
81+ * @throws HttpException
82+ */
7583 #[DataProvider('urlsDataProvider ' )]
7684 public function testProcessShouldHandleUrlsCorrectly (
7785 string $ originalUrl ,
@@ -82,6 +90,7 @@ public function testProcessShouldHandleUrlsCorrectly(
8290 int $ statusCode ,
8391 TrailingSlashModeEnum $ trailingSlashMode ,
8492 int $ handlerStatusCode = 200 ,
93+ ?string $ expectedException = null ,
8594 ): void {
8695 $ originalUri = new Uri ($ originalUrl );
8796 $ expectedUri = new Uri ($ expectedUrl );
@@ -98,8 +107,12 @@ public function testProcessShouldHandleUrlsCorrectly(
98107 $ this ->requestMock ->expects ($ this ->once ())->method ('getUri ' )->willReturn ($ originalUri );
99108
100109 $ pathChanged = $ originalUrl !== $ expectedUrl ;
101-
102- if (!$ pathChanged ) {
110+ if (is_string ($ expectedException )) {
111+ $ this ->expectException ($ expectedException );
112+ $ this ->responseFactoryMock ->expects ($ this ->never ())->method ('createResponse ' );
113+ $ this ->routingMiddleware ->process ($ this ->requestMock , $ this ->requestHandlerMock );
114+ return ;
115+ } elseif (!$ pathChanged ) {
103116 $ this ->requestHandlerMock ->method ('handle ' )->willReturn ($ this ->responseMock );
104117 } elseif ($ handlerStatusCode >= 400 ) {
105118 $ this ->responseMock ->method ('getStatusCode ' )->willReturn ($ handlerStatusCode );
@@ -122,7 +135,6 @@ public function testProcessShouldHandleUrlsCorrectly(
122135 ->with ('Location ' , (string )$ expectedUri )
123136 ->willReturnSelf ();
124137 }
125-
126138 self ::assertSame (
127139 $ this ->responseMock ,
128140 $ this ->routingMiddleware ->process ($ this ->requestMock , $ this ->requestHandlerMock )
@@ -190,6 +202,16 @@ public static function urlsDataProvider(): array
190202 'trailingSlashMode ' => TrailingSlashModeEnum::ADD ,
191203 'handlerStatusCode ' => 404 ,
192204 ],
205+ [
206+ 'originalUrl ' => 'https://local.dev ' ,
207+ 'expectedUrl ' => 'https://local.dev/ ' ,
208+ 'isTrailingSlashEnabledResult ' => true ,
209+ 'isToLowerCaseEnabledResult ' => false ,
210+ 'isUriInBlocklistResult ' => false ,
211+ 'statusCode ' => 404 ,
212+ 'trailingSlashMode ' => TrailingSlashModeEnum::ADD ,
213+ 'expectedException ' => HttpException::class
214+ ],
193215 ];
194216 }
195217}
0 commit comments