Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 192a725

Browse files
committed
Add a checks that an interval is valid
1 parent 82fb4a4 commit 192a725

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Schedule/PeriodicalMessageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests\Transport\Schedule;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1516
use Symfony\Component\Messenger\Transport\Schedule\PeriodicalMessage;
1617

1718
class PeriodicalMessageTest extends TestCase
@@ -102,6 +103,29 @@ public function testConstructors()
102103
$this->assertEquals($message, PeriodicalMessage::fromPeriod(new \stdClass(), new \DatePeriod($firstRun->sub($day), $day, 284108, \DatePeriod::EXCLUDE_START_DATE)));
103104
}
104105

106+
public function getInvalidIntervals(): iterable
107+
{
108+
yield ['wrong'];
109+
yield ['3600.5'];
110+
yield [0];
111+
yield [-3600];
112+
}
113+
114+
/**
115+
* @dataProvider getInvalidIntervals
116+
*/
117+
public function testInvalidInterval($interval)
118+
{
119+
$this->expectException(InvalidArgumentException::class);
120+
PeriodicalMessage::create(new \stdClass(), $interval, $now = new \DateTimeImmutable(), $now->modify('1 day'));
121+
}
122+
123+
public function testNegativeInterval()
124+
{
125+
$this->expectException(InvalidArgumentException::class);
126+
PeriodicalMessage::create(new \stdClass(), 'wrong', $now = new \DateTimeImmutable(), $now->modify('1 day'));
127+
}
128+
105129
public function testGetMessage()
106130
{
107131
$message = new \stdClass();

src/Symfony/Component/Messenger/Transport/Schedule/PeriodicalMessage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Messenger\Transport\Schedule;
1313

14+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
15+
1416
final class PeriodicalMessage implements ScheduledMessageInterface
1517
{
1618
public function __construct(
@@ -19,6 +21,9 @@ public function __construct(
1921
private readonly \DateTimeImmutable $firstRun,
2022
private readonly \DateTimeImmutable $priorTo,
2123
) {
24+
if (0 >= $this->intervalInSeconds) {
25+
throw new InvalidArgumentException('The interval must be greater then zero.');
26+
}
2227
}
2328

2429
public static function create(
@@ -36,9 +41,10 @@ public static function create(
3641
if (\is_string($interval)) {
3742
if ('P' === $interval[0]) {
3843
$interval = new \DateInterval($interval);
39-
} else {
40-
\assert((string) (int) $interval === $interval);
44+
} elseif (ctype_digit($interval)) {
4145
$interval = (int) $interval;
46+
} else {
47+
throw new InvalidArgumentException('The interval must be a positive integer.');
4248
}
4349
}
4450
if (!\is_int($interval)) {

0 commit comments

Comments
 (0)