[Mailer] fixed error message when connecting to a stream raises an error before connect()

This commit is contained in:
Fabien Potencier 2019-06-27 11:08:50 +02:00
parent 791a2127aa
commit eb15bffa78
2 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Tests\Transport\Smtp\Stream;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class SocketStreamTest extends TestCase
{
/**
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
* @expectedExceptionMessage Connection refused
*/
public function testSocketErrorNoConnection()
{
$s = new SocketStream();
$s->setTimeout(0.1);
$s->setPort(9999);
$s->initialize();
}
/**
* @expectedException \Symfony\Component\Mailer\Exception\TransportException
* @expectedExceptionMessage no valid certs found cafile stream
*/
public function testSocketErrorBeforeConnectError()
{
$s = new SocketStream();
$s->setStreamOptions([
'ssl' => [
// not a CA file :)
'cafile' => __FILE__,
],
]);
$s->setEncryption('ssl');
$s->setHost('smtp.gmail.com');
$s->setPort(465);
$s->initialize();
}
}

View File

@ -144,10 +144,16 @@ final class SocketStream extends AbstractStream
$options['ssl']['crypto_method'] = $options['ssl']['crypto_method'] ?? STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}
$streamContext = stream_context_create($options);
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->stream) {
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
set_error_handler(function ($type, $msg) {
throw new TransportException(sprintf('Connection could not be established with host "%s": %s.', $this->url, $msg));
});
try {
$this->stream = stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
} finally {
restore_error_handler();
}
stream_set_blocking($this->stream, true);
stream_set_timeout($this->stream, $this->timeout);
$this->in = &$this->stream;