Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Sort failed messages in reverse order #59

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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