diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/CalendarForShortMessageProvider.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/CalendarForShortMessageProvider.php index 4e3722ea8..2316a5408 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/CalendarForShortMessageProvider.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/CalendarForShortMessageProvider.php @@ -51,8 +51,13 @@ class CalendarForShortMessageProvider */ public function getCalendars(DateTimeImmutable $at): iterable { - ['startDate' => $startDate, 'endDate' => $endDate] = $this->rangeGenerator - ->generateRange($at); + $range = $this->rangeGenerator->generateRange($at); + + if (null === $range) { + return; + } + + ['startDate' => $startDate, 'endDate' => $endDate] = $range; $offset = 0; $batchSize = 10; diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultRangeGenerator.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultRangeGenerator.php index 5cd4a6168..7d6da3ef0 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultRangeGenerator.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/DefaultRangeGenerator.php @@ -31,14 +31,14 @@ use UnexpectedValueException; */ class DefaultRangeGenerator implements RangeGeneratorInterface { - public function generateRange(\DateTimeImmutable $date): array + public function generateRange(\DateTimeImmutable $date): ?array { $onMidnight = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $date->format('Y-m-d') . ' 00:00:00'); switch ($dow = (int) $onMidnight->format('w')) { case 6: // Saturday case 0: // Sunday - return ['startDate' => null, 'endDate' => null]; + return null; case 1: // Monday // send for Tuesday and Wednesday diff --git a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/RangeGeneratorInterface.php b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/RangeGeneratorInterface.php index 5ba5acb35..357e264d9 100644 --- a/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/RangeGeneratorInterface.php +++ b/src/Bundle/ChillCalendarBundle/Service/ShortMessageNotification/RangeGeneratorInterface.php @@ -23,7 +23,7 @@ use DateTimeImmutable; interface RangeGeneratorInterface { /** - * @return array + * @return ?array{startDate: DateTimeImmutable, endDate: DateTimeImmutable} when return is null, then no ShortMessage must be send */ - public function generateRange(DateTimeImmutable $date): array; + public function generateRange(DateTimeImmutable $date): ?array; } diff --git a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/CalendarForShortMessageProviderTest.php b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/CalendarForShortMessageProviderTest.php index 261687515..f3e35ef93 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/CalendarForShortMessageProviderTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Service/ShortMessageNotification/CalendarForShortMessageProviderTest.php @@ -22,6 +22,7 @@ use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Repository\CalendarRepository; use Chill\CalendarBundle\Service\ShortMessageNotification\CalendarForShortMessageProvider; use Chill\CalendarBundle\Service\ShortMessageNotification\DefaultRangeGenerator; +use Chill\CalendarBundle\Service\ShortMessageNotification\RangeGeneratorInterface; use DateTimeImmutable; use Doctrine\ORM\EntityManagerInterface; use PHPUnit\Framework\TestCase; @@ -37,6 +38,32 @@ final class CalendarForShortMessageProviderTest extends TestCase { use ProphecyTrait; + public function testGenerateRangeIsNull() + { + $calendarRepository = $this->prophesize(CalendarRepository::class); + $calendarRepository->findByNotificationAvailable( + Argument::type(DateTimeImmutable::class), + Argument::type(DateTimeImmutable::class), + Argument::type('int'), + Argument::exact(0) + )->shouldBeCalledTimes(0); + $rangeGenerator = $this->prophesize(RangeGeneratorInterface::class); + $rangeGenerator->generateRange(Argument::type(DateTimeImmutable::class))->willReturn(null); + + $em = $this->prophesize(EntityManagerInterface::class); + $em->clear()->shouldNotBeCalled(); + + $provider = new CalendarForShortMessageProvider( + $calendarRepository->reveal(), + $em->reveal(), + $rangeGenerator->reveal() + ); + + $calendars = iterator_to_array($provider->getCalendars(new DateTimeImmutable('now'))); + + $this->assertEquals(0, count($calendars)); + } + public function testGetCalendars() { $calendarRepository = $this->prophesize(CalendarRepository::class);