在内存中使用 Doctrine for Sqlite 初始化模式

Init schema with Doctrine for Sqlite in memory

我想在我的单元测试中使用内存中的 sqlite 库。

dbunit文档以sqlite为例(https://phpunit.de/manual/current/en/database.html):

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

但我找不到使用原则初始化数据库模式的方法。

我终于找到了一种方法(用于启发: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:');
}