我还在进行PHP单元测试,以测试我的symfony2控制器。我的测试类是WebTestCase的派生,测试正在进行GET或POST请求,检查一切是否正常。 我想测试所有底层,但我不想用测试搞乱我的数据库。我不想使用模拟,而是使用内存中的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
}
}
有机会获得这项工作吗?
提前致谢 亨尼斯
答案 0 :(得分:9)
我从未在内存中使用sqlite数据库但是为了测试我确实使用了保存的sqlite数据库。
为此你应该添加
# app/config/config_test.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
path: %kernel.cache_dir%/test.db
到你的测试配置(对我来说是config_test.yml)
您应该可以根据文档
将其更改为内存memory(boolean):如果SQLite数据库应该在内存中(非持久性),则为True。与路径互斥。路径优先。
所以配置应该是
# app/config/config_test.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlite
memory: true
答案 1 :(得分:1)
您可以轻松创建自己的实体管理器实例。检查一下helper class你可能在symfony 2中没有它,但你可以使用这个想法来构建你自己的基础testCaseClass并在需要时使用它。这甚至不需要启动内核。
答案 2 :(得分:1)
接受的答案对我不起作用,因为我使用URL来指定连接参数,并且还一直将驱动程序设置为pdo_sqlite
,memory
设置为true
,Doctrine仍在根配置中使用url
参数,因此我不得不覆盖它。
#api/config/packages/test/doctrine.yaml
doctrine:
dbal:
url: "sqlite:///:memory:"