Skip to content

Commit

Permalink
Merge pull request #1 from ben-challis/parallel-execution-support
Browse files Browse the repository at this point in the history
Simplified logic for baseline cache sqlite file, easier parallel execution support
  • Loading branch information
titomiguelcosta authored Nov 16, 2016
2 parents e5cc273 + dca96c1 commit 7b3d5c4
Showing 1 changed file with 70 additions and 28 deletions.
98 changes: 70 additions & 28 deletions src/Zorbus/Behat/Context/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\Tools\SchemaTool;
use Behat\Gherkin\Node\TableNode;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Exception;
use PDOException;
use RuntimeException;
use Exception;

class DoctrineContext implements Context, SnippetAcceptingContext
{
private static $database = null;
/**
* @var Connection
*/
private $connection;

/**
* @var EntityManager
*/
private $entityManager;

public function __construct(Connection $connection, EntityManager $entityManager)
/**
* @var string
*/
private $cacheDir;

/**
* @param Connection $connection
* @param EntityManager $entityManager
* @param string|null $cacheDir
*/
public function __construct(Connection $connection, EntityManager $entityManager, $cacheDir = null)
{
$this->connection = $connection;
$this->entityManager = $entityManager;
$this->cacheDir = $cacheDir;
}

/**
Expand Down Expand Up @@ -185,28 +202,28 @@ public function theFixtureIsAppended($file)

private function buildDatabase()
{
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$path = $this->connection->getDatabase();
$copy = $path.'.copy';
if ($this->cacheDir && $this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
$cachePath = $this->createSqliteCachePath($this->connection->getDatabase());

if (null !== self::$database) {
if (copy($copy, $path)) {
return;
}
}
}
if (file_exists($cachePath)) {
copy($cachePath, $this->connection->getDatabase());

$this->connection->getSchemaManager()->dropAndCreateDatabase($this->connection->getDatabase());
$this->selectDatabase();
$queries = $this->dumpStructure();
foreach ($queries as $query) {
$this->connection->query($query);
}
return;
} else {
$this->dropCreateAndSchemaUp();

$cacheDir = dirname($cachePath);

if (!is_dir($cacheDir)) {
if (!@mkdir($cacheDir, 0777, true) && !is_dir($cacheDir)) {
throw new RuntimeException('Failed to create SQLite cache directory: '.$cacheDir);
}
}

if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
if (copy($path, $copy)) {
self::$database = 'sqlite';
copy($this->connection->getDatabase(), $cachePath);
}
} else {
$this->dropCreateAndSchemaUp();
}
}

Expand All @@ -227,7 +244,7 @@ private function dropDatabase()
/**
* Runs the symfony equivalent command doctrine:schema:update --dump-sql
*
* @return string
* @return string[]
*/
private function dumpStructure()
{
Expand Down Expand Up @@ -274,4 +291,29 @@ private function selectDatabase()
} catch (Exception $e) {
}
}

/**
* @param string $databasePath
*
* @return string
*/
private function createSqliteCachePath($databasePath)
{
return $this->cacheDir.DIRECTORY_SEPARATOR.(new \SplFileInfo($databasePath))->getFilename().'.cache';
}

/**
* Drops, creates the database and builds the schema.
*/
private function dropCreateAndSchemaUp()
{
$this->connection->getSchemaManager()->dropAndCreateDatabase($this->connection->getDatabase());
$this->selectDatabase();

$queries = $this->dumpStructure();

foreach ($queries as $query) {
$this->connection->exec($query);
}
}
}

0 comments on commit 7b3d5c4

Please sign in to comment.