diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php index b900fa86d5774..0a34c86bc48f0 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/DoctrineIntegrationTest.php @@ -67,10 +67,29 @@ public function testSendWithDelay() // DBAL 2 compatibility $result = method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute(); - $available_at = new \DateTimeImmutable($result->fetchOne()); + $availableAt = new \DateTimeImmutable($result->fetchOne()); $now = new \DateTimeImmutable('now + 60 seconds'); - $this->assertGreaterThan($now, $available_at); + $this->assertGreaterThan($now, $availableAt); + } + + public function testSendWithNegativeDelay() + { + $this->connection->send('{"message": "Hi, I am not actually delayed"}', ['type' => DummyMessage::class], -600000); + + $qb = $this->driverConnection->createQueryBuilder() + ->select('m.available_at') + ->from('messenger_messages', 'm') + ->where('m.body = :body') + ->setParameter('body', '{"message": "Hi, I am not actually delayed"}'); + + // DBAL 2 compatibility + $result = method_exists($qb, 'executeQuery') ? $qb->executeQuery() : $qb->execute(); + + $availableAt = new \DateTimeImmutable($result->fetchOne()); + + $now = new \DateTimeImmutable('now - 60 seconds'); + $this->assertLessThan($now, $availableAt); } public function testItRetrieveTheFirstAvailableMessage() diff --git a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php index 2c787e9cca0c3..36875da9023b7 100644 --- a/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php @@ -127,7 +127,7 @@ public static function buildConfiguration(#[\SensitiveParameter] string $dsn, ar public function send(string $body, array $headers, int $delay = 0): string { $now = new \DateTimeImmutable('UTC'); - $availableAt = $now->modify(sprintf('+%d seconds', $delay / 1000)); + $availableAt = $now->modify(sprintf('%+d seconds', $delay / 1000)); $queryBuilder = $this->driverConnection->createQueryBuilder() ->insert($this->configuration['table_name']) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php b/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php index 0a4b218623a2e..db6b14c455c98 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php @@ -85,6 +85,15 @@ public function testQueueWithDelay() $this->assertSame([$envelope1], $this->transport->get()); } + public function testQueueWithNegativeDelay() + { + $envelope1 = new Envelope(new \stdClass()); + $envelope1 = $this->transport->send($envelope1); + $envelope2 = (new Envelope(new \stdClass()))->with(new DelayStamp(-10_000)); + $envelope2 = $this->transport->send($envelope2); + $this->assertSame([$envelope1, $envelope2], $this->transport->get()); + } + public function testQueueWithSerialization() { $envelope = new Envelope(new \stdClass()); diff --git a/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php b/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php index 4937c4b325786..b03ddd4448b93 100644 --- a/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php +++ b/src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php @@ -102,7 +102,7 @@ public function send(Envelope $envelope): Envelope /** @var DelayStamp|null $delayStamp */ if ($delayStamp = $envelope->last(DelayStamp::class)) { $now = $this->clock?->now() ?? new \DateTimeImmutable(); - $this->availableAt[$id] = $now->modify(sprintf('+%d seconds', $delayStamp->getDelay() / 1000)); + $this->availableAt[$id] = $now->modify(sprintf('%+d seconds', $delayStamp->getDelay() / 1000)); } return $envelope;