Skip to content

Commit

Permalink
Added transport name for attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasJourdan committed Jun 1, 2024
1 parent 9c85a83 commit 3197586
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
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
4 changes: 4 additions & 0 deletions src/Resources/skeleton/scheduler/Schedule.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

<?= $use_statements; ?>

<?php if ($has_transport_name): ?>
#[AsSchedule('<?= $transport_name; ?>')]
<?php else: ?>
#[AsSchedule]
<?php endif ?>
final class <?= $class_name; ?> implements ScheduleProviderInterface
{
public function __construct(
Expand Down
16 changes: 16 additions & 0 deletions tests/Maker/MakeScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ 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(

Check failure on line 36 in tests/Maker/MakeScheduleTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that two strings are equal.

Check failure on line 36 in tests/Maker/MakeScheduleTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that two strings are equal.

Check failure on line 36 in tests/Maker/MakeScheduleTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that two strings are equal.
\dirname(__DIR__).'/fixtures/make-schedule/expected/DefaultScheduleWithTransportName.php',
$runner->getPath('src/Scheduler/MainSchedule.php')

Check failure on line 38 in tests/Maker/MakeScheduleTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that two strings are equal.

Check failure on line 38 in tests/Maker/MakeScheduleTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that two strings are equal.
);
}),
];

yield 'it_generates_a_schedule' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker([
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 3197586

Please sign in to comment.