| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zumba\Mink\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Mink\Driver\CoreDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Behat\Mink\Exception\DriverException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Behat\Mink\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zumba\GastonJS\Browser\Browser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class BasePhantomJSDriver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Zumba\Mink\Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class BasePhantomJSDriver extends CoreDriver { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   /** @var  Session */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   protected $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |   /** @var  Browser */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |   protected $browser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |   /** @var  string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   protected $phantomHost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |   /** @var  \Twig_Loader_Filesystem */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   protected $templateLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |   /** @var  \Twig_Environment */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   protected $templateEnv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |    * Instantiates the driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    * @param string $phantomHost browser "api" oriented host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |    * @param string $templateCache where we are going to store the templates cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |   public function __construct($phantomHost, $templateCache = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     $this->phantomHost = $phantomHost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     $this->browser = new Browser($phantomHost); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     $this->templateLoader = new \Twig_Loader_Filesystem(realpath(__DIR__ . '/Resources/Script')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     $this->templateEnv = new \Twig_Environment($this->templateLoader, array('cache' => $this->templateCacheSetup($templateCache), 'strict_variables' => true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    * Sets up the cache template location for the scripts we are going to create with the driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * @param $templateCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |    * @throws DriverException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |   protected function templateCacheSetup($templateCache) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     $cacheDir = $templateCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     if ($templateCache === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "jcalderonzumba" . DIRECTORY_SEPARATOR . "phantomjs"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |       if (!file_exists($cacheDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         mkdir($cacheDir, 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     if (!file_exists($cacheDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |       throw new DriverException("Template cache $cacheDir directory does not exist"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     return $cacheDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |    * Helper to find a node element given an xpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |    * @param string $xpath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |    * @param int    $max | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |    * @returns int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |    * @throws DriverException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |   protected function findElement($xpath, $max = 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     $elements = $this->browser->find("xpath", $xpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     if (!isset($elements["page_id"]) || !isset($elements["ids"]) || count($elements["ids"]) !== $max) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |       throw new DriverException("Failed to get elements with given $xpath"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     return $elements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |    * @return Browser | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |   public function getBrowser() { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     return $this->browser; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |    * @return \Twig_Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |   public function getTemplateEnv() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     return $this->templateEnv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |    * Returns a javascript script via twig template engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |    * @param $templateName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |    * @param $viewData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |   public function javascriptTemplateRender($templateName, $viewData) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** @var $templateEngine \Twig_Environment */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     $templateEngine = $this->getTemplateEnv(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     return $templateEngine->render($templateName, $viewData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |