如何检查条令实体是否持久化并刷新到数据库


How to check if Doctrine entity is persisted and flushed to database?

我想写一个测试用例,通过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());

你会怎么做?

以下是我的想法:

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))]);
}

我对它进行了一些尝试,它似乎有效,尽管我还没有对它进行任何严格的测试。

根据您的工作,当您的对象中有多个关联,并且级联选项设置不正确时,分离可能会很痛苦,这两个函数可以帮助您断言实体是否在应用程序状态中管理。

static function assertEntityIsManaged(EntityManager $em, $entity) {
    $isManaged = $em->contains($entity);
    $entityClass = get_class($entity);
    $msg = "Expected entity of type $entityClass IS NOT Managed";
    assert($isManaged === TRUE, $msg);
}
static function assertEntityNotManaged(EntityManager $em, $entity) {
    $isManaged = $em->contains($entity);
    $entityClass = get_class($entity);
    $msg = "Expected entity of type $entityClass IS Managed";
    assert($isManaged === FALSE, $msg);
}

静态,因为它不是一个"正式"测试,而且我有多个实体管理器实例。