buckets/tests/Entity/ScenarioOwnerTest.php

49 lines
1.4 KiB
PHP

<?php
namespace App\Tests\Entity;
use App\Entity\Scenario;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
final class ScenarioOwnerTest extends KernelTestCase
{
private EntityManagerInterface $em;
protected function setUp(): void
{
self::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
}
public function testOwnerRoundTripsThroughPersistence(): void
{
$owner = new User();
$owner->setEmail('scenario-owner-test@example.com');
$owner->setPassword('irrelevant-hash');
$this->em->persist($owner);
$scenario = new Scenario();
$scenario->setName('Household Budget');
$scenario->setOwner($owner);
$this->em->persist($scenario);
$this->em->flush();
$scenarioId = $scenario->getId();
$ownerId = $owner->getId();
$this->em->clear();
$reloaded = $this->em->find(Scenario::class, $scenarioId);
self::assertNotNull($reloaded, 'Scenario must be retrievable from the database after an identity-map clear.');
self::assertInstanceOf(User::class, $reloaded->getOwner());
self::assertTrue(
$ownerId->equals($reloaded->getOwner()->getId()),
'The owner set before persistence must survive a flush + clear + reload round-trip.',
);
}
}