File: AbstractConnectionTestCase.php

package info (click to toggle)
php-amqplib 3.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,044 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (118 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

namespace PhpAmqpLib\Tests\Unit\Connection;

use PhpAmqpLib\Channel\Frame;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\AMQPConnectionConfig;
use PhpAmqpLib\Connection\AMQPConnectionFactory;
use PhpAmqpLib\Exception\AMQPConnectionClosedException;
use PhpAmqpLib\Tests\Unit\Test\TestConnection;
use PhpAmqpLib\Wire\IO\AbstractIO;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class AbstractConnectionTestCase extends TestCase
{
    #[Test]
    public function connection_argument_io_not_empty(): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Argument $io cannot be null');

        new TestConnection('', '');
    }

    #[Test]
    public function connection_login_method_external(): void
    {
        $config = new AMQPConnectionConfig();
        $config->setIsLazy(true);
        $config->setUser('');
        $config->setPassword('');
        $config->setLoginMethod(AMQPConnectionConfig::AUTH_EXTERNAL);
        $config->setLoginResponse($response = 'response');

        $connection = AMQPConnectionFactory::create($config);

        $reflection = new \ReflectionClass($connection);
        $property = $reflection->getProperty('login_response');
        $property->setAccessible(true);
        self::assertEquals($response, $property->getValue($connection));
    }

    #[RequiresPhpunit('< 12')]
    #[Test]
    public function close_channels_if_disconnected(): void
    {
        $ioMock = $this->createMock(AbstractIO::class);
        $config = new AMQPConnectionConfig();
        $config->setIsLazy(false);

        $args = [
            $user = null,
            $password = null,
            $vhost = '/',
            $insist = false,
            $login_method = 'AMQPLAIN',
            $login_response = null,
            $locale = 'en_US',
            $ioMock,
            $heartbeat = 0,
            $connectionTimeout = 0,
            $channelRpcTimeout = 0.0,
            $config
        ];

        /** @var MockObject&AbstractConnection $connection */
        $connection = $this->getMockForAbstractClass(
            AbstractConnection::class,
            $args,
            $mockClassName = '',
            $callOriginalConstructor = true,
            $callOriginalClone = true,
            $callAutoload = true,
            $mockedMethods = [
                'connect',
                'send_channel_method_frame',
                'wait_channel',
            ]
        );

        // Emulate channel.open_ok
        $payload = pack('n2', 20, 11);
        $connection
            ->method('wait_channel')
            ->willReturn(new Frame(
                Frame::TYPE_METHOD,
                1,
                mb_strlen($payload),
                $payload
            ));
        $newChannel = $connection->channel(1);
        $this->assertTrue($newChannel->is_open());

        // Emulate error to call do_close
        $ioMock
            ->expects($this->once())
            ->method('select')
            ->willThrowException(new AMQPConnectionClosedException('test'));
        $ioMock
            ->expects($this->once())
            ->method('close');

        $exception = null;
        try {
            $connection->select(3);
        } catch (AMQPConnectionClosedException $exception) {
        }

        $this->assertInstanceOf(AMQPConnectionClosedException::class, $exception);
        $this->assertEquals('test', $exception->getMessage());

        // After do_close is called, channel must be inactive too
        $this->assertFalse($newChannel->is_open());
    }
}