From d3eb1d3db709512f95ba79ed52305c2b87524a29 Mon Sep 17 00:00:00 2001 From: Fredrik Liljegren Date: Fri, 9 Oct 2015 09:22:07 +0200 Subject: [PATCH] Merging pull request #28 into devHeaderEncoding before master-merge. --- lib/Client.php | 24 +++++++++++++----------- tests/unit/ClientTest.php | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index 144dfee..05f8413 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -65,27 +65,29 @@ protected function connect() { $host_uri = ($scheme === 'wss' ? 'ssl' : 'tcp') . '://' . $host; // Set the stream context options if they're already set in the config - if(isset($this->options['context'])) { + if (isset($this->options['context'])) { // Suppress the error since we'll catch it below - if(@get_resource_type($this->options['context']) === 'stream-context') { + if (@get_resource_type($this->options['context']) === 'stream-context') { $context = $this->options['context']; - } else { + } + else { throw new \InvalidArgumentException( - "Stream context in \$options['context'] isn't a valid context" + "Stream context in \$options['context'] isn't a valid context" ); } - } else { + } + else { $context = stream_context_create(); } // Open the socket. @ is there to supress warning that we will catch in check below instead. $this->socket = @stream_socket_client( - $host_uri . ':' . $port, - $errno, - $errstr, - $this->options['timeout'], - STREAM_CLIENT_CONNECT, - $context + $host_uri . ':' . $port, + $errno, + $errstr, + $this->options['timeout'], + STREAM_CLIENT_CONNECT, + $context ); if ($this->socket === false) { diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 5f0162d..6a8606d 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -362,7 +362,10 @@ public function testSetFragmentSize($port) { $this->assertSame(123, $size); } - public function testSetStreamContextOptions() { + /** + * @dataProvider serverPortProvider + */ + public function testSetStreamContextOptions($port) { $context = stream_context_create(); stream_context_set_option($context, 'ssl', 'verify_peer', true); stream_context_set_option($context, 'ssl', 'verify_host', true); @@ -372,23 +375,27 @@ public function testSetStreamContextOptions() { 'context' => $context ); - $ws = new Client('ws://localhost:' . self::$port, $options); + $ws = new Client('ws://localhost:' . self::$ports[$port], $options); + // Do a send to touch the context using code in connect. We can't really assert that the + // stream has the correct context, but we make sure it doesn't crash. $ws->send('foo'); $this->assertTrue(get_resource_type($ws->options['context']) === 'stream-context'); } /** + * @dataProvider serverPortProvider + * * @expectedException \InvalidArgumentException * @expectedExceptionMessage Stream context in $options['context'] isn't a valid context */ - public function testSetInvalidStreamContextOptions() { + public function testSetInvalidStreamContextOptions($port) { $context = false; $options = array( - 'context' => $context + 'context' => $context ); - $ws = new Client('ws://localhost:' . self::$port, $options); + $ws = new Client('ws://localhost:' . self::$ports[$port], $options); $ws->send('foo'); } }