2013-02-01 130 views
1

在生产服务器上的每次代码更新之前,我通过在测试数据库中插入行来执行phpunit测试。 由于测试数据库不能反映生产数据库的内容,因此我想对生产数据库执行测试。 测试完成后,我想在测试过程中删除所有创建的行。什么是实现这一目标的最佳方式? 我无法想象一种完美的方法,无需更改生产数据。Symfony2测试数据库生产环境

回答

1

我使用Alexandre Salome在Isolation of tests in Symfony2中描述的方法来隔离我的测试和事务并在最后回滚。这种方法工作得很好,但显然你需要在生产数据库上使用它之前仔细测试它!

+0

的伟大工程!谢谢 ! –

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

谢谢,但我想对生产数据库执行测试 –

相关问题