88namespace Tests \One ;
99
1010use Framework \HTTP \URL ;
11+ use Framework \MVC \App ;
1112use JetBrains \PhpStorm \ArrayShape ;
1213
1314/**
1415 * Class TestCase.
1516 */
1617abstract class TestCase extends \PHPUnit \Framework \TestCase
1718{
19+ protected string $ baseUrl = 'https://localhost:8080/ ' ;
20+
1821 /**
1922 * Run the One file.
2023 *
21- * @param string|URL $url
24+ * @param string|URL|null $url
2225 * @param string $method
2326 * @param array<string,string> $headers
2427 *
2528 * @return array<string,mixed>
2629 */
2730 #[ArrayShape(['code ' => 'int ' , 'headers ' => 'array ' , 'body ' => 'string ' ])]
28- protected function runOne (URL | string $ url , string $ method = 'GET ' , array $ headers = []) : array
29- {
31+ protected function runOne (
32+ URL | string $ url = null ,
33+ string $ method = 'GET ' ,
34+ array $ headers = []
35+ ) : array {
36+ $ this ->baseUrl = \rtrim ($ this ->baseUrl , '/ ' ) . '/ ' ;
37+ if ($ url === null ) {
38+ $ url = $ this ->baseUrl ;
39+ }
40+ if (\is_string ($ url ) && \str_starts_with ($ url , '/ ' )) {
41+ $ url = $ this ->baseUrl . \ltrim ($ url , '/ ' );
42+ }
3043 if ( ! $ url instanceof URL ) {
3144 $ url = new URL ($ url );
3245 }
46+ if ($ url ->getScheme () === 'https ' ) {
47+ $ _SERVER ['HTTPS ' ] = 'on ' ;
48+ }
3349 $ _SERVER ['SERVER_PROTOCOL ' ] = 'HTTP/1.1 ' ;
3450 $ _SERVER ['REQUEST_METHOD ' ] = $ method ;
3551 $ _SERVER ['REQUEST_URI ' ] = $ url ->getPath ();
@@ -41,11 +57,12 @@ protected function runOne(URL | string $url, string $method = 'GET', array $head
4157 }
4258 \ob_start ();
4359 require __DIR__ . '/../public/index.php ' ;
44- $ body = (string ) \ob_get_clean ();
60+ \ob_end_clean ();
61+ $ response = App::response ();
4562 return [
46- 'code ' => ( int ) \http_response_code (),
47- 'headers ' => \headers_list (),
48- 'body ' => $ body ,
63+ 'code ' => $ response -> getStatusCode (),
64+ 'headers ' => $ response -> getHeaders (),
65+ 'body ' => $ response -> getBody () ,
4966 ];
5067 }
5168}
0 commit comments