Symfony/Doctrine UnitTsts与SQLite内存DB

Hen*_*nes 8 phpunit webtest doctrine-orm dbal

我还在研究用于测试我的symfony2控制器的PHP单元测试.我的测试类是WebTestCase的派生,测试正在进行GET或POST请求,检查一切是否正常.我想测试所有底层,但我不想用测试弄乱我的数据库.我不想使用模拟ups,而是使用内存中的SQLite数据库,我可以在其中设置测试场景以检查所有修改.我发现了很多关于如何使用doctrine 1.x做到这一点的提示,但它们不再起作用了.所以我想要这样的东西:

class BlahblahTest extends WebTestCase {
    public function testXXXYYY() {
        // 1. Setup a new database with SQLite:memory:
        // 2. create the database and all tables according the entities in my project
        $this->createTestScenario(); // 3.
        $crawler = $this->client->request('GET', '/testpage');  // 4.
        // 5. Lots of checks against the database and / or the $crawler data
    }
}
Run Code Online (Sandbox Code Playgroud)

有机会获得这项工作吗?

在此先感谢Hennes

小智 10

我从来没有在内存中使用过sqlite数据库但是为了测试我确实使用了保存的sqlite数据库.

为此你应该添加

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     %kernel.cache_dir%/test.db
Run Code Online (Sandbox Code Playgroud)

到你的测试配置(对我来说是config_test.yml)

您应该能够根据文档将其更改为内存

http://docs.doctrine-project.org/projects/doctrine-dbal/en/ latest/reference/configuration.html#pdo-sqlite

memory(boolean):如果SQLite数据库应该在内存中(非持久性),则为True.与路径互斥.路径优先.

所以配置应该是

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                memory:   true
Run Code Online (Sandbox Code Playgroud)


Jef*_*ima 5

接受的答案并没有为我工作,因为我用的是URL指定连接参数,虽然我是在驱动器设置为pdo_sqlitememorytrue,学说仍然使用url从根配置参数,所以我不得不将其覆盖.

#api/config/packages/test/doctrine.yaml
doctrine:
  dbal:
    url: "sqlite:///:memory:"
Run Code Online (Sandbox Code Playgroud)