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

Commit 4dc2c30

Browse files
committed
apply integer and numeric check in Socket adapter
1 parent 5b36ca3 commit 4dc2c30

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Client/Adapter/Socket.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ public function connect($host, $port = 80, $secure = false)
262262
} else {
263263
$connectTimeout = $this->config['timeout'];
264264
}
265+
266+
if ($connectTimeout !== null && (! is_int($connectTimeout) || ! is_numeric($connectTimeout))) {
267+
throw new AdapterException\InvalidArgumentException(sprintf(
268+
'integer or numeric string expected, got %s',
269+
gettype($connectTimeout)
270+
));
271+
}
272+
265273
ErrorHandler::start();
266274
$this->socket = stream_socket_client(
267275
$host . ':' . $port,

test/Client/SocketTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ public function testSetConfigInvalidConfig($config)
171171
$this->_adapter->setOptions($config);
172172
}
173173

174+
public function provideValidTimeoutConfig()
175+
{
176+
return [
177+
'integer' => [10],
178+
'numeric' => ['10'],
179+
];
180+
}
181+
182+
/**
183+
* @dataProvider provideValidTimeoutConfig
184+
*/
185+
public function testPassValidTimeout($timeout)
186+
{
187+
$adapter = new Adapter\Socket();
188+
$adapter->setOptions(['timeout' => $timeout]);
189+
190+
$adapter->connect('http://framework.zend.com');
191+
}
192+
193+
public function testThrowInvalidArgumentExceptionOnNonIntegerAndNonNumericStringTimeout()
194+
{
195+
$adapter = new Adapter\Socket();
196+
$adapter->setOptions(['timeout' => 'timeout']);
197+
198+
$this->expectException(InvalidArgumentException::class);
199+
$this->expectExceptionMessage('integer or numeric string expected, got string');
200+
201+
$adapter->connect('http://framework.zend.com');
202+
}
203+
174204
/**
175205
* Stream context related tests
176206
*/

0 commit comments

Comments
 (0)