Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FunctionalTestCase #4

Merged
merged 2 commits into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
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 src/ContaoDatabaseTrait.php
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 src/FunctionalTestCase.php
@@ -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);
}
}