1

我正在开发一个 Symfony4 应用程序并想测试一个服务是否真的更新了我的数据库。

这里我给出一个数据传输对象

public function update(ChargeDto $chargeDto): ChargeDto
{
    $charge = $this->chargeRepository->find($chargeDto->id);
    // The mapper fills the new data into my entity
    $charge = ChargeMapper::fromDto($charge, $chargeDto);
    // here I check that the entity has in fact the new data 
    VarDumper::dump($charge->getAuthorPNumber());

    $this->entityManager->persist($charge);
    $this->entityManager->flush();
    $this->entityManager->refresh($charge);
    // here I check that the entity still has the new data 
    VarDumper::dump($charge->getAuthorPNumber());

    return ChargeMapper::toDto($charge, $chargeDto);
}

在我的测试类中,我从数据库中获取更新的数据集以检查它是否真的更新了:

    $res = $this->chargeRepo->find($updatedDto->id);
    VarDumper::dump($res->getAuthorPNumber());

我得到了旧数据。如果我手动查看数据库,我看到它实际上已更新并包含新值。

我不知道任何缓存都被主动激活。 我的测试如何获得真正的新值和更新值

PS:我只是仔细检查了:对于 prod 环境,我有一个用于学说缓存的配置( /config/prod/doctrine.yaml ),但对于开发环境和测试环境都没有。

4

1 回答 1

1

entityManager应该有一个名为 clear 的函数,它将删除缓存的对象。(确保你只在测试中调用它,而不是在实际代码中)

您还可以通过将 EntityName 作为参数传递给函数来从缓存中仅清除 1 种类型的实体。

于 2020-07-15T11:56:58.130 回答