1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
<?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\Trigger;
use PHPUnit\Framework\TestCase;
use Random\Randomizer;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
class CronExpressionTriggerTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\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)) {
return [
['# * * * *', '30 * * * *'],
['# # * * *', '30 0 * * *'],
['# # # * *', '30 0 25 * *'],
['# # # # *', '30 0 25 10 *'],
['# # # # #', '30 0 25 10 5'],
['# # 1,15 1-11 *', '30 0 1,15 1-11 *'],
['# # 1,15 * *', '30 0 1,15 * *'],
['#hourly', '30 * * * *'],
['#daily', '30 0 * * *'],
['#weekly', '30 0 * * 3'],
['#weekly@midnight', '30 0 * * 3'],
['#monthly', '30 0 25 * *'],
['#monthly@midnight', '30 0 25 * *'],
['#yearly', '30 0 25 10 *'],
['#yearly@midnight', '30 0 25 10 *'],
['#annually', '30 0 25 10 *'],
['#annually@midnight', '30 0 25 10 *'],
['#midnight', '30 0 * * *'],
['#(1-15) * * * *', '1 * * * *'],
['#(1-15) * * * #(3-5)', '1 * * * 3'],
['#(1-15) * # * #(3-5)', '1 * 17 * 5'],
];
}
return [
['# * * * *', '36 * * * *'],
['# # * * *', '36 0 * * *'],
['# # # * *', '36 0 14 * *'],
['# # # # *', '36 0 14 3 *'],
['# # # # #', '36 0 14 3 5'],
['# # 1,15 1-11 *', '36 0 1,15 1-11 *'],
['# # 1,15 * *', '36 0 1,15 * *'],
['#hourly', '36 * * * *'],
['#daily', '36 0 * * *'],
['#weekly', '36 0 * * 6'],
['#weekly@midnight', '36 0 * * 6'],
['#monthly', '36 0 14 * *'],
['#monthly@midnight', '36 0 14 * *'],
['#yearly', '36 0 14 3 *'],
['#yearly@midnight', '36 0 14 3 *'],
['#annually', '36 0 14 3 *'],
['#annually@midnight', '36 0 14 3 *'],
['#midnight', '36 0 * * *'],
['#(1-15) * * * *', '7 * * * *'],
['#(1-15) * * * #(3-5)', '7 * * * 3'],
['#(1-15) * # * #(3-5)', '7 * 1 * 5'],
];
}
public function testHashFieldsAreRandomizedIndependently()
{
$parts = explode(' ', (string) CronExpressionTrigger::fromSpec('#(1-6) #(1-6) #(1-6) #(1-6) #(1-6)', 'some context'));
$this->assertNotCount(1, array_unique($parts));
}
public function testFromHashWithStandardExpression()
{
$this->assertSame('56 20 1 9 0', (string) CronExpressionTrigger::fromSpec('56 20 1 9 0', 'some context'));
$this->assertSame('0 0 * * *', (string) CronExpressionTrigger::fromSpec('@daily'));
}
public function testDefaultTimezone()
{
$now = new \DateTimeImmutable('Europe/Paris');
$trigger = CronExpressionTrigger::fromSpec('0 12 * * *');
$this->assertSame('Europe/Paris', $trigger->getNextRunDate($now)->getTimezone()->getName());
}
public function testTimezoneIsUsed()
{
$now = new \DateTimeImmutable('Europe/Paris');
$timezone = new \DateTimeZone('UTC');
$trigger = CronExpressionTrigger::fromSpec('0 12 * * *', null, $timezone);
$this->assertSame('UTC', $trigger->getNextRunDate($now)->getTimezone()->getName());
}
}
|