Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
fix(message-order): consume message by creation order (fifo) (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazsudo authored and core23 committed Feb 9, 2019
1 parent fbbea65 commit 24edcdb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Iterator/MessageManagerMessageIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function setCurrent(): void
$this->bufferize($this->types);
}

$this->current = array_pop($this->buffer);
$this->current = array_shift($this->buffer);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Entity/MessageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ public function testGetPagerWithInProgressMessages(): void
->getPager(['state' => MessageInterface::STATE_IN_PROGRESS], 1);
}

public function testFindByTypes(): void
{
$this
->getMessageManager(function ($qb): void {
$qb->expects($this->never())->method('andWhere');
$qb->expects($this->once())->method('where')->willReturnSelf();
$qb->expects($this->once())->method('setParameters')
->with(['state' => MessageInterface::STATE_OPEN]);
$qb->expects($this->once())->method('orderBy')->with(
$this->equalTo('m.createdAt')
)->willReturnSelf();
})
->findByTypes([], MessageInterface::STATE_OPEN, 10);
}

/**
* @return MessageManagerMock
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Iterator/MessageManagerMessageIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Sonata\NotificationBundle\Iterator\MessageManagerMessageIterator as MessageManagerMessageIteratorObject;
use Sonata\NotificationBundle\Model\Message;
use Sonata\NotificationBundle\Model\MessageManagerInterface;

/**
* @author Kevin Nedelec <kevin.nedelec@ekino.com>
Expand Down Expand Up @@ -77,4 +80,27 @@ public function testLongForeach(): void
}
}
}

public function testMessageConsumptionOrder(): void
{
$now = new \DateTime();
$olderDate = (clone $now)->modify('-1 hour');
$oldMessage = new Message();
$oldMessage->setCreatedAt($olderDate);
$oldMessage->setType('older.message');

$newMessage = new Message();
$newMessage->setCreatedAt($now);
$newMessage->setType('newer.message');

$messageManager = $this->createMock(MessageManagerInterface::class);
$messageManager->expects($this->once())->method('findByTypes')->willReturn([$oldMessage, $newMessage]);

$iterator = new MessageManagerMessageIteratorObject($messageManager, [], 500000, 2);

$iterator->next();
$this->assertSame($oldMessage, $iterator->current());
$iterator->next();
$this->assertSame($newMessage, $iterator->current());
}
}

0 comments on commit 24edcdb

Please sign in to comment.