Skip to content

Commit

Permalink
minor #52333 [Scheduler] Use MockClock (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

[Scheduler] Use MockClock

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | n/a
| License       | MIT

Commits
-------

6085fe0 [Scheduler] Use MockClock
  • Loading branch information
fabpot committed Oct 27, 2023
2 parents b691eba + 6085fe0 commit 49abe89
Showing 1 changed file with 6 additions and 20 deletions.
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Scheduler\Generator\Checkpoint;
use Symfony\Component\Scheduler\Generator\MessageContext;
Expand All @@ -30,11 +29,7 @@ class MessageGeneratorTest extends TestCase
*/
public function testGetMessagesFromSchedule(string $startTime, array $runs, array $schedule)
{
// for referencing
$now = self::makeDateTime($startTime);

$clock = $this->createMock(ClockInterface::class);
$clock->method('now')->willReturnReference($now);
$clock = new MockClock(self::makeDateTime($startTime));

foreach ($schedule as $i => $s) {
if (\is_array($s)) {
Expand All @@ -50,7 +45,7 @@ public function testGetMessagesFromSchedule(string $startTime, array $runs, arra
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

foreach ($runs as $time => $expected) {
$now = self::makeDateTime($time);
$clock->modify($time);
$this->assertSame($expected, iterator_to_array($scheduler->getMessages(), false));
}
}
Expand All @@ -60,11 +55,7 @@ public function testGetMessagesFromSchedule(string $startTime, array $runs, arra
*/
public function testGetMessagesFromScheduleProvider(string $startTime, array $runs, array $schedule)
{
// for referencing
$now = self::makeDateTime($startTime);

$clock = $this->createMock(ClockInterface::class);
$clock->method('now')->willReturnReference($now);
$clock = new MockClock(self::makeDateTime($startTime));

foreach ($schedule as $i => $s) {
if (\is_array($s)) {
Expand Down Expand Up @@ -92,18 +83,14 @@ public function getSchedule(): Schedule
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

foreach ($runs as $time => $expected) {
$now = self::makeDateTime($time);
$clock->modify($time);
$this->assertSame($expected, iterator_to_array($scheduler->getMessages(), false));
}
}

public function testYieldedContext()
{
// for referencing
$now = self::makeDateTime('22:12:00');

$clock = $this->createMock(ClockInterface::class);
$clock->method('now')->willReturnReference($now);
$clock = new MockClock(self::makeDateTime('22:12:00'));

$message = $this->createMessage((object) ['id' => 'message'], '22:13:00', '22:14:00', '22:16:00');
$schedule = (new Schedule())->add($message);
Expand All @@ -114,8 +101,7 @@ public function testYieldedContext()
// Warmup. The first run is alw ays returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

$now = self::makeDateTime('22:14:10');

$clock->sleep(2 * 60 + 10);
$iterator = $scheduler->getMessages();

$this->assertInstanceOf(MessageContext::class, $context = $iterator->key());
Expand Down

0 comments on commit 49abe89

Please sign in to comment.