2013-02-01 2 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

감사하지만 프로덕션 데이터베이스에 대한 테스트를 수행하려고했습니다. –

관련 문제