[Mailer] fixed error message when connecting to a stream raises an error before connect()
This commit is contained in:
parent
791a2127aa
commit
eb15bffa78
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
$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);
|
$streamContext = stream_context_create($options);
|
||||||
$this->stream = @stream_socket_client($this->url, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $streamContext);
|
|
||||||
if (false === $this->stream) {
|
set_error_handler(function ($type, $msg) {
|
||||||
throw new TransportException(sprintf('Connection could not be established with host "%s": %s (%s)', $this->url, $errstr, $errno));
|
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_blocking($this->stream, true);
|
||||||
stream_set_timeout($this->stream, $this->timeout);
|
stream_set_timeout($this->stream, $this->timeout);
|
||||||
$this->in = &$this->stream;
|
$this->in = &$this->stream;
|
||||||
|
Reference in New Issue
Block a user