Skip to content

Commit 7f6d276

Browse files
committed
Dependency injection; fixes #73
1 parent e6f3a4d commit 7f6d276

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

lib/WebDriver/AbstractWebDriver.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ abstract class AbstractWebDriver
4040
*/
4141
protected $url;
4242

43+
/**
44+
* Curl service
45+
*
46+
* @var \WebDriver\Service\CurlService
47+
*/
48+
private $curlService;
49+
4350
/**
4451
* Return array of supported method names and corresponding HTTP request methods
4552
*
@@ -87,6 +94,26 @@ public function getURL()
8794
return $this->url;
8895
}
8996

97+
/**
98+
* Set curl service
99+
*
100+
* @param \WebDriver\Service\CurlService $curlService
101+
*/
102+
public function setCurlService($curlService)
103+
{
104+
$this->curlService = $curlService;
105+
}
106+
107+
/**
108+
* Get curl service
109+
*
110+
* @return \WebDriver\Service\CurlService
111+
*/
112+
public function getCurlService()
113+
{
114+
return $this->curlService ?: ServiceFactory::getInstance()->getService('service.curl');
115+
}
116+
90117
/**
91118
* Curl request to webdriver server.
92119
*
@@ -120,7 +147,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
120147
$url .= '/' . $parameters;
121148
}
122149

123-
list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);
150+
list($rawResult, $info) = $this->curlService->execute($requestMethod, $url, $parameters, $extraOptions);
124151

125152
$httpCode = $info['http_code'];
126153

lib/WebDriver/SauceLabs/SauceRest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class SauceRest
4141
*/
4242
private $accessKey;
4343

44+
/**
45+
* Curl service
46+
*
47+
* @var \WebDriver\Service\CurlService
48+
*/
49+
private $curlService;
50+
4451
/**
4552
* Constructor
4653
*
@@ -53,6 +60,26 @@ public function __construct($userId, $accessKey)
5360
$this->accessKey = $accessKey;
5461
}
5562

63+
/**
64+
* Set curl service
65+
*
66+
* @param \WebDriver\Service\CurlService $curlService
67+
*/
68+
public function setCurlService($curlService)
69+
{
70+
$this->curlService = $curlService;
71+
}
72+
73+
/**
74+
* Get curl service
75+
*
76+
* @return \WebDriver\Service\CurlService
77+
*/
78+
public function getCurlService()
79+
{
80+
return $this->curlService ?: ServiceFactory::getInstance()->getService('service.curl');
81+
}
82+
5683
/**
5784
* Execute Sauce Labs REST API command
5885
*
@@ -82,7 +109,7 @@ protected function execute($requestMethod, $url, $parameters = null)
82109

83110
$url = 'https://saucelabs.com/rest/v1/' . $url;
84111

85-
list($rawResult, $info) = ServiceFactory::getInstance()->getService('service.curl')->execute($requestMethod, $url, $parameters, $extraOptions);
112+
list($rawResult, $info) = $this->curlService->execute($requestMethod, $url, $parameters, $extraOptions);
86113

87114
return json_decode($rawResult, true);
88115
}

lib/WebDriver/Service/CurlService.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
*/
3333
class CurlService implements CurlServiceInterface
3434
{
35+
/**
36+
* @var array
37+
*/
38+
private $defaultOptions;
39+
40+
/**
41+
* Constructor
42+
*
43+
* @param array $defaultOptions
44+
*/
45+
public function __construct($defaultOptions = array())
46+
{
47+
$this->defaultOptions = is_array($defaultOptions) ? $defaultOptions : array();
48+
}
49+
3550
/**
3651
* {@inheritdoc}
3752
*/
@@ -82,7 +97,7 @@ public function execute($requestMethod, $url, $parameters = null, $extraOptions
8297
break;
8398
}
8499

85-
foreach ($extraOptions as $option => $value) {
100+
foreach (array_merge($this->defaultOptions, $extraOptions) as $option => $value) {
86101
curl_setopt($curl, $option, $value);
87102
}
88103

0 commit comments

Comments
 (0)