diff --git a/src/Service/QueueService.php b/src/Service/QueueService.php index 94fd35a..b2cb831 100644 --- a/src/Service/QueueService.php +++ b/src/Service/QueueService.php @@ -125,7 +125,9 @@ public function getToRepublish(int $limit, ?int $offset = null): array */ public function createQueue(QueueInterface $queueable): QueueEntityInterface { - return $this->entityFactory->createQueue($queueable); + $queue = $this->entityFactory->createQueue($queueable); + + return $this->save($queue); } public function flush(QueueEntityInterface $entity = null): void diff --git a/tests/unit/Service/QueueServiceTest.php b/tests/unit/Service/QueueServiceTest.php index 531a0ee..3888ea7 100644 --- a/tests/unit/Service/QueueServiceTest.php +++ b/tests/unit/Service/QueueServiceTest.php @@ -4,17 +4,22 @@ namespace Lamoda\QueueBundle\Tests\Unit\Service; +use DateTime; use Lamoda\QueueBundle\Entity\QueueRepository; use Lamoda\QueueBundle\Event\QueueAttemptsReachedEvent; use Lamoda\QueueBundle\Exception\AttemptsReachedException; use Lamoda\QueueBundle\Exception\UnexpectedValueException; use Lamoda\QueueBundle\Factory\EntityFactory; +use Lamoda\QueueBundle\Publisher; +use Lamoda\QueueBundle\Service\DelayService; use Lamoda\QueueBundle\Service\QueueService; use Lamoda\QueueBundle\Tests\Unit\Job\StubJob; use Lamoda\QueueBundle\Tests\Unit\QueueCommonServicesTrait; use Lamoda\QueueBundle\Tests\Unit\QueueEntity; use Lamoda\QueueBundle\Tests\Unit\SymfonyMockTrait; +use OldSound\RabbitMqBundle\RabbitMq\Producer; use PHPUnit_Framework_TestCase; +use Psr\Log\NullLogger; use Symfony\Component\EventDispatcher\EventDispatcher; class QueueServiceTest extends PHPUnit_Framework_TestCase @@ -183,6 +188,39 @@ public function testCreateQueue(): void ); } + public function testSavingScheduledQueue(): void + { + $dateTime = new DateTime(); + $job = new StubJob(1); + $queue = new QueueEntity('queue', 'exchange', StubJob::class, ['id' => 1]); + $queue->setScheduled($dateTime); + + $queueRepository = $this->getQueueRepository(); + $queueRepository + ->expects($this->once()) + ->method('save') + ->with($queue) + ->willReturnArgument(0); + + $entityFactory = $this->getEntityFactory(); + $entityFactory + ->expects($this->once()) #at least once + ->method('createQueue') + ->willReturn($queue); + + $queueService = $this->createService($queueRepository, $entityFactory); + $publisher = new Publisher( + $this->createMock(Producer::class), + $queueService, + new NullLogger(), + $this->createMock(DelayService::class) + ); + + $createdQueue = $queueService->createQueue($job); + $publisher->prepareQueueForPublish($createdQueue); + $publisher->release(); + } + public function testIsTransactionActive(): void { $expected = true;