diff --git a/src/Services/DatabaseBackup/AbstractDatabaseBackup.php b/src/Services/DatabaseBackup/AbstractDatabaseBackup.php index fb8f768a..52623cca 100644 --- a/src/Services/DatabaseBackup/AbstractDatabaseBackup.php +++ b/src/Services/DatabaseBackup/AbstractDatabaseBackup.php @@ -11,7 +11,6 @@ namespace Liip\FunctionalTestBundle\Services\DatabaseBackup; -use Doctrine\DBAL\Connection; use Liip\FunctionalTestBundle\Services\FixturesLoaderFactory; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -24,11 +23,6 @@ abstract class AbstractDatabaseBackup implements DatabaseBackupInterface protected $fixturesLoaderFactory; - /** - * @var Connection - */ - protected $connection; - /** * @var array */ @@ -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; } diff --git a/src/Services/DatabaseBackup/DatabaseBackupInterface.php b/src/Services/DatabaseBackup/DatabaseBackupInterface.php index c281283d..a5434a44 100644 --- a/src/Services/DatabaseBackup/DatabaseBackupInterface.php +++ b/src/Services/DatabaseBackup/DatabaseBackupInterface.php @@ -12,14 +12,13 @@ namespace Liip\FunctionalTestBundle\Services\DatabaseBackup; use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; -use Doctrine\DBAL\Connection; /** * @author Aleksey Tupichenkov */ interface DatabaseBackupInterface { - public function init(Connection $connection, array $metadatas, array $classNames): void; + public function init(array $metadatas, array $classNames): void; public function getBackupFilePath(): string; diff --git a/src/Services/DatabaseBackup/SqliteDatabaseBackup.php b/src/Services/DatabaseBackup/SqliteDatabaseBackup.php index 255e8a07..14911143 100644 --- a/src/Services/DatabaseBackup/SqliteDatabaseBackup.php +++ b/src/Services/DatabaseBackup/SqliteDatabaseBackup.php @@ -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 @@ -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']; } @@ -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()); } } diff --git a/src/Services/DatabaseTools/AbstractDatabaseTool.php b/src/Services/DatabaseTools/AbstractDatabaseTool.php index 6b1bba8f..15cd9e59 100644 --- a/src/Services/DatabaseTools/AbstractDatabaseTool.php +++ b/src/Services/DatabaseTools/AbstractDatabaseTool.php @@ -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 diff --git a/src/Services/DatabaseTools/MongoDBDatabaseTool.php b/src/Services/DatabaseTools/MongoDBDatabaseTool.php index e6940648..e752b8a3 100644 --- a/src/Services/DatabaseTools/MongoDBDatabaseTool.php +++ b/src/Services/DatabaseTools/MongoDBDatabaseTool.php @@ -21,6 +21,8 @@ */ class MongoDBDatabaseTool extends AbstractDatabaseTool { + protected static $databaseCreated = false; + public function getType(): string { return 'MongoDB'; @@ -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); @@ -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) { diff --git a/src/Services/DatabaseTools/ORMDatabaseTool.php b/src/Services/DatabaseTools/ORMDatabaseTool.php index cdf1870f..07055562 100644 --- a/src/Services/DatabaseTools/ORMDatabaseTool.php +++ b/src/Services/DatabaseTools/ORMDatabaseTool.php @@ -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) { diff --git a/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php b/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php index d8b5f340..56e72bbb 100644 --- a/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php +++ b/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php @@ -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) { diff --git a/src/Services/DatabaseTools/PHPCRDatabaseTool.php b/src/Services/DatabaseTools/PHPCRDatabaseTool.php index 067ec153..89801b3b 100644 --- a/src/Services/DatabaseTools/PHPCRDatabaseTool.php +++ b/src/Services/DatabaseTools/PHPCRDatabaseTool.php @@ -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) {