Skip to content

Commit

Permalink
make $context for RecurringMessage::hashedCron() optional
Browse files Browse the repository at this point in the history
if `$message` is `\Stringable`.
  • Loading branch information
kbond committed Mar 29, 2023
1 parent 9a1cfc3 commit 9060123
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Scheduler/RecurringMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ public static function cron(string $expression, object $message): self
return new self(CronExpressionTrigger::fromSpec($expression), $message);
}

public static function hashedCron(string $expression, string $context, object $message): self
public static function hashedCron(string $expression, object $message, ?string $context = null): self
{
if (!$context && $message instanceof \Stringable) {
$context = (string) $message;
}

if (!$context) {
throw new InvalidArgumentException('A context must be provided when the message is not a stringable object.');
}

return new self(CronExpressionTrigger::fromHash($expression, $context), $message);
}

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Scheduler/Tests/RecurringMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\RecurringMessage;

class RecurringMessageTest extends TestCase
{
public function testCanCreateHashedCronMessage()
{
$object = new class() {
public function __toString(): string
{
return 'my task';
}
};

$this->assertSame('56 2 * * *', (string) RecurringMessage::hashedCron('#midnight', $object)->getTrigger());
$this->assertSame('3 0 * * *', (string) RecurringMessage::hashedCron('#midnight', $object, 'context')->getTrigger());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;

final class CronExpressionTriggerTest extends TestCase
class CronExpressionTriggerTest extends TestCase
{
/**
* @dataProvider hashedExpressionProvider
Expand All @@ -25,7 +25,7 @@ public function testHashedExpressionParsing(string $input, string $expected)
$triggerB = CronExpressionTrigger::fromHash($input, 'my task');
$triggerC = CronExpressionTrigger::fromHash($input, 'another task');

$this->assertSame('cron: '.$expected, (string) $triggerA);
$this->assertSame($expected, (string) $triggerA);
$this->assertSame((string) $triggerB, (string) $triggerA);
$this->assertNotSame((string) $triggerC, (string) $triggerA);
}
Expand Down Expand Up @@ -59,7 +59,7 @@ public static function hashedExpressionProvider(): array

public function testFromHashWithStandardExpression()
{
$this->assertSame('cron: 56 20 1 9 0', (string) CronExpressionTrigger::fromHash('56 20 1 9 0', 'some context'));
$this->assertSame('cron: 0 0 * * *', (string) CronExpressionTrigger::fromHash('@daily', 'some context'));
$this->assertSame('56 20 1 9 0', (string) CronExpressionTrigger::fromHash('56 20 1 9 0', 'some context'));
$this->assertSame('0 0 * * *', (string) CronExpressionTrigger::fromHash('@daily', 'some context'));
}
}

0 comments on commit 9060123

Please sign in to comment.