我想编写一个测试用例,通过Doctrine实体管理器验证实体是否已完全持久化到数据库。
例如:
<?php
function notPersisted() {
return new Entity();
}
function persistedButNotFlushed() {
$entity = new Entity();
$entityManager->persist($entity);
return $entity;
}
function persistedAndFlushed() {
$entity = new Entity();
$entityManager->persist($entity);
$entityManager->flush();
return $entity;
}
function persistedButNotAllChangesFlushed() {
$entity = new Entity();
$entityManager->persist($entity);
$entityManager->flush();
$entity->setFoo('bar');
$entityManager->persist($entity);
return $entity;
}
$this->assertNotPersistedToDb(notPersisted());
$this->assertNotPersistedToDb(persistedButNotFlushed());
$this->assertPersistedToDb(persistedAndFlushed());
$this->assertNotPersistedToDb(persistedButNotAllChangesFlushed());
你会怎么做呢?
答案 0 :(得分:2)
这是我想出的:
protected function assertEntityIsPersistedToDb($entity) {
$unitOfWork = $this->entityManager->getUnitOfWork();
$isManaged = $this->entityManager->contains($entity);
$isFlushedToDb = !$unitOfWork->isEntityScheduled($entity);
$id = $this->getEntityId($entity);
$entityClassName = get_class($entity);
$msg = "Expected entity of type $entityClassName to be persisted to the database, ";
if (!$id) {
$msg .= "but the entity does not have an id.";
}
else if (!$isManaged) {
$msg .= "but entity is not managed by the entity manager.";
}
else if (!$isFlushedToDb) {
$msg .= "but the entity has changes which have not yet been flushed.";
}
$this->assertTrue(!!$id && $isManaged && $isFlushedToDb, $msg);
}
protected function assertEntityNotPersistedToDb($entity) {
// Ensure that this is a managed entity
if (!$this->entityManager->contains($entity)) {
throw new \PHPUnit_Framework_ExpectationFailedException(
"Unable to determine if entity is persisted to DB: the entity is not managed."
);
// Alternatively, we could maybe do an $em->merge($entity),
// and then check its state. However, this could change our application
// state, and potential effect test behavior.
}
$isFlushedToDb = !$this->entityManager->getUnitOfWork()
->isEntityScheduled($entity);
$entityClass = get_class($entity);
$msg = "Expected entity of type $entityClass not to be persisted to the database.";
$this->assertFalse($isFlushedToDb, $msg);
}
protected function getEntityId($entity) {
$idList = $this->entityManager
->getClassMetadata(get_class($entity))
->getIdentifier();
return empty($idList) ?
null : call_user_func([$entity, 'get' . ucfirst(reset($idList))]);
}
我已经玩了一下它似乎有效,但我还没有通过任何严格的测试。
答案 1 :(得分:0)
根据您的工作,如果您的对象中有多个关联且未正确设置级联选项,则分离可能会很麻烦,这两个函数可帮助您断言是否在应用程序状态下管理实体。
a b c
5 23 0
20 16 0
23 43 4
静态因为它不是“正式”测试,而且我有多个实体管理器实例。