临时教条2用phpunit进行测试 [英] Temporary Doctrine2 fixtures for testing with phpunit

查看:130
本文介绍了临时教条2用phpunit进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Symfony2 + Doctrine2的应用程序,我想为(使用phpunit)创建一些测试。

I have an application built on Symfony2 + Doctrine2 which I want to create some tests for (using phpunit).

例如,如果我想测试一个独特的验证器对DB中的记录,我想创建一个记录,我可以使用,但测试后,我不再需要它了。那么有办法创建临时(或虚拟)灯具,还是手动创建和删除它们?

For example if I want to test a unique validator against a record in the DB, I want to create a record I can work with, but after the test I don't need it anymore. So is there a way to create temporary (or virtual) fixtures or do I have to manually create and delete them?

推荐答案

你可以使用 Doctrine DataFixture ,并将此代码放在单元测试类的setUp方法中:

You can use Doctrine DataFixture and put this code in your setUp method of a unit test class:

$loader = new Doctrine\Common\DataFixtures\Loader;
$loader->loadFromDirectory('/path/to/MyDataFixtures');
$purger = new Doctrine\Common\DataFixtures\Purger\ORMPurger($em);
$executor = new Doctrine\Common\DataFixtures\Executor\ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures());

您可以参考文档查看如何创建DataFixture类。

You can refer to the docs to see how create DataFixture classes.

这是一个很好的例子: Symfony 2 + Doctrine 2 + PHPUnit 3.5:关闭异常的序列化

Here is a good example of how to do it: Symfony 2 + Doctrine 2 + PHPUnit 3.5: Serialization of closure exception

PS:我假设你有一个工作的$ em (EntityManager)。

PS: I assume you have a working $em (EntityManager) in this example.

这篇关于临时教条2用phpunit进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆