fromSpec example

public static function every(string|int|\DateInterval $frequency, object $message, string|\DateTimeImmutable $from = null, string|\DateTimeImmutable $until = new \DateTimeImmutable('3000-01-01')): self
    {
        return self::trigger(new PeriodicalTrigger($frequency$from$until)$message);
    }

    /** * @param MessageProviderInterface|object $message A message provider that yields messages or a static message that will be dispatched on every trigger */
    public static function cron(string $expression, object $message, \DateTimeZone|string $timezone = null): self
    {
        if (!str_contains($expression, '#')) {
            return self::trigger(CronExpressionTrigger::fromSpec($expression, null, $timezone)$message);
        }

        if (!$message instanceof \Stringable) {
            throw new InvalidArgumentException('A message must be stringable to use "hashed" cron expressions.');
        }

        return self::trigger(CronExpressionTrigger::fromSpec($expression(string) $message$timezone)$message);
    }

    /** * @param MessageProviderInterface|object $message A message provider that yields messages or a static message that will be dispatched on every trigger */
use PHPUnit\Framework\TestCase;
use Random\Randomizer;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;

class CronExpressionTriggerTest extends TestCase
{
    /** * @dataProvider hashedExpressionProvider */
    public function testHashedExpressionParsing(string $input, string $expected)
    {
        $triggerA = CronExpressionTrigger::fromSpec($input, 'my task');
        $triggerB = CronExpressionTrigger::fromSpec($input, 'my task');
        $triggerC = CronExpressionTrigger::fromSpec($input, 'another task');

        $this->assertSame($expected(string) $triggerA);
        $this->assertSame((string) $triggerB(string) $triggerA);
        $this->assertNotSame((string) $triggerC(string) $triggerA);
    }

    public static function hashedExpressionProvider(): array
    {
        if (class_exists(Randomizer::class)) {
            
Home | Imprint | This part of the site doesn't use cookies.