File: SocketTimeoutTest.php

package info (click to toggle)
libphp-swiftmailer 6.3.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,164 kB
  • sloc: php: 27,203; sh: 36; makefile: 16
file content (65 lines) | stat: -rw-r--r-- 1,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

class Swift_Transport_StreamBuffer_SocketTimeoutTest extends \PHPUnit\Framework\TestCase
{
    protected $buffer;
    protected $server;
    protected $randomHighPort;

    protected function setUp(): void
    {
        if (!\defined('SWIFT_SMTP_HOST')) {
            $this->markTestSkipped(
                'Cannot run test without an SMTP host to connect to (define '.
                'SWIFT_SMTP_HOST in tests/acceptance.conf.php if you wish to run this test)'
             );
        }

        $serverStarted = false;
        for ($i = 0; $i < 5; ++$i) {
            $this->randomHighPort = random_int(50000, 65000);
            $this->server = stream_socket_server('tcp://127.0.0.1:'.$this->randomHighPort);
            if ($this->server) {
                $serverStarted = true;
            }
        }

        $this->buffer = new Swift_Transport_StreamBuffer(
            $this->createMock('Swift_ReplacementFilterFactory')
        );
    }

    protected function initializeBuffer()
    {
        $host = '127.0.0.1';
        $port = $this->randomHighPort;

        $this->buffer->initialize([
            'type' => Swift_Transport_IoBuffer::TYPE_SOCKET,
            'host' => $host,
            'port' => $port,
            'protocol' => 'tcp',
            'blocking' => 1,
            'timeout' => 1,
        ]);
    }

    public function testTimeoutException()
    {
        $this->initializeBuffer();
        $e = null;
        try {
            $line = $this->buffer->readLine(0);
        } catch (Exception $e) {
        }
        $this->assertInstanceOf('Swift_IoException', $e, 'IO Exception Not Thrown On Connection Timeout');
        $this->assertMatchesRegularExpression('/Connection to .* Timed Out/', $e->getMessage());
    }

    protected function tearDown(): void
    {
        if ($this->server) {
            stream_socket_shutdown($this->server, STREAM_SHUT_RDWR);
        }
    }
}