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