buckets/tests/Entity/ScenarioPersistenceTest.php

128 lines
3.5 KiB
PHP

<?php
namespace App\Tests\Entity;
use App\Entity\Bucket;
use App\Entity\Scenario;
use App\Tests\Concerns\CreatesUsers;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Uid\UuidV7;
final class ScenarioPersistenceTest extends KernelTestCase
{
use CreatesUsers;
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');
$scenario->setOwner($this->persistOwner());
$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 testDescriptionRoundTripsWhenSet(): void
{
$scenario = new Scenario();
$scenario->setName('Described Scenario');
$scenario->setDescription('Covers rent, groceries, and the emergency buffer.');
$scenario->setOwner($this->persistOwner());
$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');
$scenario->setOwner($this->persistOwner());
$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());
}
public function testScenarioExposesItsBucketsInverseRelation(): void
{
$scenario = new Scenario();
$scenario->setName('Household Budget');
$scenario->setOwner($this->persistOwner());
$this->em->persist($scenario);
$this->em->flush();
$rent = new Bucket();
$rent->setScenario($scenario);
$rent->setName('Rent');
$rent->setPriority(1);
$groceries = new Bucket();
$groceries->setScenario($scenario);
$groceries->setName('Groceries');
$groceries->setPriority(2);
$this->em->persist($rent);
$this->em->persist($groceries);
$this->em->flush();
$id = $scenario->getId();
$this->em->clear();
$reloaded = $this->em->find(Scenario::class, $id);
self::assertNotNull($reloaded);
$names = array_map(
static fn (Bucket $bucket): string => $bucket->getName(),
$reloaded->getBuckets()->toArray(),
);
sort($names);
self::assertSame(
['Groceries', 'Rent'],
$names,
'A reloaded Scenario must expose its Buckets via an inverse relation.',
);
}
}