Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 5b36ca3

Browse files
committed
apply integer and numeric check
1 parent 41f1315 commit 5b36ca3

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/Client/Adapter/Curl.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,24 @@ public function connect($host, $port = 80, $secure = false)
202202
}
203203

204204
if (isset($this->config['connecttimeout'])) {
205-
$connectTimeout = (int) $this->config['connecttimeout'];
205+
$connectTimeout = $this->config['connecttimeout'];
206206
} elseif (isset($this->config['timeout'])) {
207-
$connectTimeout = (int) $this->config['timeout'];
207+
$connectTimeout = $this->config['timeout'];
208208
} else {
209209
$connectTimeout = null;
210210
}
211+
212+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
213+
throw new AdapterException\InvalidArgumentException(sprintf(
214+
'integer or numeric string expected, got %s',
215+
gettype($connectTimeout)
216+
));
217+
}
218+
219+
if ($connectTimeout !== null) {
220+
$connectTimeout = (int) $connectTimeout;
221+
}
222+
211223
if ($connectTimeout !== null) {
212224
if (defined('CURLOPT_CONNECTTIMEOUT_MS')) {
213225
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000);

test/Client/CurlTest.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,34 @@ public function testConfigSetAsZendConfig()
9696
$this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
9797
}
9898

99+
public function provideValidTimeoutConfig()
100+
{
101+
return [
102+
'integer' => [10],
103+
'numeric' => ['10'],
104+
];
105+
}
106+
99107
/**
100-
* Test not integer timeout casted to int
101-
*
108+
* @dataProvider provideValidTimeoutConfig
102109
*/
103-
public function testCastTimeOutConfig()
110+
public function testPassValidTimeout($timeout)
104111
{
105-
$config = new Config([
106-
'timeout' => "timeout",
107-
]);
112+
$adapter = new Adapter\Curl();
113+
$adapter->setOptions(['timeout' => $timeout]);
108114

109-
$this->_adapter->setOptions($config);
115+
$adapter->connect('http://framework.zend.com');
116+
}
110117

111-
$hasConfig = $this->_adapter->getConfig();
112-
$this->assertEquals((int) $config->timeout, $hasConfig['timeout']);
118+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
119+
{
120+
$adapter = new Adapter\Curl();
121+
$adapter->setOptions(['timeout' => 'timeout']);
122+
123+
$this->expectException(InvalidArgumentException::class);
124+
$this->expectExceptionMessage('integer or numeric string expected, got string');
125+
126+
$adapter->connect('http://framework.zend.com');
113127
}
114128

115129
/**

0 commit comments

Comments
 (0)