我有以下测试用例:
namespace TestsAppBundleRepository;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
use AppBundleEntityContactEmail;
class ContactEmailTest extends KernelTestCase
{
/**
* @var DoctrineORMEntityManager
*/
private $entityManager;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testInsert()
{
$email="jdoe@example.com";
/**
* @var AppbundleRepositoryContactEmailRepository
*/
$repository=$this->entityManager->getRepository(ContactEmail::class);
$contactEmailEntity=$repository->addEmail($email);
$this->assertEquals($contactEmailEntity->getEmail(),$email);
$emailSearched=$repository->findByEmail($email);
if(empty($emailSearched)){
$this->fail('No email has been found');
}
$this->assertEquals($email,$emailSearched[0]);
}
/**
* expectException(DoctrineDBALExceptionUniqueConstraintViolationException)
*/
public function testInsertDucplicate()
{
$email="jdoe@example.com";
/**
* @var AppbundleRepositoryContactEmailRepository
*/
$repository=$this->entityManager->getRepository(ContactEmail::class);
// We purpocely ingoring the returned value
$repository->addEmail($email);
$repository->addEmail($email);
}
/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
}
}
我尝试测试自定义存储库的以下方法:
namespace AppBundleRepository;
use AppBundleEntityContactEmail;
/**
* ContactEmailRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ContactEmailRepository extends DoctrineORMEntityRepository
{
/**
* Adding an Email to the database
* @param String $email
*
* @throws DoctrineDBALExceptionUniqueConstraintViolationException
*
* @return AppBundleEntityContactEmail
*/
public function addEmail($email)
{
$emailToAdd=new ContactEmail();
$emailToAdd->setEmail($email);
/**
* @var DoctrineORMEntityManager
*/
$em=$this->getEntityManager();
$em->persist($emailToAdd);
$em->flush();
return $emailToAdd;
}
}
那么,在每次测试之后,我将如何删除所有数据库条目,以便拥有一个具有干净实例的新鲜空数据库?
我问的原因是因为我不想以前测试的剩余条目可能会破坏我的测试。
一个好的做法是在这个网络中使用"在WebTestCase 上使用DataFixtures的phpUnitTest之后的phpUnitTest之后拆解数据库"的答案中提到的逻辑。为此,将setUp
方法更改为:
//namespace definition and use classes from other namespaces
use DoctrineORMToolsSchemaTool;
//Class definition etc etc...
protected function setUp()
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
//In case leftover entries exist
$schemaTool = new SchemaTool($this->entityManager);
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
// Drop and recreate tables for all entities
$schemaTool->dropSchema($metadata);
$schemaTool->createSchema($metadata);
}
如您所见,您可以通过$this->entityManager->getMetadataFactory()->getAllMetadata();
链方法调用获取架构信息,然后使用 chematool 以编程方式清除和创建架构。
另一个好方法是在我的情况下使用单独的数据库进行测试,我将以下配置放入config-test.yml
doctrine:
dbal:
dbname: '%database_name%-test'
这会自动在数据库名称中添加一个-test
结尾,因此,如果我进行开发的数据库被命名为mydb
测试数据库将被mydb-test
,因此您可以随意执行测试,而不必担心破坏开发工作流。