1212use Amp \Http \Server \RequestHandler \ClosureRequestHandler ;
1313use Amp \Http \Server \Response ;
1414use Amp \Http \Server \SocketHttpServer ;
15- use Illuminate \Contracts \Debug \ExceptionHandler ;
16- use Illuminate \Contracts \Http \Kernel as HttpKernel ;
17- use Illuminate \Foundation \Testing \Concerns \WithoutExceptionHandlingHandler ;
18- use Illuminate \Http \Request ;
19- use Illuminate \Routing \UrlGenerator ;
20- use Illuminate \Support \Uri ;
15+ use Exception ;
2116use Pest \Browser \Contracts \HttpServer ;
2217use Pest \Browser \Exceptions \ServerNotFoundException ;
2318use Pest \Browser \Execution ;
2419use Pest \Browser \GlobalState ;
2520use Psr \Log \NullLogger ;
21+ use Symfony \Component \HttpFoundation \Request ;
22+ use Symfony \Component \HttpKernel \KernelInterface ;
23+ use Symfony \Component \HttpKernel \TerminableInterface ;
2624use Symfony \Component \Mime \MimeTypes ;
2725use Throwable ;
2826
@@ -49,7 +47,7 @@ final class SymfonyHttpServer implements HttpServer
4947 private ?Throwable $ lastThrowable = null ;
5048
5149 /**
52- * Creates a new laravel http server instance.
50+ * Creates a new Symfony http server instance.
5351 */
5452 public function __construct (
5553 public readonly string $ host ,
@@ -79,13 +77,11 @@ public function rewrite(string $url): string
7977 }
8078
8179 $ parts = parse_url ($ url );
82- $ queryParameters = [];
8380 $ path = $ parts ['path ' ] ?? '/ ' ;
84- parse_str ($ parts ['query ' ] ?? '' , $ queryParameters );
81+ $ query = $ parts ['query ' ] ?? '' ;
82+ $ fragment = $ parts ['fragment ' ] ?? '' ;
8583
86- return (string ) Uri::of ($ this ->url ())
87- ->withPath ($ path )
88- ->withQuery ($ queryParameters );
84+ return $ this ->url ().$ path .($ query !== '' ? '? ' .$ query : '' ).($ fragment !== '' ? '# ' .$ fragment : '' );
8985 }
9086
9187 /**
@@ -146,22 +142,8 @@ public function bootstrap(): void
146142 {
147143 $ this ->start ();
148144
149- $ url = $ this ->url ();
150-
151- config (['app.url ' => $ url ]);
152-
153- config (['cors.paths ' => ['* ' ]]);
154-
155- if (app ()->bound ('url ' )) {
156- $ urlGenerator = app ('url ' );
157-
158- assert ($ urlGenerator instanceof UrlGenerator);
159-
160- $ this ->setOriginalAssetUrl ($ urlGenerator ->asset ('' ));
161-
162- $ urlGenerator ->useOrigin ($ url );
163- $ urlGenerator ->useAssetOrigin ($ url );
164- $ urlGenerator ->forceScheme ('http ' );
145+ if (is_string ($ _ENV ['DEFAULT_URI ' ])) {
146+ $ this ->setOriginalAssetUrl ($ _ENV ['DEFAULT_URI ' ]);
165147 }
166148 }
167149
@@ -184,11 +166,7 @@ public function throwLastThrowableIfNeeded(): void
184166 return ;
185167 }
186168
187- $ exceptionHandler = app (ExceptionHandler::class);
188-
189- if ($ exceptionHandler instanceof WithoutExceptionHandlingHandler) {
190- throw $ this ->lastThrowable ;
191- }
169+ throw $ this ->lastThrowable ;
192170 }
193171
194172 /**
@@ -222,18 +200,20 @@ private function handleRequest(AmpRequest $request): Response
222200 Execution::instance ()->tick ();
223201 }
224202
225- $ uri = $ request ->getUri ();
226- $ path = in_array ($ uri ->getPath (), ['' , '0 ' ], true ) ? '/ ' : $ uri ->getPath ();
227- $ query = $ uri ->getQuery () ?? '' ; // @phpstan-ignore-line
228- $ fullPath = $ path .($ query !== '' ? '? ' .$ query : '' );
229- $ absoluteUrl = mb_rtrim ($ this ->url (), '/ ' ).$ fullPath ;
230-
231- $ filepath = public_path ($ path );
203+ $ publicPath = getcwd ().DIRECTORY_SEPARATOR .(is_string ($ _ENV ['PUBLIC_PATH ' ]) ? $ _ENV ['PUBLIC_PATH ' ] : 'public ' );
204+ $ filepath = $ publicPath .$ request ->getUri ()->getPath ();
232205 if (file_exists ($ filepath ) && ! is_dir ($ filepath )) {
233206 return $ this ->asset ($ filepath );
234207 }
235208
236- $ kernel = app ()->make (HttpKernel::class);
209+ $ kernelClass = is_string ($ _ENV ['KERNEL_CLASS ' ]) ? $ _ENV ['KERNEL_CLASS ' ] : 'App\Kernel ' ;
210+ if (class_exists ($ kernelClass ) === false ) {
211+ $ this ->lastThrowable = new Exception ('You must define the test kernel class environment variable: KERNEL_CLASS. ' );
212+
213+ throw $ this ->lastThrowable ;
214+ }
215+ /** @var KernelInterface&TerminableInterface $kernel */
216+ $ kernel = new $ kernelClass ($ _ENV ['APP_ENV ' ], (bool ) $ _ENV ['APP_DEBUG ' ]);
237217
238218 $ contentType = $ request ->getHeader ('content-type ' ) ?? '' ;
239219 $ method = mb_strtoupper ($ request ->getMethod ());
@@ -244,7 +224,7 @@ private function handleRequest(AmpRequest $request): Response
244224 }
245225
246226 $ symfonyRequest = Request::create (
247- $ absoluteUrl ,
227+ ( string ) $ request -> getUri () ,
248228 $ method ,
249229 $ parameters ,
250230 $ request ->getCookies (),
@@ -255,21 +235,15 @@ private function handleRequest(AmpRequest $request): Response
255235
256236 $ symfonyRequest ->headers ->add ($ request ->getHeaders ());
257237
258- $ debug = config ('app.debug ' );
259-
260238 try {
261- config (['app.debug ' => false ]);
262-
263- $ response = $ kernel ->handle ($ laravelRequest = Request::createFromBase ($ symfonyRequest ));
239+ $ response = $ kernel ->handle ($ symfonyRequest );
264240 } catch (Throwable $ e ) {
265241 $ this ->lastThrowable = $ e ;
266242
267243 throw $ e ;
268- } finally {
269- config (['app.debug ' => $ debug ]);
270244 }
271245
272- $ kernel ->terminate ($ laravelRequest , $ response );
246+ $ kernel ->terminate ($ symfonyRequest , $ response );
273247
274248 if (property_exists ($ response , 'exception ' ) && $ response ->exception !== null ) {
275249 assert ($ response ->exception instanceof Throwable);
@@ -312,7 +286,7 @@ private function asset(string $filepath): Response
312286
313287 $ contentType = $ contentType [0 ] ?? 'application/octet-stream ' ;
314288
315- if (str_ends_with ($ filepath , '.js ' )) {
289+ if (str_ends_with ($ filepath , '.js ' ) || str_ends_with ( $ filepath , ' .css ' ) ) {
316290 $ temporaryStream = fopen ('php://temp ' , 'r+ ' );
317291 assert ($ temporaryStream !== false , 'Failed to open temporary stream. ' );
318292
0 commit comments