title: Exception not handled in correct method using PhpUnit
body: <p>I'm trying to create a library for a personal project using php sockets. For that I started using phpUnit, to learn and to write a (more or less) qualitative library.</p>
  
  <p>When i don't provide the try/catch block in the testConnection method, php gives an error that the connection has timed out (wich is normal, 'cause the device isn't connected). But php should handle the exception in the execute method below, not in the testConnection method. And I can't seem to figure this out. </p>
  
  <p>This is the error:</p>
  
  <pre><code>PHPUnit_Framework_Error_Warning : stream_socket_client(): unable to connect to tcp://x.x.x.x:* (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
  </code></pre>
  
  <p>Testclass with method and try/catch that should not be there:</p>
  
  <pre><code>public function testConnection() {
      $adu = new Adu();
      $adu-&gt;setPort('AS0');
      $adu-&gt;setData('?');
  
      $command = new Command('x.x.x.x', *);
      $command-&gt;setAduSent($adu);
  
      try
      {
          $command-&gt;execute();
      }
      catch (Exception $e)
      {
          echo $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p>This (the execute method) is where the Exception should be handled:</p>
  
  <pre><code>public function execute()
  {
      try {
          $this-&gt;stream = $this-&gt;createStream($this-&gt;address, $this-&gt;port, $this-&gt;timeout);
      }
      catch(Exception $e) {
          $this-&gt;logger-&gt;error('Exception (' . $e-&gt;getCode() . '): ' . $e-&gt;getMessage() . ' on line ' . $e-&gt;getLine(), $e);
      }
  
      $this-&gt;send($this-&gt;stream, $this-&gt;aduSent);
      $this-&gt;aduReceived = $this-&gt;receive($this-&gt;stream);
  }
  
  private function createStream($address, $port, $timeout = 2)
  {
      $stream = stream_socket_client('tcp://' . $address . ':' . $port, $errorCode, $errorMessage, $timeout);
  
      if(!$stream) {
          throw new Exception('Failed to connect(' . $errorCode . '): ' . $errorMessage);
      }
  
      return $stream;
  }
  </code></pre>
  
  <h1>Solution</h1>
  
  <p>Because a try/catch won't catch errors/warnings I had to suppress the warnings triggered by the stream_socket_client. An then check if the return value is false or a stream object. If false, throw an appropriate Exception.</p>
  
  <pre><code>$stream = @stream_socket_client('tcp://' . $address . ':' . $port, $errorCode, $errorMessage, $timeout);
  </code></pre>