Coder Social home page Coder Social logo

Comments (3)

ndench avatar ndench commented on May 27, 2024 1

This bundle now seems to support paratest out of the box? Except I'm still getting this issue with caching the sqlite database.
Following @mente's example resolves the issue. The below is the only change required to the SqliteDatabaseBackup class.

Would it be worth me submitting a PR for this?

    public function getBackupFilePath(): string
    {
+        $token = \getenv('TEST_TOKEN') ?: 'default';
+        return $this->container->getParameter('kernel.cache_dir').'/test_sqlite_'.$token.\md5(\serialize($this->metadatas).\serialize($this->classNames)).'.db';
-        return $this->container->getParameter('kernel.cache_dir').'/test_sqlite_'.md5(serialize($this->metadatas).serialize($this->classNames)).'.db';
    }

from liiptestfixturesbundle.

alexislefebvre avatar alexislefebvre commented on May 27, 2024

Thanks for the detailed issue.

The support of paratest was removed to ease the maintenance of this bundle in #56, but we can restore it.

So we have to be able to:

  1. define a custom name for the database
  2. define a custom path for the backup

Could you please show the code that work for you today?

from liiptestfixturesbundle.

mente avatar mente commented on May 27, 2024

Thanks for looking into this.

We use the ConnectionFactory from the old times of the bundle (even has old reference to LiipFunctionalTestBundle):

use Doctrine\Bundle\DoctrineBundle\ConnectionFactory as BaseConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;

/**
 * Creates a connection taking the db name from the env with
 * a unique number defined by current process ID.
 *
 * @link https://github.com/liip/LiipFunctionalTestBundle/blob/master/Factory/ConnectionFactory.php idea borrowed here
 */
class DBALConnectionFactory extends BaseConnectionFactory
{
    /**
     * Create a connection by name.
     *
     * @param array         $params
     * @param Configuration $config
     * @param EventManager  $eventManager
     * @param array         $mappingTypes
     *
     * @return \Doctrine\DBAL\Connection
     */
    public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
    {
        $parallelPrefix = getenv('TEST_TOKEN');
        if ($params['driver'] === 'pdo_sqlite') {
            $params['path'] = str_replace('__DBNAME__', $parallelPrefix, $params['path']);
        } else {
            $params['dbname'] = str_replace('__DBNAME__', $parallelPrefix, $params['dbname']);
        }

        return parent::createConnection($params, $config, $eventManager, $mappingTypes);
    }
}

And the one we use for sqlite fixtures replacement

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

/**
 * @author Aleksey Tupichenkov <[email protected]>
 */
final class SqliteDatabaseBackup extends AbstractDatabaseBackup implements DatabaseBackupInterface
{
    public function getBackupFilePath(): string
    {
        $token = getenv('TEST_TOKEN') ?: 'default';

        return $this->container->getParameter('kernel.cache_dir').'/test_sqlite_'. $token . md5(serialize($this->metadatas).serialize($this->classNames)).'.db';
    }

    private function getDatabaseName(Connection $connection): string
    {
        $params = $connection->getParams();
        if (isset($params['master'])) {
            $params = $params['master'];
        }

        $name = $params['path'] ?? ($params['dbname'] ?? false);
        if (!$name) {
            throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
        }

        return $name;
    }

    public function isBackupActual(): bool
    {
        $backupDBFileName = $this->getBackupFilePath();
        $backupReferenceFileName = $backupDBFileName.'.ser';

        return file_exists($backupDBFileName) && file_exists($backupReferenceFileName) && $this->isBackupUpToDate($backupDBFileName);
    }

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

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

    public function restore(AbstractExecutor $executor, array $excludedTables = []): void
    {
        /** @var EntityManager $em */
        $em = $executor->getReferenceRepository()->getManager();
        $connection = $em->getConnection();

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

Had to copy-paste class because it's final in the bundle. Need to note that we're using latest 1.x version due to older symfony dependencies.

from liiptestfixturesbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.