8000 make `$context` for `RecurringMessage::hashedCron()` optional · symfony/symfony@051c256 · GitHub
[go: up one dir, main page]

Skip to content

Commit 051c256

Browse files
committed
make $context for RecurringMessage::hashedCron() optional
if `$message` is `\Stringable`.
1 parent 9a1cfc3 commit 051c256

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/Symfony/Component/Scheduler/RecurringMessage.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ public static function cron(string $expression, object $message): self
4646
return new self(CronExpressionTrigger::fromSpec($expression), $message);
4747
}
4848

49-
public static function hashedCron(string $expression, string $context, object $message): self
49+
public static function hashedCron(string $expression, object $message, ?string $context = null): self
5050
{
51+
if (!$context && $message instanceof \Stringable) {
52+
$context = (string) $message;
53+
}
54+
55+
if (!$context) {
56+
throw new InvalidArgumentException('A context must be provided when the message is not a stringable object.');
57+
}
58+
5159
return new self(CronExpressionTrigger::fromHash($expression, $context), $message);
5260
}
5361

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\RecurringMessage;
16+
17+
class RecurringMessageTest extends TestCase
18+
{
19+
public function testCanCreateHashedCronMessage()
20+
{
21+
$object = new class() {
22+
public function __toString(): string
23+
{
24+
return 'my task';
25+
}
26+
};
27+
28+
$this->assertSame('56 2 * * *', (string) RecurringMessage::hashedCron('#midnight', $object)->getTrigger());
29+
$this->assertSame('3 0 * * *', (string) RecurringMessage::hashedCron('#midnight', $object, 'context')->getTrigger());
30+
}
31+
32+
public function testHashedCronContextIsRequiredIfMessageIsNotStringable()
33+
{
34+
$this->expectException(\InvalidArgumentException::class);
35+
36+
RecurringMessage::hashedCron('#midnight', new \stdClass());
37+
}
38+
}

src/Symfony/Component/Scheduler/Tests/Trigger/CronExpressionTriggerTest.php

Lines changed: 4 additions & 4 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
1616

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

28-
$this->assertSame('cron: '.$expected, (string) $triggerA);
28+
$this->assertSame($expected, (string) $triggerA);
2929
$this->assertSame((string) $triggerB, (string) $triggerA);
3030
$this->assertNotSame((string) $triggerC, (string) $triggerA);
3131
}
@@ -59,7 +59,7 @@ public static function hashedExpressionProvider(): array
5959

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

0 commit comments

Comments
 (0)
0