【发布时间】:2019-01-24 21:37:42
【问题描述】:
我有以下测试用例:
namespace Tests\AppBundle\Repository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use AppBundle\Entity\ContactEmail;
class ContactEmailTest extends KernelTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testInsert()
{
$email="jdoe@example.com";
/**
* @var Appbundle\Repository\ContactEmailRepository
*/
$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(Doctrine\DBAL\Exception\UniqueConstraintViolationException)
*/
public function testInsertDucplicate()
{
$email="jdoe@example.com";
/**
* @var Appbundle\Repository\ContactEmailRepository
*/
$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 AppBundle\Repository;
use AppBundle\Entity\ContactEmail;
/**
* ContactEmailRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ContactEmailRepository extends \Doctrine\ORM\EntityRepository
{
/**
* Adding an Email to the database
* @param String $email
*
* @throws Doctrine\DBAL\Exception\UniqueConstraintViolationException
*
* @return AppBundle\Entity\ContactEmail
*/
public function addEmail($email)
{
$emailToAdd=new ContactEmail();
$emailToAdd->setEmail($email);
/**
* @var Doctrine\ORM\EntityManager
*/
$em=$this->getEntityManager();
$em->persist($emailToAdd);
$em->flush();
return $emailToAdd;
}
}
那么在每次测试之后,我将如何对所有数据库条目进行核对,以便拥有一个全新的空数据库和一个干净的实例?
我问的原因是因为我不希望以前测试的剩余条目可能会破坏我的测试。
【问题讨论】:
-
也许我在你的代码中遗漏了一些应该让我更清楚的东西,但我很好奇你为什么要避免使用数据夹具?
-
我并没有避免它只是在我的测试用例中我不需要数据夹具。如果你仔细看,你会发现我测试了数据插入,因此不需要数据夹具。我只是把数据夹具引用为了区分我的问题和this 一个。
-
您可以在
setUp()或tearDown()或两者上重置数据库。例如。有一个快照文件系统重置数据库。
标签: doctrine-orm phpunit symfony-3.4