127 lines
3.7 KiB
PHP
127 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Entity;
|
|
|
|
use App\Entity\Scenario;
|
|
use App\Enum\DistributionMode;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Symfony\Component\Uid\UuidV7;
|
|
|
|
final class ScenarioPersistenceTest extends KernelTestCase
|
|
{
|
|
private EntityManagerInterface $em;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
|
}
|
|
|
|
public function testItPersistsAScenarioWithAUuidId(): void
|
|
{
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Household Budget');
|
|
|
|
$this->em->persist($scenario);
|
|
$this->em->flush();
|
|
|
|
$id = $scenario->getId();
|
|
self::assertInstanceOf(UuidV7::class, $id);
|
|
|
|
$this->em->clear();
|
|
|
|
$reloaded = $this->em->find(Scenario::class, $id);
|
|
|
|
self::assertNotNull($reloaded, 'Scenario must be retrievable from the database after an identity-map clear.');
|
|
self::assertInstanceOf(UuidV7::class, $reloaded->getId());
|
|
self::assertSame('Household Budget', $reloaded->getName());
|
|
}
|
|
|
|
public function testDistributionModeDefaultsToEvenWhenNotExplicitlySet(): void
|
|
{
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Default Mode Scenario');
|
|
|
|
$this->em->persist($scenario);
|
|
$this->em->flush();
|
|
|
|
$id = $scenario->getId();
|
|
|
|
$this->em->clear();
|
|
|
|
$reloaded = $this->em->find(Scenario::class, $id);
|
|
|
|
self::assertNotNull($reloaded);
|
|
self::assertSame(
|
|
DistributionMode::EVEN,
|
|
$reloaded->getDistributionMode(),
|
|
'distribution_mode must default to EVEN when not explicitly set.',
|
|
);
|
|
}
|
|
|
|
public function testItRoundTripsAnExplicitlySetPriorityDistributionMode(): void
|
|
{
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Priority Mode Scenario');
|
|
$scenario->setDistributionMode(DistributionMode::PRIORITY);
|
|
|
|
$this->em->persist($scenario);
|
|
$this->em->flush();
|
|
|
|
$id = $scenario->getId();
|
|
|
|
$this->em->clear();
|
|
|
|
$reloaded = $this->em->find(Scenario::class, $id);
|
|
|
|
self::assertNotNull($reloaded);
|
|
self::assertInstanceOf(
|
|
DistributionMode::class,
|
|
$reloaded->getDistributionMode(),
|
|
'distribution_mode must hydrate as a DistributionMode enum instance, not a raw string.',
|
|
);
|
|
self::assertSame(
|
|
DistributionMode::PRIORITY,
|
|
$reloaded->getDistributionMode(),
|
|
'An explicitly set PRIORITY distribution_mode must survive a flush + clear + reload round-trip.',
|
|
);
|
|
}
|
|
|
|
public function testDescriptionRoundTripsWhenSet(): void
|
|
{
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Described Scenario');
|
|
$scenario->setDescription('Covers rent, groceries, and the emergency buffer.');
|
|
|
|
$this->em->persist($scenario);
|
|
$this->em->flush();
|
|
|
|
$id = $scenario->getId();
|
|
|
|
$this->em->clear();
|
|
|
|
$reloaded = $this->em->find(Scenario::class, $id);
|
|
|
|
self::assertNotNull($reloaded);
|
|
self::assertSame('Covers rent, groceries, and the emergency buffer.', $reloaded->getDescription());
|
|
}
|
|
|
|
public function testDescriptionIsNullableAndRemainsNullWhenNotSet(): void
|
|
{
|
|
$scenario = new Scenario();
|
|
$scenario->setName('Undescribed Scenario');
|
|
|
|
$this->em->persist($scenario);
|
|
$this->em->flush();
|
|
|
|
$id = $scenario->getId();
|
|
|
|
$this->em->clear();
|
|
|
|
$reloaded = $this->em->find(Scenario::class, $id);
|
|
|
|
self::assertNotNull($reloaded);
|
|
self::assertNull($reloaded->getDescription());
|
|
}
|
|
}
|