| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Behatch\HttpCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Behat\EventDispatcher\Event\StepTested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Behat\Behat\EventDispatcher\Event\AfterStepTested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Behat\Behat\Tester\Result\ExecutedStepResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Behat\Mink\Mink; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class HttpCallListener implements EventSubscriberInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $contextSupportedVoter; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $httpCallResultPool; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $mink; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct(ContextSupportedVoter $contextSupportedVoter, HttpCallResultPool $httpCallResultPool, Mink $mink) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->contextSupportedVoter = $contextSupportedVoter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->httpCallResultPool = $httpCallResultPool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->mink = $mink; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public static function getSubscribedEvents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |            StepTested::AFTER => 'afterStep' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function afterStep(AfterStepTested $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $testResult = $event->getTestResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (!$testResult instanceof ExecutedStepResult) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $httpCallResult = new HttpCallResult( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $testResult->getCallResult()->getReturn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($this->contextSupportedVoter->vote($httpCallResult)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->httpCallResultPool->store($httpCallResult); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // For now to avoid modification on MinkContext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // We add fallback on Mink | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if ($this->mink->getSession()->isStarted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->httpCallResultPool->store( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     new HttpCallResult($this->mink->getSession()->getPage()->getContent()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } catch (\LogicException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             // Mink has no response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } catch (\Behat\Mink\Exception\DriverException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             // No Mink | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  |