saufen/tests/DbTestCase.php
2025-06-08 21:22:26 +02:00

36 lines
1.1 KiB
PHP

<?php declare(strict_types=1);
namespace Tests;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Tests\TestCase;
abstract class DbTestCase extends TestCase
{
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();
}
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();
}
}