edelagnier
edelagnier

Reputation: 1545

Init schema with Doctrine for Sqlite in memory

I would like to use a in memory sqlite base for my unit test.

The documentation of dbunit take sqlite as an example (https://phpunit.de/manual/current/en/database.html):

public function getConnection() {
    $pdo = new PDO('sqlite::memory:');
    return $this->createDefaultDBConnection($pdo, ':memory:');
}

but I can't find a way to initialize the schema of the database using doctrine.

Upvotes: 1

Views: 1079

Answers (1)

edelagnier
edelagnier

Reputation: 1545

I finally found a way to do it (using for inspiration : http://www.jeremygiberson.com : Using DBunit with Doctrine ORM)

public function getConnection() {
    // create entity manager following the doctrine way 
    $this->entityManager = require(__DIR__ . '/config/bootstrap.php');
    // init database schema
    $schemaTool = new SchemaTool($this->entityManager);
    $schemaTool->createSchema($this->entityManager->getMetadataFactory()->getAllMetadata());
    // get pdo
    $pdo = $this->entityManager->getConnection()->getWrappedConnection();
    // create connection
    return $this->createDefaultDBConnection($pdo, ':memory:');
}

Upvotes: 2

Related Questions