Skip to content

Commit

Permalink
Fix FunctionalTestCase (see #5)
Browse files Browse the repository at this point in the history
Description
-----------

Unfortunately, #4 got merged without the changes we discussed, but here they are …

Commits
-------

03516da2 Fix FunctionalTestCase
6022d29e Fix method name
85e014d5 Boot kernel when resetting database
  • Loading branch information
aschempp committed Mar 6, 2020
1 parent 6fcbeb4 commit 5e171c4
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions test-case/src/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

abstract class FunctionalTestCase extends WebTestCase
{
protected function loadFixture(string $yamlFile, bool $resetDatabase = true): void
protected static function loadFixtures(array $yamlFiles, bool $truncateTables = true): void
{
self::bootKernel();

Expand All @@ -20,35 +20,34 @@ protected function loadFixture(string $yamlFile, bool $resetDatabase = true): vo
/** @var Connection $connection */
$connection = $doctrine->getConnection();

if ($resetDatabase) {
$this->resetDatabase();
}

$data = Yaml::parseFile($yamlFile);

foreach ($data as $table => $rows) {
foreach ($rows as $row) {
if ('sql' === $table) {
$connection->exec($row);
continue;
}
if ($truncateTables) {
$platform = $connection->getDatabasePlatform();

$connection->insert($table, $row);
/** @var Table $table */
foreach ($connection->getSchemaManager()->listTables() as $table) {
$connection->exec($platform->getTruncateTableSQL($table->getName()));
}
}

foreach ($yamlFiles as $file) {
self::importFixture($connection, $file);
}
}

private function resetDatabase(): void
protected static function resetDatabaseSchema(): void
{
self::bootKernel();

$doctrine = self::$container->get('doctrine');

/** @var Connection $connection */
$connection = $doctrine->getConnection();
$schemaManager = $connection->getSchemaManager();
$platform = $connection->getDatabasePlatform();

/** @var Table $table */
foreach ($schemaManager->listTables() as $table) {
$connection->exec('DROP TABLE '.$table->getName());
$connection->exec($platform->getDropTableSQL($table));
}

/** @var EntityManagerInterface $manager */
Expand All @@ -58,4 +57,20 @@ private function resetDatabase(): void
$tool = new SchemaTool($manager);
$tool->createSchema($metadata);
}

private static function importFixture(Connection $connection, string $file)
{
$data = Yaml::parseFile($file);

foreach ($data as $table => $rows) {
foreach ($rows as $row) {
if ('sql' === $table) {
$connection->exec($row);
continue;
}

$connection->insert($table, $row);
}
}
}
}

0 comments on commit 5e171c4

Please sign in to comment.