<commit_before><?php

namespace PhpAmqpLib\Tests\Functional;

use PhpAmqpLib\Connection\AMQPSocketConnection;
use PhpAmqpLib\Exception\AMQPProtocolChannelException;

class ChannelTest extends AbstractPublishConsumeTest
{
    protected function createConnection()
    {
        return new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST);
    }

    public function testDeclaringExchangeOnChannelWithClosedConnectionThrowsException()
    {
        $connection = $this->createConnection();
        $channel = $connection->channel();

        try {
            $channel->exchange_declare('tst.exchange2', 'topic', true);
        } catch (AMQPProtocolChannelException $e) {
            // Do Nothing
        }

        $this->setExpectedException('\PhpAmqpLib\Exception\AMQPRuntimeException', 'Channel connection is closed.');
        $channel->exchange_declare('tst.exchange2', 'topic', false, true, false);

        $channel->close();
        $connection->close();
    }
}<commit_msg>add channel test for delivery tag<commit_after><?php

namespace PhpAmqpLib\Tests\Functional;

use PhpAmqpLib\Connection\AMQPSocketConnection;
use PhpAmqpLib\Exception\AMQPBasicCancelException;
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
use PhpAmqpLib\Exception\AMQPTimeoutException;
use PhpAmqpLib\Helper\Protocol\Wait091;
use PhpAmqpLib\Message\AMQPMessage;

class ChannelTest extends AbstractPublishConsumeTest
{
    protected function createConnection()
    {
        return new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST);
    }

    public function testDeclaringExchangeOnChannelWithClosedConnectionThrowsException()
    {
        $connection = $this->createConnection();
        $channel = $connection->channel();

        try {
            $channel->exchange_declare('tst.exchange2', 'topic', true);
        } catch (AMQPProtocolChannelException $e) {
            // Do Nothing
        }

        $this->setExpectedException('\PhpAmqpLib\Exception\AMQPRuntimeException', 'Channel connection is closed.');
        $channel->exchange_declare('tst.exchange2', 'topic', false, true, false);

        $channel->close();
        $connection->close();
    }

    /**
     * @group by
     */
    public function testPublishWithConfirm()
    {
        $connection = $this->createConnection();
        $connection->connectOnConstruct();
        $channel = $connection->channel();

        try {
            $channel->exchange_declare('tst.exchange3', 'topic', false);
        } catch (AMQPProtocolChannelException $e) {
            // Do Nothing
        }

        $deliveryTags = [];

        $channel->set_ack_handler(function (AMQPMessage $message) use (&$deliveryTags) {
            $deliveryTags[] = (int) $message->get('delivery_tag');
            return false;
        });

        $channel->confirm_select();

        $connection2 = $this->createConnection();
        $channel2 = $connection2->channel();

        $channel2->queue_declare('tst.queue3');
        $channel2->queue_bind('tst.queue3', 'tst.exchange3', '#');

        $channel->basic_publish(new AMQPMessage('foo'), 'tst.exchange3');
        $channel->basic_publish(new AMQPMessage('bar'), 'tst.exchange3');

        $publishedMessagesProperty = new \ReflectionProperty(get_class($this->channel), 'published_messages');
        $publishedMessagesProperty->setAccessible(true);

        $channel->wait_for_pending_acks_returns(1);

        $msg1 = $channel2->basic_get('tst.queue3');
        $this->assertInstanceOf(AMQPMessage::class, $msg1);
        $msg2 = $channel2->basic_get('tst.queue3');
        $this->assertInstanceOf(AMQPMessage::class, $msg2);

        $this->assertSame('foo', $msg1->getBody());
        $this->assertSame('bar', $msg2->getBody());

        $this->assertSame([1,2], $deliveryTags);

        $channel2->queue_delete('tst.queue3');
        $channel->exchange_delete('tst.exchange3');
    }
}
{'fraction_non_alphanumeric': 0.1352185089974293, 'fraction_numerical': 0.008740359897172237, 'mean_word_length': 3.3233333333333333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179979', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1103, 'n_words': 177}