Skip to content

Commit ed105f1

Browse files
committed
minor #52313 [FrameworkBundle][Scheduler] Add test for autoconfigured schedule (valtzu)
This PR was merged into the 6.4 branch. Discussion ---------- [FrameworkBundle][Scheduler] Add test for autoconfigured schedule | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | License | MIT Add functional tests for autoconfigured schedules / tasks, as discussed in #51525 (comment). Commits ------- ea2a8cd Add test for autoconfigured schedule
2 parents 8a69f67 + ea2a8cd commit ed105f1

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Messenger/DummyTask.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#[AsCronTask(expression: '* * * * *', arguments: [1], schedule: 'dummy_task')]
99
#[AsCronTask(expression: '0 * * * *', timezone: 'Europe/Berlin', arguments: ['2'], schedule: 'dummy_task', method: 'method2')]
1010
#[AsPeriodicTask(frequency: 5, arguments: [3], schedule: 'dummy_task')]
11-
#[AsPeriodicTask(frequency: '1 day', from: '00:00:00', jitter: 60, arguments: ['4'], schedule: 'dummy_task', method: 'method4')]
11+
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', jitter: 60, arguments: ['4'], schedule: 'dummy_task', method: 'method4')]
1212
class DummyTask
1313
{
1414
public static array $calls = [];
1515

16-
#[AsPeriodicTask(frequency: '1 hour', from: '09:00:00', until: '17:00:00', arguments: ['b' => 6, 'a' => '5'], schedule: 'dummy_task')]
17-
#[AsCronTask(expression: '0 0 * * *', arguments: ['7', 8], schedule: 'dummy_task')]
16+
#[AsPeriodicTask(frequency: '1 hour', from: '2023-10-26 09:00:00Z', until: '2023-10-26 17:00:00Z', arguments: ['b' => 6, 'a' => '5'], schedule: 'dummy_task')]
17+
#[AsCronTask(expression: '0 10 * * *', arguments: ['7', 8], schedule: 'dummy_task')]
1818
public function attributesOnMethod(string $a, int $b): void
1919
{
2020
self::$calls[__FUNCTION__][] = [$a, $b];

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SchedulerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage;
1515
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummySchedule;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTask;
1617
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
1718
use Symfony\Component\Clock\MockClock;
1819
use Symfony\Component\HttpKernel\KernelInterface;
20+
use Symfony\Component\Messenger\MessageBusInterface;
21+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1922
use Symfony\Component\Scheduler\Messenger\SchedulerTransport;
2023
use Symfony\Component\Scheduler\RecurringMessage;
2124

@@ -54,6 +57,36 @@ public function testScheduler()
5457
$this->assertSame([$foo, $bar, $foo, $bar], $fetchMessages(600.0));
5558
}
5659

60+
public function testAutoconfiguredScheduler()
61+
{
62+
$container = self::getContainer();
63+
$container->set('clock', $clock = new MockClock('2023-10-26T08:59:59Z'));
64+
65+
$this->assertTrue($container->get('receivers')->has('scheduler_dummy_task'));
66+
$this->assertInstanceOf(SchedulerTransport::class, $cron = $container->get('receivers')->get('scheduler_dummy_task'));
67+
$bus = $container->get(MessageBusInterface::class);
68+
69+
$getCalls = static function (float $sleep) use ($clock, $cron, $bus) {
70+
DummyTask::$calls = [];
71+
$clock->sleep($sleep);
72+
foreach ($cron->get() as $message) {
73+
$bus->dispatch($message->with(new ReceivedStamp('scheduler_dummy_task')));
74+
}
75+
76+
return DummyTask::$calls;
77+
};
78+
79+
$this->assertSame([], $getCalls(0));
80+
$this->assertSame(['__invoke' => [[1]], 'method2' => [['2']], 'attributesOnMethod' => [['5', 6]]], $getCalls(1));
81+
$this->assertSame(['__invoke' => [[3]]], $getCalls(5));
82+
$this->assertSame(['__invoke' => [[3]]], $getCalls(5));
83+
$calls = $getCalls(3595);
84+
$this->assertCount(779, $calls['__invoke']);
85+
$this->assertSame([['2']], $calls['method2']);
86+
$this->assertSame([['4']], $calls['method4']);
87+
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
88+
}
89+
5790
protected static function createKernel(array $options = []): KernelInterface
5891
{
5992
return parent::createKernel(['test_case' => 'Scheduler'] + $options);

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"require-dev": {
3636
"doctrine/annotations": "^1.13.1|^2",
3737
"doctrine/persistence": "^1.3|^2|^3",
38+
"dragonmantank/cron-expression": "^3.1",
3839
"seld/jsonlint": "^1.10",
3940
"symfony/asset": "^5.4|^6.0|^7.0",
4041
"symfony/asset-mapper": "^6.4|^7.0",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy