forked from contao/test-case
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionalTestCase.php
61 lines (47 loc) · 1.65 KB
/
FunctionalTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Contao\TestCase;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Yaml\Yaml;
abstract class FunctionalTestCase extends WebTestCase
{
protected function loadFixture(string $yamlFile, bool $resetDatabase = true): void
{
self::bootKernel();
$doctrine = self::$container->get('doctrine');
/** @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;
}
$connection->insert($table, $row);
}
}
}
private function resetDatabase(): void
{
$doctrine = self::$container->get('doctrine');
/** @var Connection $connection */
$connection = $doctrine->getConnection();
$schemaManager = $connection->getSchemaManager();
/** @var Table $table */
foreach ($schemaManager->listTables() as $table) {
$connection->exec('DROP TABLE '.$table->getName());
}
/** @var EntityManagerInterface $manager */
$manager = $doctrine->getManager();
$metadata = $manager->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($manager);
$tool->createSchema($metadata);
}
}