Skip to content

Commit 1f28584

Browse files
authored
Merge pull request #89 from synolia/feature/plan-command-once
plan command once
2 parents 35313cb + 559eebf commit 1f28584

7 files changed

+134
-9
lines changed

src/Planner/ScheduledCommandPlanner.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,31 @@
99
use Sylius\Component\Resource\Factory\FactoryInterface;
1010
use Synolia\SyliusSchedulerCommandPlugin\Entity\CommandInterface;
1111
use Synolia\SyliusSchedulerCommandPlugin\Entity\ScheduledCommandInterface;
12+
use Synolia\SyliusSchedulerCommandPlugin\Enum\ScheduledCommandStateEnum;
13+
use Synolia\SyliusSchedulerCommandPlugin\Repository\ScheduledCommandRepository;
1214

1315
class ScheduledCommandPlanner implements ScheduledCommandPlannerInterface
1416
{
1517
public function __construct(
1618
private FactoryInterface $scheduledCommandFactory,
1719
private EntityManagerInterface $entityManager,
20+
private ScheduledCommandRepository $scheduledCommandRepository,
1821
private LoggerInterface $logger,
1922
) {
2023
}
2124

2225
public function plan(CommandInterface $command): ScheduledCommandInterface
2326
{
27+
/** @var ScheduledCommandInterface[] $scheduledCommands */
28+
$scheduledCommands = $this->scheduledCommandRepository->findBy([
29+
'command' => $command->getCommand(),
30+
'state' => ScheduledCommandStateEnum::WAITING,
31+
]);
32+
33+
if (0 !== count($scheduledCommands)) {
34+
return array_shift($scheduledCommands);
35+
}
36+
2437
/** @var ScheduledCommandInterface $scheduledCommand */
2538
$scheduledCommand = $this->scheduledCommandFactory->createNew();
2639

tests/PHPUnit/AbstractIsDueTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ abstract class AbstractIsDueTest extends KernelTestCase
1717
/** @var EntityManagerInterface */
1818
protected $entityManager;
1919

20-
public function setUp(): void
20+
public static function setUpBeforeClass(): void
2121
{
22-
parent::setUp();
2322
$kernel = self::bootKernel();
2423
self::initDatabase($kernel);
24+
}
25+
26+
public function setUp(): void
27+
{
28+
parent::setUp();
2529

2630
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
2731
$this->entityManager->beginTransaction();

tests/PHPUnit/Command/PurgeScheduledCommandCommandTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ final class PurgeScheduledCommandCommandTest extends KernelTestCase
2525

2626
private ReflectionClass $reflectionClass;
2727

28-
public function setUp(): void
28+
public static function setUpBeforeClass(): void
2929
{
30-
$kernel = static::bootKernel();
30+
$kernel = self::bootKernel();
3131
self::initDatabase($kernel);
32+
}
33+
34+
public function setUp(): void
35+
{
36+
parent::setUp();
3237

3338
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
3439
$this->entityManager->beginTransaction();

tests/PHPUnit/Command/SynoliaSchedulerRunCommandTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ final class SynoliaSchedulerRunCommandTest extends KernelTestCase
2323

2424
private ?EntityManagerInterface $entityManager = null;
2525

26-
public function setUp(): void
26+
public static function setUpBeforeClass(): void
2727
{
28-
$kernel = static::bootKernel();
28+
$kernel = self::bootKernel();
2929
self::initDatabase($kernel);
30+
}
31+
32+
public function setUp(): void
33+
{
34+
parent::setUp();
3035

3136
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
3237
$this->entityManager->beginTransaction();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Synolia\SyliusSchedulerCommandPlugin\PHPUnit\Planner;
6+
7+
use function Amp\Promise\first;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Sylius\Component\Resource\Factory\Factory;
10+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11+
use Synolia\SyliusSchedulerCommandPlugin\Entity\Command;
12+
use Synolia\SyliusSchedulerCommandPlugin\Entity\ScheduledCommandInterface;
13+
use Synolia\SyliusSchedulerCommandPlugin\Enum\ScheduledCommandStateEnum;
14+
use Synolia\SyliusSchedulerCommandPlugin\Planner\ScheduledCommandPlannerInterface;
15+
use Tests\Synolia\SyliusSchedulerCommandPlugin\PHPUnit\WithDatabaseTrait;
16+
17+
class ScheduledCommandPlannerTest extends KernelTestCase
18+
{
19+
use WithDatabaseTrait;
20+
21+
private EntityManagerInterface $entityManager;
22+
23+
public function setUp(): void
24+
{
25+
parent::setUp();
26+
$kernel = self::bootKernel();
27+
self::initDatabase($kernel);
28+
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
29+
}
30+
31+
public function testPlanACommand(): void
32+
{
33+
$command = $this->getCommand();
34+
35+
/** @var ScheduledCommandPlannerInterface $planner */
36+
$planner = static::getContainer()->get(ScheduledCommandPlannerInterface::class);
37+
$scheduledCommand = $planner->plan($command);
38+
39+
$count = $this->entityManager->getRepository(ScheduledCommandInterface::class)->count([]);
40+
$this->assertEquals(1, $count);
41+
42+
$scheduledCommandsOnDatabase = $this->entityManager->getRepository(ScheduledCommandInterface::class)->findBy([
43+
'command' => 'about',
44+
'state' => ScheduledCommandStateEnum::WAITING,
45+
]);
46+
$this->assertCount(1, $scheduledCommandsOnDatabase);
47+
/** @var ScheduledCommandInterface $scheduledCommandOnDatabase */
48+
$scheduledCommandOnDatabase = $scheduledCommandsOnDatabase[0];
49+
$this->assertSame($scheduledCommand->getCommand(), $scheduledCommandOnDatabase->getCommand());
50+
}
51+
52+
public function testPlanACommandTwice(): void
53+
{
54+
$command = $this->getCommand();
55+
56+
/** @var ScheduledCommandPlannerInterface $planner */
57+
$planner = static::getContainer()->get(ScheduledCommandPlannerInterface::class);
58+
// first plan
59+
$planner->plan($command);
60+
// seconde plan
61+
$planner->plan($command);
62+
63+
$scheduledCommandsOnDatabase = $this->entityManager->getRepository(ScheduledCommandInterface::class)->findBy([
64+
'command' => 'about',
65+
'state' => ScheduledCommandStateEnum::WAITING,
66+
]);
67+
68+
// Command must by scheduled only once time
69+
$this->assertCount(1, $scheduledCommandsOnDatabase);
70+
}
71+
72+
private function getCommand(): Command
73+
{
74+
/** @var Command $command */
75+
$command = (new Factory(Command::class))->createNew();
76+
$command
77+
->setName('Plan Command')
78+
->setCommand('about')
79+
;
80+
81+
$this->entityManager->persist($command);
82+
$this->entityManager->flush();
83+
84+
return $command;
85+
}
86+
}

tests/PHPUnit/Service/ExecuteScheduleCommandTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ class ExecuteScheduleCommandTest extends WebTestCase
2020

2121
private ?EntityManagerInterface $entityManager = null;
2222

23-
public function setUp(): void
23+
public static function setUpBeforeClass(): void
2424
{
25-
parent::setUp();
2625
$kernel = self::bootKernel();
2726
self::initDatabase($kernel);
27+
}
28+
29+
public function setUp(): void
30+
{
31+
parent::setUp();
2832

2933
$this->scheduleCommandRunner = static::getContainer()->get(ScheduleCommandRunnerInterface::class);
3034
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);

tests/PHPUnit/WithDatabaseTrait.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Tests\Synolia\SyliusSchedulerCommandPlugin\PHPUnit;
66

77
use Doctrine\ORM\Tools\SchemaTool;
8+
use Sylius\Bundle\FixturesBundle\Loader\SuiteLoaderInterface;
9+
use Sylius\Bundle\FixturesBundle\Suite\SuiteRegistryInterface;
810
use Symfony\Component\HttpKernel\KernelInterface;
911

1012
trait WithDatabaseTrait
@@ -24,6 +26,12 @@ public static function initDatabase(KernelInterface $kernel)
2426
$schemaTool = new SchemaTool($entityManager);
2527
$schemaTool->updateSchema($metadatas);
2628

27-
// If you are using the Doctrine Fixtures Bundle you could load these here
29+
/** @var SuiteRegistryInterface $suiteRegistry */
30+
$suiteRegistry = $kernel->getContainer()->get(SuiteRegistryInterface::class);
31+
$suite = $suiteRegistry->getSuite('default');
32+
33+
/** @var SuiteLoaderInterface $suiteLoader */
34+
$suiteLoader = $kernel->getContainer()->get(SuiteLoaderInterface::class);
35+
$suiteLoader->load($suite);
2836
}
2937
}

0 commit comments

Comments
 (0)