| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Browser\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Security\Core\User\InMemoryUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Zenstruck\Browser\KernelBrowser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zenstruck\Browser\Tests\Fixture\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author Kevin Bond <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | trait KernelBrowserTests | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     use BrowserKitBrowserTests; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     public function can_use_kernel_browser_as_typehint(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             ->use(function(KernelBrowser $browser) { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |                 $browser->visit('/redirect1'); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             }) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             ->assertOn('/page1') | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function can_act_as_user(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         if (!Kernel::securityEnabled()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             $this->markTestSkipped('Only enable security-related tests in 5.3+'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ->throwExceptions() | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             ->actingAs(new InMemoryUser('kevin', 'pass')) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             ->visit('/user') | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             ->assertSee('user: kevin/pass') | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function can_enable_exception_throwing(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->expectException(\Exception::class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->expectExceptionMessage('exception thrown'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->browser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ->throwExceptions() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |             ->visit('/exception') | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function can_re_enable_catching_exceptions(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $browser = $this->browser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $browser->throwExceptions()->visit('/exception'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $browser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 ->catchExceptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ->visit('/exception') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ->assertStatus(500) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |         $this->fail('Exception was not caught.'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function can_enable_the_profiler(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $profile = $this->browser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->withProfiling() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ->visit('/page1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->profile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |         $this->assertTrue($profile->hasCollector('request')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     protected function browser(): KernelBrowser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |         return $this->kernelBrowser(); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 101 |  |  |  |