From 85b5d5b9e218006d8a8e32cac61a8d832ff34cba Mon Sep 17 00:00:00 2001 From: brendankay Date: Mon, 30 Jul 2018 08:59:27 +1200 Subject: [PATCH] Add timeout to socket --- composer.json | 2 +- src/TelnetClient.php | 5 +++-- tests/unit/TelnetClientTest.php | 8 +++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index ca4f7fb..c7e27ea 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ ], "require": { "php": ">5.5.0", - "clue/socket-raw": "^1.2" + "clue/socket-raw": "^1.3" }, "require-dev": { "phpunit/phpunit" : "4.*", diff --git a/src/TelnetClient.php b/src/TelnetClient.php index c667723..30e1c35 100644 --- a/src/TelnetClient.php +++ b/src/TelnetClient.php @@ -104,10 +104,11 @@ public function __construct( * @param string $prompt * @param string $promptError * @param string $lineEnding + * @param float|null $timeout * * @throws TelnetExceptionInterface */ - public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null) + public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null, $timeout = null) { if ($prompt !== null) { $this->setPrompt($prompt); @@ -122,7 +123,7 @@ public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = } try { - $socket = $this->socketFactory->createClient($dsn); + $socket = $this->socketFactory->createClient($dsn, $timeout); } catch (Exception $e) { throw new TelnetException(sprintf('unable to create socket connection to [%s]', $dsn), 0, $e); } diff --git a/tests/unit/TelnetClientTest.php b/tests/unit/TelnetClientTest.php index 0369942..03a8289 100644 --- a/tests/unit/TelnetClientTest.php +++ b/tests/unit/TelnetClientTest.php @@ -19,16 +19,17 @@ class TelnetClientTest extends \PHPUnit_Framework_TestCase * @param string $prompt * @param string $promptError * @param string $lineEnding + * @param float|null $timeout * * @return void */ - public function testConnect($prompt = null, $promptError = null, $lineEnding = null) + public function testConnect($prompt = null, $promptError = null, $lineEnding = null, $timeout = null) { $dsn = 'localhost:80'; $socket = m::mock(Socket::class); $socketFactory = m::mock(SocketFactory::class) ->shouldReceive('createClient') - ->with($dsn) + ->with($dsn, $timeout) ->andReturn($socket) ->once() ->getMock(); @@ -75,7 +76,7 @@ public function testConnect($prompt = null, $promptError = null, $lineEnding = n } $telnetClient = $telnetClient->getMock()->makePartial(); - $telnetClient->connect($dsn, $prompt, $promptError, $lineEnding); + $telnetClient->connect($dsn, $prompt, $promptError, $lineEnding, $timeout); } /** @@ -85,6 +86,7 @@ public function dataProviderConnect() { return [ ['PROMPT', 'PROMPTERROR', 'LINENDING'], + ['PROMPT', 'PROMPTERROR', 'LINENDING', 5], [null, null, null] ]; }