Skip to content

Commit

Permalink
TASK: Sort failed messages in reverse order (#59)
Browse files Browse the repository at this point in the history
Resolves: #58
  • Loading branch information
sabbelasichon authored Nov 6, 2023
1 parent 6638b45 commit 4c536fd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Classes/Repository/FailedMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Ssch\T3Messenger\Repository;

use Ssch\T3Messenger\Domain\Dto\FailedMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;
use TYPO3\CMS\Core\SingletonInterface;
Expand All @@ -37,7 +38,7 @@ public function list(): array
continue;
}

$failedMessages = $failureTransport->all();
$failedMessages = $this->inReverseOrder($failureTransport->all());

foreach ($failedMessages as $failedMessage) {
$allFailedMessages[] = FailedMessage::createFromEnvelope($failedMessage);
Expand All @@ -46,4 +47,18 @@ public function list(): array

return $allFailedMessages;
}

/**
* @param Envelope[] $failedMessages
*
* @return Envelope[];
*/
private function inReverseOrder(iterable $failedMessages): array
{
if (! is_array($failedMessages)) {
$failedMessages = iterator_to_array($failedMessages);
}

return array_reverse($failedMessages);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "t3_messenger" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command;

final class MyOtherFailingCommand
{
private string $note;

public function __construct(string $note)
{
$this->note = $note;
}

public function getNote(): string
{
return $this->note;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyFailingCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyOtherCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyOtherFailingCommand;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;

final class MyMessengerHandler implements MessageSubscriberInterface, LoggerAwareInterface
Expand All @@ -42,6 +43,11 @@ public function thirdMessageMethod(MyFailingCommand $command): void
throw new \InvalidArgumentException('Failing by intention');
}

public function fourthMessageMethod(MyOtherFailingCommand $command): void
{
throw new \InvalidArgumentException('Failing by intention');
}

public static function getHandledMessages(): iterable
{
yield MyCommand::class => [
Expand All @@ -55,5 +61,9 @@ public static function getHandledMessages(): iterable
yield MyFailingCommand::class => [
'method' => 'thirdMessageMethod',
];

yield MyOtherFailingCommand::class => [
'method' => 'fourthMessageMethod',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyFailingCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyOtherFailingCommand;

return [
'routing' => [
Expand All @@ -20,5 +21,8 @@
MyFailingCommand::class => [
'senders' => ['async'],
],
MyOtherFailingCommand::class => [
'senders' => ['async'],
],
],
];
24 changes: 22 additions & 2 deletions Tests/Functional/Repository/FailedMessageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Ssch\T3Messenger\Repository\FailedMessageRepository;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyFailingCommand;
use Ssch\T3Messenger\Tests\Functional\Fixtures\Extensions\t3_messenger_test\Classes\Command\MyOtherFailingCommand;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Messenger\EventListener\StopWorkerOnFailureLimitListener;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -42,13 +43,32 @@ public function test(): void
// Arrange
$this->messageBus->dispatch(new MyFailingCommand('Add to failed queue'));
$this->runWorker();
$this->messageBus->dispatch(new MyOtherFailingCommand('Add to failed queue'));
$this->runWorker();

// Act
$failedMessages = $this->subject->list();

// Assert
self::assertSame(MyFailingCommand::class, $failedMessages[0]->getMessage());
self::assertSame('Failing by intention', $failedMessages[0]->getErrorMessage());
$failedMessagesInAssertion = [];
foreach ($failedMessages as $failedMessage) {
$failedMessagesInAssertion[] = [
'class' => $failedMessage->getMessage(),
'error_message' => $failedMessage->getErrorMessage(),
];
}

self::assertCount(2, $failedMessages);
self::assertSame([
[
'class' => MyOtherFailingCommand::class,
'error_message' => 'Failing by intention',
],
[
'class' => MyFailingCommand::class,
'error_message' => 'Failing by intention',
],
], $failedMessagesInAssertion);
}

private function runWorker(): void
Expand Down

0 comments on commit 4c536fd

Please sign in to comment.