21
21
use Micro \Plugin \Http \Facade \HttpFacadeInterface ;
22
22
use PHPUnit \Framework \TestCase ;
23
23
use Symfony \Component \HttpFoundation \Request ;
24
+ use Symfony \Component \HttpFoundation \Response ;
24
25
25
26
/**
26
27
* @author Stanislau Komar <head.trackingsoft@gmail.com>
@@ -72,44 +73,40 @@ public function testDecorated()
72
73
/**
73
74
* @dataProvider dataProviderException
74
75
*/
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
76
77
{
77
78
$ kernel = $ this ->createKernel ($ env );
78
79
$ request = Request::create ($ uri );
79
80
$ request ->request ->set ('_format ' , $ format );
80
81
81
82
$ isDev = str_starts_with ($ env , 'dev ' );
83
+ if (!$ isDev ) {
84
+ $ this ->expectException (HttpException::class);
85
+ }
86
+
82
87
preg_match ('/\d+/ ' , $ uri , $ match );
83
88
$ exceptionCode = (int ) $ match [0 ];
84
89
85
- $ this ->expectException (HttpException::class);
86
- $ this ->expectExceptionCode ($ exceptionCode );
87
-
88
- ob_start ();
89
-
90
- try {
90
+ if (!$ isDev ) {
91
91
$ response = $ kernel ->container ()->get (HttpFacadeInterface::class)
92
92
->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
+ }
93
101
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 ();
102
103
103
- if ('html ' === $ format ) {
104
- $ this ->assertStringStartsWith ('<!-- ' , $ flushedContent );
105
- $ this ->assertStringEndsWith ('--> ' , $ flushedContent );
106
- }
107
- }
104
+ $ this ->assertEquals ($ exceptionCode , $ response ->getStatusCode ());
108
105
109
- throw $ httpException ;
106
+ if ($ isFlush ) {
107
+ $ this ->assertEquals ($ responseContent , $ flushedContent );
110
108
}
111
109
112
- $ responseContent = $ response ->getContent ();
113
110
if ('html ' === $ format ) {
114
111
$ this ->assertStringStartsWith ('<!-- ' , $ responseContent );
115
112
$ this ->assertStringEndsWith ('--> ' , $ responseContent );
@@ -118,15 +115,12 @@ public function testExceptionResponse(string $env, string $uri, bool $isFlush, m
118
115
if ('json ' === $ format ) {
119
116
$ this ->assertJson ($ responseContent );
120
117
}
121
-
122
- $ this ->assertStringEndsWith ($ responseContent , $ flushedContent );
123
- $ this ->assertStringStartsWith ($ responseContent , $ flushedContent );
124
118
}
125
119
126
120
/**
127
121
* @dataProvider dataProviderSuccess
128
122
*/
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
130
124
{
131
125
$ kernel = $ this ->createKernel ($ env );
132
126
$ request = Request::create ($ uri );
@@ -139,11 +133,11 @@ public function testSuccessResponse(string $env, string $uri, bool $isFlush, mix
139
133
140
134
$ responseFlushedContent = ob_get_clean ();
141
135
142
- $ this ->assertEquals ($ isFlush ? $ result : '' , $ responseFlushedContent );
136
+ $ this ->assertEquals ($ isFlush ? $ result : false , $ responseFlushedContent );
143
137
$ this ->assertEquals ($ result , $ response ->getContent ());
144
138
}
145
139
146
- public function dataProviderSuccess ()
140
+ public function dataProviderSuccess (): array
147
141
{
148
142
return [
149
143
['dev ' , '/ ' , true , 'Hello, world ' , 'html ' ],
@@ -159,7 +153,7 @@ public function dataProviderSuccess()
159
153
];
160
154
}
161
155
162
- public function dataProviderException ()
156
+ public function dataProviderException (): array
163
157
{
164
158
return [
165
159
['dev ' , '/404 ' , true , null , 'html ' ],
0 commit comments