Skip to content

Commit

Permalink
Add FunctionalTestCase (see #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Mar 3, 2020
1 parent 4aa8735 commit 6fcbeb4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion test-case/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
"php": "^7.2"
},
"conflict": {
"contao/core-bundle": "<4.9",
"phpunit/phpunit": "<8.0"
},
"require-dev": {
"ext-pdo": "*",
"contao/core-bundle": "^4.5",
"contao/core-bundle": "^4.9",
"doctrine/dbal": "^2.10",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^2.12",
"php-http/guzzle6-adapter": "^1.1"
},
Expand Down
3 changes: 3 additions & 0 deletions test-case/src/ContaoDatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

trait ContaoDatabaseTrait
{
/**
* @var Connection
*/
private static $connection;

protected static function loadFileIntoDatabase(string $sqlFile): void
Expand Down
61 changes: 61 additions & 0 deletions test-case/src/FunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}

0 comments on commit 6fcbeb4

Please sign in to comment.