| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the ScenarioStateBehatExtension project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Rodrigue Villetard <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Gorghoa\ScenarioStateBehatExtension\Call\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Behat\Behat\Transformation\Call\TransformationCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Behat\Testwork\Call\Call; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Behat\Testwork\Call\Handler\CallHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Behat\Testwork\Environment\Call\EnvironmentCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Behat\Testwork\Hook\Call\HookCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Gorghoa\ScenarioStateBehatExtension\Resolver\ArgumentsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Vincent Chalamon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class RuntimeCallHandler implements CallHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var CallHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $decorated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var ArgumentsResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $argumentsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param CallHandler       $decorated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param ArgumentsResolver $argumentsResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(CallHandler $decorated, ArgumentsResolver $argumentsResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->decorated = $decorated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->argumentsResolver = $argumentsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function supportsCall(Call $call) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->decorated->supportsCall($call); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     public function handleCall(Call $call) | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         /** @var \ReflectionMethod $function */ | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $function = $call->getCallee()->getReflection(); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $arguments = $call->getArguments(); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         if ($call instanceof HookCall) { | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             $scope = $call->getScope(); | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             // Manage `scope` argument | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             foreach ($function->getParameters() as $parameter) { | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                 if (null !== $parameter->getClass() && get_class($scope) === $parameter->getClass()->getName()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |                     $arguments[$parameter->getName()] = $scope; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |                     break; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         $arguments = $this->argumentsResolver->resolve($function, $arguments); | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         if ($call instanceof TransformationCall) { | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             $call = new TransformationCall($call->getEnvironment(), $call->getDefinition(), $call->getCallee(), $arguments); | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         } elseif ($call instanceof HookCall) { | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             $call = new EnvironmentCall($call->getScope()->getEnvironment(), $call->getCallee(), $arguments); | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         return $this->decorated->handleCall($call); | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |