1010use PHPUnit \Framework \MockObject \Exception ;
1111use PHPUnit \Framework \MockObject \MockObject ;
1212use PHPUnit \Framework \TestCase ;
13+ use Psr \Http \Message \ResponseFactoryInterface ;
1314use Psr \Http \Message \ResponseInterface ;
1415use Psr \Http \Message \ServerRequestInterface ;
1516use Psr \Http \Server \RequestHandlerInterface ;
@@ -24,6 +25,7 @@ class CorsHeaderMiddlewareTest extends TestCase
2425 private readonly ResponseInterface &MockObject $ responseMock ;
2526 private readonly RequestHandlerInterface &MockObject $ handlerMock ;
2627 private readonly LoggerInterface &MockObject $ logger ;
28+ private readonly ResponseFactoryInterface &MockObject $ responseFactoryMock ;
2729
2830 /**
2931 * @throws Exception
@@ -35,12 +37,12 @@ protected function setUp(): void
3537 $ this ->requestMock = $ this ->createMock (ServerRequestInterface::class);
3638 $ this ->responseMock = $ this ->createMock (ResponseInterface::class);
3739 $ this ->handlerMock = $ this ->createMock (RequestHandlerInterface::class);
40+ $ this ->responseFactoryMock = $ this ->createMock (ResponseFactoryInterface::class);
3841 $ this ->logger = $ this ->createMock (LoggerInterface::class);
3942
4043 ObjectAccess::setProperty ($ this ->middleware , 'enabled ' , true , true );
4144 ObjectAccess::setProperty ($ this ->middleware , 'logger ' , $ this ->logger , true );
42-
43- $ this ->handlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
45+ ObjectAccess::setProperty ($ this ->middleware , 'responseFactory ' , $ this ->responseFactoryMock , true );
4446 }
4547
4648 public function testMiddlewareIsNotEnabled (): void
@@ -65,8 +67,11 @@ public function testMiddlewarePreflightWithConfig(): void
6567 };
6668 });
6769
68- $ this ->responseMock ->expects ($ this ->exactly (5 ))->method ('withHeader ' )->willReturnSelf ();
70+ // the process is not passed down the chain and a fresh response is created
71+ $ this ->handlerMock ->expects ($ this ->never ())->method ('handle ' );
72+ $ this ->responseFactoryMock ->expects ($ this ->once ())->method ('createResponse ' )->willReturn ($ this ->responseMock );
6973
74+ $ this ->responseMock ->expects ($ this ->exactly (5 ))->method ('withHeader ' )->willReturnSelf ();
7075 $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
7176 }
7277
@@ -83,9 +88,12 @@ public function testMiddlewarePreflightWithWildcardConfig(): void
8388 };
8489 });
8590
86- $ this ->responseMock ->expects ($ this ->exactly (4 ))->method ('withHeader ' )->willReturnSelf ();
91+ // the process is not passed down the chain and a fresh response is created
92+ $ this ->handlerMock ->expects ($ this ->never ())->method ('handle ' );
93+ $ this ->responseFactoryMock ->expects ($ this ->once ())->method ('createResponse ' )->willReturn ($ this ->responseMock );
8794
88- $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
95+ $ this ->responseMock ->expects ($ this ->exactly (4 ))->method ('withHeader ' )->willReturnSelf ();
96+ $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
8997 }
9098
9199 public function testMiddlewareActualRequestWithConfig (): void
@@ -100,6 +108,8 @@ public function testMiddlewareActualRequestWithConfig(): void
100108 };
101109 });
102110
111+ // request is passed down the chain
112+ $ this ->handlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
103113 $ this ->responseMock ->expects ($ this ->exactly (4 ))->method ('withHeader ' )->willReturnSelf ();
104114
105115 $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
@@ -117,6 +127,8 @@ public function testMiddlewareActualRequestWithWildcardConfig(): void
117127 };
118128 });
119129
130+ // request is passed down the chain
131+ $ this ->handlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
120132 $ this ->responseMock ->expects ($ this ->exactly (4 ))->method ('withHeader ' )->willReturnSelf ();
121133
122134 $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
@@ -142,6 +154,8 @@ public function testMiddlewareActualRequestWithWildcardOrigin(): void
142154 };
143155 });
144156
157+ // request is passed down the chain
158+ $ this ->handlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
145159 $ this ->responseMock ->expects ($ this ->exactly (4 ))->method ('withHeader ' )->willReturnSelf ();
146160
147161 $ this ->middleware ->process ($ this ->requestMock , $ this ->handlerMock );
@@ -167,6 +181,8 @@ public function testMiddlewareActualRequestWithNotAllowedOrigin(): void
167181 };
168182 });
169183
184+ // request is passed down the chain
185+ $ this ->handlerMock ->expects ($ this ->once ())->method ('handle ' )->willReturn ($ this ->responseMock );
170186 $ this ->responseMock ->expects ($ this ->never ())->method ('withHeader ' )->willReturnSelf ();
171187
172188 $ this ->logger ->expects ($ this ->exactly (2 ))->method ('debug ' );
0 commit comments