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