我一直在尝试在 Symfony2 项目中测试模型,但我不知道如何让实体管理器保存和检索记录。
任何人都可以为我指出正确的文档吗?
最佳答案
为了测试您的模型,您可以使用 setUp() 方法。 link to docs
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class MyModelTest extends WebTestCase
{
/**
* @var EntityManager
*/
private $_em;
protected function setUp()
{
$kernel = static::createKernel();
$kernel->boot();
$this->_em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$this->_em->beginTransaction();
}
/**
* Rollback changes.
*/
public function tearDown()
{
$this->_em->rollback();
}
public function testSomething()
{
$user = $this->_em->getRepository('MyAppMyBundle:MyModel')->find(1);
}
希望对你有帮助
关于php - 如何使用 PHPUnit 测试 Symfony2 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7313791/