| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Browser\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Panther\PantherTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zenstruck\Browser\HttpBrowser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | trait HasHttpBrowser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** @var HttpBrowserClient[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private static array $httpBrowserClients = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @after | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     final public static function _resetHttpBrowserClients(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         self::$httpBrowserClients = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected function httpBrowser(array $kernelOptions = [], array $pantherOptions = []): HttpBrowser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if (!\is_a($class, HttpBrowser::class, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if (!$baseUri && !$this instanceof PantherTestCase) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (!$baseUri) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             self::startWebServer($pantherOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $baseUri = self::$baseUri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // copied from PantherTestCaseTrait::createHttpBrowserClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $client = new HttpBrowserClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $urlComponents = \parse_url($baseUri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $host = $urlComponents['host']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (isset($urlComponents['port'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $host .= ":{$urlComponents['port']}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $client->setServerParameter('HTTP_HOST', $host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ('https' === ($urlComponents['scheme'] ?? 'http')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $client->setServerParameter('HTTPS', 'true'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $browser = new $class(self::$httpBrowserClients[] = $client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if ($this instanceof KernelTestCase) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if (!static::$booted) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 static::bootKernel($kernelOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             if (static::$container->has('profiler')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $browser->setProfiler(static::$container->get('profiler')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         BrowserExtension::registerBrowser($browser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $browser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |