44
55namespace Runtime \FrankenPhpSymfony \Tests ;
66
7- require_once __DIR__ . '/function-mock.php ' ;
7+ require_once __DIR__ . '/function-mock.php ' ;
88
99use PHPUnit \Framework \TestCase ;
10+ use Runtime \FrankenPhpSymfony \Exception \InvalidMiddlewareException ;
1011use Runtime \FrankenPhpSymfony \Runner ;
12+ use Runtime \FrankenPhpSymfony \Tests \Support \InvalidMiddleware ;
13+ use Runtime \FrankenPhpSymfony \Tests \Support \TestMiddleware ;
1114use Symfony \Component \HttpFoundation \Request ;
1215use Symfony \Component \HttpFoundation \Response ;
1316use Symfony \Component \HttpKernel \HttpKernelInterface ;
@@ -22,22 +25,65 @@ interface TestAppInterface extends HttpKernelInterface, TerminableInterface
2225 */
2326class RunnerTest extends TestCase
2427{
25- public function testRun (): void
28+
29+ static function runData (): iterable
2630 {
31+ yield 'basic ' => [];
32+ yield 'middleware ' => [
33+ 'middleware ' => TestMiddleware::class
34+ ];
35+ yield 'Invalid middleware ' => [
36+ 'middleware ' => InvalidMiddleware::class,
37+ 'expectException ' => InvalidMiddlewareException::class
38+ ];
39+ }
40+
41+ /**
42+ * @dataProvider runData
43+ */
44+ public function testRun (
45+ ?string $ middleware = null ,
46+ ?string $ expectException = null
47+ ): void {
48+ if ($ expectException !== null ) {
49+ $ this ->expectException ($ expectException );
50+ }
51+
2752 $ application = $ this ->createMock (TestAppInterface::class);
28- $ application
29- ->expects ($ this ->once ())
30- ->method ('handle ' )
31- ->willReturnCallback (function (Request $ request , int $ type = HttpKernelInterface::MAIN_REQUEST , bool $ catch = true ): Response {
32- $ this ->assertSame ('bar ' , $ request ->server ->get ('FOO ' ));
3353
34- return new Response ();
35- });
36- $ application ->expects ($ this ->once ())->method ('terminate ' );
54+ if ($ expectException === null ) {
55+ $ application
56+ ->expects ($ this ->once ())
57+ ->method ('handle ' )
58+ ->willReturnCallback (
59+ function (
60+ Request $ request ,
61+ int $ type = HttpKernelInterface::MAIN_REQUEST ,
62+ bool $ catch = true
63+ ): Response {
64+ $ this ->assertSame ('bar ' , $ request ->server ->get ('FOO ' ));
65+
66+ return new Response ();
67+ }
68+ );
69+ $ application ->expects ($ this ->once ())->method ('terminate ' );
70+ }
3771
3872 $ _SERVER ['FOO ' ] = 'bar ' ;
3973
40- $ runner = new Runner ($ application , 500 );
74+ $ runner = new Runner ($ application , 500 , array_filter ([
75+ $ middleware
76+ ]));
77+
78+ $ assertMiddlewareInvoked = $ expectException === null && $ middleware && method_exists ($ middleware , 'isInvoked ' );
79+ if ($ assertMiddlewareInvoked ) {
80+ $ this ->assertFalse ($ middleware ::isInvoked ());
81+ }
82+
4183 $ this ->assertSame (0 , $ runner ->run ());
84+
85+ if ($ assertMiddlewareInvoked ) {
86+ $ this ->assertTrue ($ middleware ::isInvoked ());
87+ }
4288 }
4389}
0 commit comments