Schedule example

use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;

#[AsSchedule('dummy')] class DummySchedule implements ScheduleProviderInterface
{
    public static array $recurringMessages;

    public function getSchedule(): Schedule
    {
        return (new Schedule())
            ->add(...self::$recurringMessages)
            ->stateful(new ArrayAdapter())
            ->lock(new Lock(new Key('dummy')new InMemoryStore()))
        ;
    }
}
// for referencing         $now = self::makeDateTime($startTime);

        $clock = $this->createMock(ClockInterface::class);
        $clock->method('now')->willReturnReference($now);

        foreach ($schedule as $i => $s) {
            if (\is_array($s)) {
                $schedule[$i] = $this->createMessage(...$s);
            }
        }
        $schedule = (new Schedule())->add(...$schedule);
        $schedule->stateful(new ArrayAdapter());

        $scheduler = new MessageGenerator($schedule, 'dummy', $clock);

        // Warmup. The first run always returns nothing.         $this->assertSame([]iterator_to_array($scheduler->getMessages(), false));

        foreach ($runs as $time => $expected) {
            $now = self::makeDateTime($time);
            $this->assertSame($expectediterator_to_array($scheduler->getMessages(), false));
        }
    }
$command = new DebugCommand($schedules);
        $tester = new CommandTester($command);

        $tester->execute([]['decorated' => false]);

        $filler = str_repeat(' ', 92);
        $this->assertSame("\nScheduler\n=========\n\n [ERROR] No schedules found.{$filler}\n\n", $tester->getDisplay(true));
    }

    public function testExecuteWithScheduleWithoutTriggerDoesNotDisplayMessage()
    {
        $schedule = new Schedule();
        $schedule->add(RecurringMessage::trigger(new CallbackTrigger(fn () => null, 'test')new \stdClass()));

        $schedules = $this->createMock(ServiceProviderInterface::class);
        $schedules
            ->expects($this->once())
            ->method('getProvidedServices')
            ->willReturn(['schedule_name' => $schedule])
        ;
        $schedules
            ->expects($this->once())
            ->method('get')
            


class SomeScheduleProvider implements ScheduleProviderInterface
{
    public function __construct(
        private readonly array $messages,
    ) {
    }

    public function getSchedule(): Schedule
    {
        return (new Schedule())->add(...$this->messages);
    }
}

class Container implements ContainerInterface
{
    use ServiceLocatorTrait;
}
namespace Symfony\Component\Scheduler\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Exception\LogicException;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;

class ScheduleTest extends TestCase
{
    public function testCannotAddDuplicateMessage()
    {
        $schedule = new Schedule();
        $schedule->add(RecurringMessage::cron('* * * * *', new \stdClass()));

        $this->expectException(LogicException::class);

        $schedule->add(RecurringMessage::cron('* * * * *', new \stdClass()));
    }
}
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\Scheduler;

class SchedulerTest extends TestCase
{
    public function testCanRunAndStop()
    {
        $handler = new Handler();
        $handler->clock = $clock = new MockClock();
        $schedule = (new Schedule())->add(RecurringMessage::every('1 second', new Message()));
        $scheduler = new Scheduler([Message::class => $handler][$schedule]$clock);
        $handler->scheduler = $scheduler;

        $scheduler->run(['sleep' => 100]);

        $this->assertSame(3, $handler->count);
    }
}

class Message
{
}
Home | Imprint | This part of the site doesn't use cookies.