在生产服务器上的每次代码更新之前,我通过在测试数据库中插入行来执行phpunit测试。 由于测试数据库不能反映生产数据库的内容,因此我想对生产数据库执行测试。 测试完成后,我想在测试过程中删除所有创建的行。什么是实现这一目标的最佳方式? 我无法想象一种完美的方法,无需更改生产数据。Symfony2测试数据库生产环境
1
A
回答
1
我使用Alexandre Salome在Isolation of tests in Symfony2中描述的方法来隔离我的测试和事务并在最后回滚。这种方法工作得很好,但显然你需要在生产数据库上使用它之前仔细测试它!
0
我建议你使用sqlite(默认)进行测试,因为它更快,你不必担心他们是否会在生产数据库上搞点东西。我所做的是每个
EntityTest.php extends TestsHelper.php extends PHPUnit_Framework_TestCase
和设置(),我创建的数据库和灯具。
我从互联网上的代码,它的工作原理。你可能会觉得它很有用。
// class TestsHelper
/**
* @var Symfony\Component\DependencyInjection\Container
*/
protected $container;
public function setUp()
{
// Boot the AppKernel in the test environment and with the debug.
$this->kernel = new \AppKernel('test', true);
$this->kernel->boot();
// Store the container and the entity manager in test case properties
$this->container = $this->kernel->getContainer();
$this->em = $this->container->get('doctrine')->getEntityManager();
// Build the schema for sqlite
$this->generateSchema();
$this->generateFixtures() ;
parent::setUp();
}
public function tearDown()
{
// Shutdown the kernel.
$this->kernel->shutdown();
parent::tearDown();
}
protected function generateSchema()
{
// Get the metadatas of the application to create the schema.
$metadatas = $this->getMetadatas();
if (! empty($metadatas)) {
// Create SchemaTool
/**
* @var \Doctrine\ORM\Tools\SchemaTool
*/
$tool = new SchemaTool($this->em);
// $tool->dropDatabase() ;
$tool->createSchema($metadatas);
} else {
throw new Doctrine\DBAL\Schema\SchemaException('No Metadata Classes to process.');
}
}
/**
* Overwrite this method to get specific metadatas.
*
* @return Array
*/
protected function getMetadatas()
{
return $this->em->getMetadataFactory()->getAllMetadata();
}
而且在generateFixtures(),您将创建他们像往常一样:
$entity = new MyEntity() ;
$this->em->persist($entity) ;
$this->em->flush() ;
+0
谢谢,但我想对生产数据库执行测试 –
相关问题
- 1. Tomcat测试和生产环境
- 2. 生产/测试环境容易问题
- 3. 设置测试和生产环境
- 4. 测试和生产环境之间的数据不同
- 5. HttpPostedFileBase null在生产环境中,但没有测试环境
- 6. 不同于测试和生产环境的开发环境?
- 7. 生产环境中的System.Runtime.Serialization.InvalidDataContractException,而不是测试环境
- 8. 生产环境使用临时数据库而不是生产数据库
- 9. Symfony2上“生产”环境的错误
- 10. Symfony2:跳转到生产环境失败
- 11. 为生产和测试环境更改数据库的设计模式
- 12. 在非生产环境中测试生产配置文件
- 13. 区分Websphere Commerce中的生产环境,临时环境和测试环境
- 14. 在测试环境中的symfony2 behat:数据库表格未创建
- 15. Symfony2 Doctrine数据库投入生产
- 16. 什么是“测试环境”中的测试过程,已经部署在生产环境中而没有测试
- 17. 如何使用测试数据库代理生产数据库?
- 18. 从测试数据库更新生产数据库的脚本
- 19. 在测试数据库和生产数据库之间切换?
- 20. Dockerfile生产/建设/调试/测试环境
- 21. 从本地环境到生产环境获取修改的数据库
- 22. 将wordpress从测试环境移到生产环境并保留文章
- 23. WordPress的:如何复制您的生产环境创建一个测试环境?
- 24. Tomcat web应用程序生产环境和测试环境的属性
- 25. Rails生产环境
- 26. Sidekiq仅在生产环境中重试
- 27. Rails在生产环境中调试
- 28. 如何在生产环境中更新数据库模式?
- 29. 生产环境中的数据库超时问题
- 30. 为大型生产数据库创建开发环境
的伟大工程!谢谢 ! –