Skip to content

Commit

Permalink
small refactoring and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseytupichenkov committed Mar 4, 2018
1 parent 87d9f1a commit b919a90
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
9 changes: 1 addition & 8 deletions src/Services/DatabaseBackup/AbstractDatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Liip\FunctionalTestBundle\Services\DatabaseBackup;

use Doctrine\DBAL\Connection;
use Liip\FunctionalTestBundle\Services\FixturesLoaderFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -24,11 +23,6 @@ abstract class AbstractDatabaseBackup implements DatabaseBackupInterface

protected $fixturesLoaderFactory;

/**
* @var Connection
*/
protected $connection;

/**
* @var array
*/
Expand All @@ -47,9 +41,8 @@ public function __construct(ContainerInterface $container, FixturesLoaderFactory
$this->fixturesLoaderFactory = $fixturesLoaderFactory;
}

public function init(Connection $connection, array $metadatas, array $classNames): void
public function init(array $metadatas, array $classNames): void
{
$this->connection = $connection;
$this->metadatas = $metadatas;
$this->classNames = $classNames;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Services/DatabaseBackup/DatabaseBackupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
namespace Liip\FunctionalTestBundle\Services\DatabaseBackup;

use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\DBAL\Connection;

/**
* @author Aleksey Tupichenkov <[email protected]>
*/
interface DatabaseBackupInterface
{
public function init(Connection $connection, array $metadatas, array $classNames): void;
public function init(array $metadatas, array $classNames): void;

public function getBackupFilePath(): string;

Expand Down
18 changes: 14 additions & 4 deletions src/Services/DatabaseBackup/SqliteDatabaseBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Liip\FunctionalTestBundle\Services\DatabaseBackup;

use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;

/**
* @author Aleksey Tupichenkov <[email protected]>
Expand All @@ -23,9 +25,9 @@ public function getBackupFilePath(): string
return $this->container->getParameter('kernel.cache_dir').'/test_sqlite_'.md5(serialize($this->metadatas).serialize($this->classNames)).'.db';
}

private function getDatabaseName(): string
private function getDatabaseName(Connection $connection): string
{
$params = $this->connection->getParams();
$params = $connection->getParams();
if (isset($params['master'])) {
$params = $params['master'];
}
Expand All @@ -48,13 +50,21 @@ public function isBackupActual(): bool

public function backup(AbstractExecutor $executor): void
{
/** @var EntityManager $em */
$em = $executor->getReferenceRepository()->getManager();
$connection = $em->getConnection();

$executor->getReferenceRepository()->save($this->getBackupFilePath());
copy($this->getDatabaseName(), $this->getBackupFilePath());
copy($this->getDatabaseName($connection), $this->getBackupFilePath());
}

public function restore(AbstractExecutor $executor): void
{
copy($this->getBackupFilePath(), $this->getDatabaseName());
/** @var EntityManager $em */
$em = $executor->getReferenceRepository()->getManager();
$connection = $em->getConnection();

copy($this->getBackupFilePath(), $this->getDatabaseName($connection));
$executor->getReferenceRepository()->load($this->getBackupFilePath());
}
}
10 changes: 6 additions & 4 deletions src/Services/DatabaseTools/AbstractDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ protected function locateResources(array $paths): array

protected function getMetadatas(): array
{
if (!isset(self::$cachedMetadatas[$this->omName])) {
self::$cachedMetadatas[$this->omName] = $this->om->getMetadataFactory()->getAllMetadata();
usort(self::$cachedMetadatas[$this->omName], function ($a, $b) {
$key = $this->getDriverName().$this->getType().$this->omName;

if (!isset(self::$cachedMetadatas[$key])) {
self::$cachedMetadatas[$key] = $this->om->getMetadataFactory()->getAllMetadata();
usort(self::$cachedMetadatas[$key], function ($a, $b) {
return strcmp($a->name, $b->name);
});
}

return self::$cachedMetadatas[$this->omName];
return self::$cachedMetadatas[$key];
}

public function setExcludedDoctrineTables(array $excludedDoctrineTables): void
Expand Down
16 changes: 15 additions & 1 deletion src/Services/DatabaseTools/MongoDBDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class MongoDBDatabaseTool extends AbstractDatabaseTool
{
protected static $databaseCreated = false;

public function getType(): string
{
return 'MongoDB';
Expand All @@ -36,6 +38,16 @@ protected function getPurger(): MongoDBPurger
return new MongoDBPurger();
}

protected function createDatabaseOnce(): void
{
if (!self::$databaseCreated) {
$sm = $this->om->getSchemaManager();
$sm->createDatabases();
$sm->updateIndexes();
self::$databaseCreated = true;
}
}

public function loadFixtures(array $classNames = [], bool $append = false): AbstractExecutor
{
$referenceRepository = new ProxyReferenceRepository($this->om);
Expand All @@ -45,9 +57,11 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$cacheDriver->deleteAll();
}

$this->createDatabaseOnce();

$backupService = $this->getBackupService();
if ($backupService) {
$backupService->init($this->connection, $this->getMetadatas(), $classNames);
$backupService->init($this->getMetadatas(), $classNames);

if ($backupService->isBackupActual()) {
if (null !== $this->connection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/DatabaseTools/ORMDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst

$backupService = $this->getBackupService();
if ($backupService) {
$backupService->init($this->connection, $this->getMetadatas(), $classNames);
$backupService->init($this->getMetadatas(), $classNames);

if ($backupService->isBackupActual()) {
if (null !== $this->connection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/DatabaseTools/ORMSqliteDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst

$backupService = $this->getBackupService();
if ($backupService) {
$backupService->init($this->connection, $this->getMetadatas(), $classNames);
$backupService->init($this->getMetadatas(), $classNames);

if ($backupService->isBackupActual()) {
if (null !== $this->connection) {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/DatabaseTools/PHPCRDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst

$backupService = $this->getBackupService();
if ($backupService) {
$backupService->init($this->connection, $this->getMetadatas(), $classNames);
$backupService->init($this->getMetadatas(), $classNames);

if ($backupService->isBackupActual()) {
if (null !== $this->connection) {
Expand Down

0 comments on commit b919a90

Please sign in to comment.