2121use Micro \Plugin \Http \Facade \HttpFacadeInterface ;
2222use PHPUnit \Framework \TestCase ;
2323use Symfony \Component \HttpFoundation \Request ;
24+ use Symfony \Component \HttpFoundation \Response ;
2425
2526/**
2627 * @author Stanislau Komar <head.trackingsoft@gmail.com>
@@ -72,44 +73,40 @@ public function testDecorated()
7273 /**
7374 * @dataProvider dataProviderException
7475 */
75- public function testExceptionResponse (string $ env , string $ uri , bool $ isFlush , mixed $ result , string $ format )
76+ public function testExceptionResponse (string $ env , string $ uri , bool $ isFlush , mixed $ result , string $ format ): void
7677 {
7778 $ kernel = $ this ->createKernel ($ env );
7879 $ request = Request::create ($ uri );
7980 $ request ->request ->set ('_format ' , $ format );
8081
8182 $ isDev = str_starts_with ($ env , 'dev ' );
83+ if (!$ isDev ) {
84+ $ this ->expectException (HttpException::class);
85+ }
86+
8287 preg_match ('/\d+/ ' , $ uri , $ match );
8388 $ exceptionCode = (int ) $ match [0 ];
8489
85- $ this ->expectException (HttpException::class);
86- $ this ->expectExceptionCode ($ exceptionCode );
87-
88- ob_start ();
89-
90- try {
90+ if (!$ isDev ) {
9191 $ response = $ kernel ->container ()->get (HttpFacadeInterface::class)
9292 ->execute ($ request , $ isFlush );
93+ } else {
94+ ob_start ();
95+ /** @var Response $response */
96+ $ response = $ kernel ->container ()->get (HttpFacadeInterface::class)
97+ ->execute ($ request , $ isFlush );
98+ $ flushedContent = ob_get_contents ();
99+ ob_end_clean ();
100+ }
93101
94- $ flushedContent = ob_get_clean ();
95- } catch (HttpException $ httpException ) {
96- $ flushedContent = ob_get_clean ();
97-
98- if ($ isFlush ) {
99- if ('json ' === $ format ) {
100- $ this ->assertJson ($ flushedContent );
101- }
102+ $ responseContent = $ response ->getContent ();
102103
103- if ('html ' === $ format ) {
104- $ this ->assertStringStartsWith ('<!-- ' , $ flushedContent );
105- $ this ->assertStringEndsWith ('--> ' , $ flushedContent );
106- }
107- }
104+ $ this ->assertEquals ($ exceptionCode , $ response ->getStatusCode ());
108105
109- throw $ httpException ;
106+ if ($ isFlush ) {
107+ $ this ->assertEquals ($ responseContent , $ flushedContent );
110108 }
111109
112- $ responseContent = $ response ->getContent ();
113110 if ('html ' === $ format ) {
114111 $ this ->assertStringStartsWith ('<!-- ' , $ responseContent );
115112 $ this ->assertStringEndsWith ('--> ' , $ responseContent );
@@ -118,15 +115,12 @@ public function testExceptionResponse(string $env, string $uri, bool $isFlush, m
118115 if ('json ' === $ format ) {
119116 $ this ->assertJson ($ responseContent );
120117 }
121-
122- $ this ->assertStringEndsWith ($ responseContent , $ flushedContent );
123- $ this ->assertStringStartsWith ($ responseContent , $ flushedContent );
124118 }
125119
126120 /**
127121 * @dataProvider dataProviderSuccess
128122 */
129- public function testSuccessResponse (string $ env , string $ uri , bool $ isFlush , mixed $ result , string $ format )
123+ public function testSuccessResponse (string $ env , string $ uri , bool $ isFlush , mixed $ result , string $ format ): void
130124 {
131125 $ kernel = $ this ->createKernel ($ env );
132126 $ request = Request::create ($ uri );
@@ -139,11 +133,11 @@ public function testSuccessResponse(string $env, string $uri, bool $isFlush, mix
139133
140134 $ responseFlushedContent = ob_get_clean ();
141135
142- $ this ->assertEquals ($ isFlush ? $ result : '' , $ responseFlushedContent );
136+ $ this ->assertEquals ($ isFlush ? $ result : false , $ responseFlushedContent );
143137 $ this ->assertEquals ($ result , $ response ->getContent ());
144138 }
145139
146- public function dataProviderSuccess ()
140+ public function dataProviderSuccess (): array
147141 {
148142 return [
149143 ['dev ' , '/ ' , true , 'Hello, world ' , 'html ' ],
@@ -159,7 +153,7 @@ public function dataProviderSuccess()
159153 ];
160154 }
161155
162- public function dataProviderException ()
156+ public function dataProviderException (): array
163157 {
164158 return [
165159 ['dev ' , '/404 ' , true , null , 'html ' ],
0 commit comments