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 (64 lines) | stat: -rw-r--r-- 2,042 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
<?php

namespace PhpAmqpLib\Tests\Functional\Message;

use LogicException;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\Functional\Channel\ChannelTestCase;
use PHPUnit\Framework\Attributes\Test;

/**
 * @group connection
 */
class AMQPMessageTest extends ChannelTestCase
{
    #[Test]
    public function double_ack_throws_exception()
    {
        $sent = new AMQPMessage('test' . mt_rand());
        list($queue) = $this->channel->queue_declare();
        $this->channel->basic_publish($sent, '', $queue);

        $received = $this->channel->basic_get($queue);

        self::assertSame($sent->getBody(), $received->getBody());
        self::assertNotEmpty($received->getDeliveryTag());
        self::assertSame($this->channel, $received->getChannel());
        self::assertEquals('', $received->getExchange());
        self::assertEquals($queue, $received->getRoutingKey());
        self::assertEquals(0, $received->getMessageCount());
        self::assertFalse($received->isRedelivered());
        self::assertFalse($received->isTruncated());

        $received->ack();

        // 2nd ack must throw logic exception
        $this->expectException(LogicException::class);
        $received->ack();
    }

    #[Test]
    public function publish_confirm_mode()
    {
        $message = new AMQPMessage('test' . mt_rand());
        $confirmed = null;
        list($queue) = $this->channel->queue_declare();
        $this->channel->set_ack_handler(
            function (AMQPMessage $message) use (&$confirmed) {
                $confirmed = $message;
            }
        );

        $this->channel->confirm_select();
        $this->channel->basic_publish($message, '', $queue);

        self::assertGreaterThan(0, $message->getDeliveryTag());
        self::assertEquals('', $message->getExchange());
        self::assertEquals($queue, $message->getRoutingKey());
        self::assertFalse($message->isRedelivered());

        $this->channel->wait_for_pending_acks(3);

        self::assertSame($message, $confirmed);
    }
}