saufen/tests/DbTestCase.php
2025-06-09 22:05:01 +02:00

50 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests;
use Exception;
use Override;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
abstract class DbTestCase extends TestCase
{
/**
* @template T of object
* @param class-string<T> $id
* @return object<T>
*/
protected function get(string $id): object
{
return $this->getContainer()->get($id);
}
protected function setUp(): void
{
$em = $this->getContainer()->get(EntityManagerInterface::class);
$metadata = $em->getMetadataFactory()->getAllMetadata();
if (empty($metadata)) {
throw new Exception('No metadata found. Did you forget to map entities?');
}
$schemaTool = new SchemaTool($em);
$schemaTool->dropDatabase(); // Clean slate, in case anything exists
$schemaTool->createSchema($metadata);
parent::setUp();
}
#[Override]
protected function tearDown(): void
{
$em = $this->getContainer()->get(EntityManagerInterface::class);
$metadata = $em->getMetadataFactory()->getAllMetadata();
if (empty($metadata)) {
throw new Exception('No metadata found. Did you forget to map entities?');
}
$schemaTool = new SchemaTool($em);
$schemaTool->dropDatabase(); // Clean slate, in case anything exists
parent::tearDown();
}
}