Skip to content

Commit

Permalink
feature #1568 [make:schedule] Add transport name for attribute
Browse files Browse the repository at this point in the history
Co-authored-by: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com>
  • Loading branch information
NicolasJourdan and jrushlow authored Jun 5, 2024
1 parent 9c85a83 commit d4fa901
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Maker/MakeSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class MakeSchedule extends AbstractMaker
{
private string $scheduleName;
private ?string $message = null;
private ?string $transportName = null;

public function __construct(
private FileManager $fileManager,
Expand Down Expand Up @@ -82,6 +83,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
}
}

$this->transportName = $io->ask('What should we call the new transport? (To be used for the attribute #[AsSchedule(name)])');

$scheduleNameHint = 'MainSchedule';

// If the count is 1, no other messages were found - don't ask to create a message
Expand Down Expand Up @@ -126,6 +129,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'use_statements' => $useStatements,
'has_custom_message' => null !== $this->message,
'message_class_name' => $this->message,
'has_transport_name' => null !== $this->transportName,
'transport_name' => $this->transportName,
],
);

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/scheduler/Schedule.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<?= $use_statements; ?>

#[AsSchedule]
#[AsSchedule<?php if ($has_transport_name): ?>('<?= $transport_name; ?>')<?php endif ?>]
final class <?= $class_name; ?> implements ScheduleProviderInterface
{
public function __construct(
Expand Down
19 changes: 19 additions & 0 deletions tests/Maker/MakeScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@ protected function getMakerClass(): string

public function getTestDetails(): \Generator
{
yield 'it_generates_a_schedule_with_transport_name' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'dummy', // use transport name "dummy"
'', // use default schedule name "MainSchedule"
]);

$this->assertStringContainsString('Success', $output);

self::assertFileEquals(
\dirname(__DIR__).'/fixtures/make-schedule/expected/DefaultScheduleWithTransportName.php',
$runner->getPath('src/Scheduler/MainSchedule.php')
);
}),
];

yield 'it_generates_a_schedule' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // use default transport name
'', // use default schedule name "MainSchedule"
]);

Expand All @@ -48,6 +65,7 @@ public function getTestDetails(): \Generator
})
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // Use the default transport name
0, // Select "Empty Schedule"
'MySchedule', // Go with the default name "MainSchedule"
]);
Expand All @@ -70,6 +88,7 @@ public function getTestDetails(): \Generator
})
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
'', // Use the default transport name
1, // Select "MyMessage" from choice
'', // Go with the default name "MessageFixtureSchedule"
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Scheduler;

use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;

#[AsSchedule('dummy')]
final class MainSchedule implements ScheduleProviderInterface
{
public function __construct(
private CacheInterface $cache,
) {
}

public function getSchedule(): Schedule
{
return (new Schedule())
->add(
// @TODO - Create a Message to schedule
// RecurringMessage::every('1 hour', new App\Message\Message()),
)
->stateful($this->cache)
;
}
}

0 comments on commit d4fa901

Please sign in to comment.