buckets/tests/Entity/ScenarioPersistenceTest.php

82 lines
2.3 KiB
PHP

<?php
namespace App\Tests\Entity;
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());
}
}