| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace WSW\BrowserAutomation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Facebook\WebDriver\WebDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WSW\BrowserAutomation\Driver\DriverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WSW\BrowserAutomation\Report\CliReport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WSW\BrowserAutomation\Report\ReportInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WSW\BrowserAutomation\Scenario\ScenarioRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use WSW\BrowserAutomation\Scenario\ScenarioRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package WSW\BrowserAutomation | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | final class Application | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var WebDriver | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private $driver; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var ReportInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     private $report; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @var ScenarioRepositoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     private $scenario; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * Application constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @param DriverInterface $webDriver | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param ReportInterface $report | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @param ScenarioRepositoryInterface $scenario | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 9 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         DriverInterface $webDriver, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         ReportInterface $report = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         ScenarioRepositoryInterface $scenario = null | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 9 |  |         $this->driver = $webDriver->getDriver(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 9 |  |         $this->report    = $report ?? new CliReport; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 9 |  |         $this->scenario  = $scenario ?? new ScenarioRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 9 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Add new scenario in collection. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @param callable $callback | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @return self | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 6 |  |     public function scenario(string $name, callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |         $this->scenario->add($name, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 | 3 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @param array $scenarios | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |     public function run(array $scenarios = []): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 6 |  |         $scenarios = count($scenarios) ? $scenarios : ['default']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  |         $result = $this->scenario->dispatch($scenarios, $this->driver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |         $this->driver->quit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         $this->report->toReport($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 | 3 |  |         return $result; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |