<commit_before><?php
/**
 * PHP version 7.1
 *
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
 */
namespace Hexagonal\Messaging;

use Exception;
use Hexagonal\DataBuilders\A;
use Hexagonal\DomainEvents\{EventStore, StoredEvent};
use Mockery;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;

class MessagePublisherTest extends TestCase
{
    /** @test */
    function it_publishes_a_single_message_to_an_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $aSingleMessage = [$message = A::storedEvent()->build()];
        $this->store->allEvents()->willReturn($aSingleMessage);
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(false);
        $this->tracker->track(Argument::type(PublishedMessage::class))->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this->producer->send($exchangeName, $message)->shouldHaveBeenCalled();
        $this->assertEquals(
            1,
            $messages,
            'Should have processed only 1 message'
        );
    }

    /** @test */
    function it_publishes_several_messages_to_an_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $severalMessages = [
            A::storedEvent()->build(),
            A::storedEvent()->build(),
            A::storedEvent()->build(),
        ];
        $messagesCount = count($severalMessages);
        $this->store->allEvents()->willReturn($severalMessages);
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(false);
        $this->tracker->track(Argument::type(PublishedMessage::class))->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this
            ->producer
            ->send($exchangeName, Argument::type(StoredEvent::class))
            ->shouldHaveBeenCalledTimes($messagesCount)
        ;
        $this->assertEquals(
            $messagesCount,
            $messages,
            'Should have processed 3 messages'
        );
    }

    /** @test */
    function it_publishes_a_single_message_to_a_non_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $aSingleMessage = [
            $eventMessage = A::storedEvent()->withId(11000)->build(),
        ];
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(true);
        $message = A::publishedMessage()
            ->withExchangeName($exchangeName)
            ->build()
        ;
        $this
            ->store
            ->eventsStoredAfter($message->mostRecentMessageId())
            ->willReturn($aSingleMessage)
        ;
        $this->tracker->mostRecentPublishedMessage($exchangeName)->willReturn($message);
        $this->tracker->track($message)->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this->producer->send($exchangeName, $eventMessage)->shouldHaveBeenCalled();
        $this->assertEquals(
            1,
            $messages,
            'Should have processed only 1 message'
        );
        $this->assertEquals(
            11000,
            $message->mostRecentMessageId(),
            'Most recent message ID should be 11000'
        );
    }

    /** @test */
    function it_publishes_several_messages_to_a_non_empty_exchange()
    {
        $nonEmptyExchangeName = 'non_empty_exchange_name';
        $severalMessages = [
            A::storedEvent()->build(),
            A::storedEvent()->build(),
            $eventMessage = A::storedEvent()->withId(11000)->build(),
        ];

        $store = $this->givenAStoreWith($severalMessages);
        $tracker = $this->givenANonEmptyExchange($nonEmptyExchangeName);
        $message = A::publishedMessage()
            ->withExchangeName($nonEmptyExchangeName)
            ->build()
        ;
        $this->givenMostRecentPublishedMessageIs($tracker, $message, $nonEmptyExchangeName);
        $this->expectToTrackUpdatedMessage($tracker, $message);
        $producer = $this->expectToProcessAll($severalMessages, $nonEmptyExchangeName);

        $publisher = new MessagePublisher($store, $tracker, $producer);

        $messages = $publisher->publishTo($nonEmptyExchangeName);

        $this->assertEquals(
            3,
            $messages,
            'Should have processed 3 messages'
        );
        $this->assertEquals(
            11000,
            $message->mostRecentMessageId(),
            'Most recent message ID should be 11000'
        );<commit_msg>Refactor several messages, non empty exchange test.<commit_after><?php
/**
 * PHP version 7.1
 *
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
 */
namespace Hexagonal\Messaging;

use Exception;
use Hexagonal\DataBuilders\A;
use Hexagonal\DomainEvents\{EventStore, StoredEvent};
use Mockery;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;

class MessagePublisherTest extends TestCase
{
    /** @test */
    function it_publishes_a_single_message_to_an_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $aSingleMessage = [$message = A::storedEvent()->build()];
        $this->store->allEvents()->willReturn($aSingleMessage);
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(false);
        $this->tracker->track(Argument::type(PublishedMessage::class))->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this->producer->send($exchangeName, $message)->shouldHaveBeenCalled();
        $this->assertEquals(
            1,
            $messages,
            'Should have processed only 1 message'
        );
    }

    /** @test */
    function it_publishes_several_messages_to_an_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $severalMessages = [
            A::storedEvent()->build(),
            A::storedEvent()->build(),
            A::storedEvent()->build(),
        ];
        $messagesCount = count($severalMessages);
        $this->store->allEvents()->willReturn($severalMessages);
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(false);
        $this->tracker->track(Argument::type(PublishedMessage::class))->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this
            ->producer
            ->send($exchangeName, Argument::type(StoredEvent::class))
            ->shouldHaveBeenCalledTimes($messagesCount)
        ;
        $this->assertEquals(
            $messagesCount,
            $messages,
            'Should have processed 3 messages'
        );
    }

    /** @test */
    function it_publishes_a_single_message_to_a_non_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $aSingleMessage = [
            $eventMessage = A::storedEvent()->withId(11000)->build(),
        ];
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(true);
        $message = A::publishedMessage()
            ->withExchangeName($exchangeName)
            ->build()
        ;
        $this
            ->store
            ->eventsStoredAfter($message->mostRecentMessageId())
            ->willReturn($aSingleMessage)
        ;
        $this->tracker->mostRecentPublishedMessage($exchangeName)->willReturn($message);
        $this->tracker->track($message)->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this->producer->send($exchangeName, $eventMessage)->shouldHaveBeenCalled();
        $this->assertEquals(
            1,
            $messages,
            'Should have processed only 1 message'
        );
        $this->assertEquals(
            11000,
            $message->mostRecentMessageId(),
            'Most recent message ID should be 11000'
        );
    }

    /** @test */
    function it_publishes_several_messages_to_a_non_empty_exchange()
    {
        $exchangeName = 'exchange_name';
        $severalMessages = [
            A::storedEvent()->build(),
            A::storedEvent()->build(),
            $eventMessage = A::storedEvent()->withId(11000)->build(),
        ];
        $messagesCount = count($severalMessages);
        $message = A::publishedMessage()
            ->withExchangeName($exchangeName)
            ->build()
        ;
        $this
            ->store
            ->eventsStoredAfter($message->mostRecentMessageId())
            ->willReturn($severalMessages)
        ;
        $this->tracker->hasPublishedMessages($exchangeName)->willReturn(true);
        $this
            ->tracker
            ->mostRecentPublishedMessage($exchangeName)
            ->willReturn($message)
        ;
        $this->tracker->track($message)->shouldBeCalled();

        $messages = $this->publisher->publishTo($exchangeName);

        $this
            ->producer
            ->send($exchangeName, Argument::type(StoredEvent::class))
            ->shouldHaveBeenCalledTimes($messagesCount)
        ;
        $this->assertEquals(
            3,
            $messages,
            'Should have processed 3 messages'
        );
        $this->assertEquals(
            11000,
            $message->mostRecentMessageId(),
            'Most recent message ID should be 11000'
        );
{'fraction_non_alphanumeric': 0.13395068375148056, 'fraction_numerical': 0.008398837084096049, 'mean_word_length': 2.473448017950636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 163, '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': '2499143', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2425, 'n_words': 384}