File: AMQPMessageTest.php

package info (click to toggle)
php-amqplib 3.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,060 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (156 lines) | stat: -rw-r--r-- 4,492 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

namespace PhpAmqpLib\Tests\Unit\Message;

use PhpAmqpLib\Exception\AMQPEmptyDeliveryTagException;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPBufferReader;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class AMQPMessageTest extends TestCase
{
    #[DataProvider('propertiesData')]
    #[Test]
    public function serialize_properties(array $expected, array $properties)
    {
        $reader = new AMQPBufferReader('');
        $message = new AMQPMessage('', $properties);

        $encodedData = $message->serialize_properties();
        $reader->reset($encodedData);
        $message->load_properties($reader);
        $props = $message->get_properties();

        if (isset($props['application_headers'])) {
            $props['application_headers'] = $props['application_headers']->getNativeData();
        }

        $this->assertEquals($expected, $props);
    }

    #[Test]
    public function get_and_set_body()
    {
        $message = new AMQPMessage('');
        $message->setBody('body');
        $message->setIsTruncated(true);
        $message->content_encoding = 'shortstr';

        $this->assertEquals($message->getBody(), 'body');
        $this->assertTrue($message->isTruncated());
        $this->assertEquals($message->getContentEncoding(), 'shortstr');
    }

    #[Test]
    public function delivery_tag_immutable()
    {
        $message = new AMQPMessage();
        $message->setDeliveryTag('10');
        $this->assertEquals(10, $message->getDeliveryTag());

        $this->expectException(\LogicException::class);
        $message->setDeliveryTag(1231);
    }

    #[Test]
    public function delivery_tag_empty()
    {
        $this->expectException(AMQPEmptyDeliveryTagException::class);
        $message = new AMQPMessage();
        $message->getDeliveryTag();
    }

    public static function propertiesData()
    {
        return [
            [
            ['priority' => 1, 'timestamp' => time()],
            ['priority' => 1, 'timestamp' => time()],
            ],
            [
            ['message_id' => '5414cfa74899a'],
            ['message_id' => '5414cfa74899a'],
            ],
            [
            ['message_id' => 0],
            ['message_id' => 0],
            ],
            [
            [],
            ['timestamp' => null],
            ],
            [
            [],
            ['priority' => null],
            ],
            [
            ['priority' => 0],
            ['priority' => 0],
            ],
            [
            ['priority' => false],
            ['priority' => false],
            ],
            [
            ['priority' => '0'],
            ['priority' => '0'],
            ],
            [
            ['application_headers' => ['x-foo' => '']],
            ['application_headers' => ['x-foo' => ['S', '']]],
            ],
            [
            ['application_headers' => ['x-foo' => '']],
            ['application_headers' => ['x-foo' => ['S', null]]],
            ],
            [
            ['application_headers' => ['x-foo' => 0]],
            ['application_headers' => ['x-foo' => ['I', 0]]],
            ],
            [
            ['application_headers' => ['x-foo' => 1]],
            ['application_headers' => ['x-foo' => ['I', true]]],
            ],
            [
            ['application_headers' => ['x-foo' => 0]],
            ['application_headers' => ['x-foo' => ['I', '0']]],
            ],
            [
            ['application_headers' => ['x-foo' => []]],
            ['application_headers' => ['x-foo' => ['A', []]]],
            ],
            [
            ['application_headers' => ['x-foo' => [null]]],
            ['application_headers' => ['x-foo' => ['A', [null]]]],
            ],
        ];
    }

    public function ack_new_message()
    {
        $message = new AMQPMessage();

        $exception = null;
        try {
            $message->ack();
        } catch (\Exception $exception) {
        }
        $this->assertInstanceOf(\LogicException::class, $exception);

        $exception = null;
        try {
            $message->nack();
        } catch (\Exception $exception) {
        }
        $this->assertInstanceOf(\LogicException::class, $exception);

        $exception = null;
        try {
            $message->reject();
        } catch (\Exception $exception) {
        }
        $this->assertInstanceOf(\LogicException::class, $exception);
    }
}