PHPunit WebTestCase (Symfony 5.1) 时数据库更新不可读
Database updates not readable while PHPunit WebTestCase (Symfony 5.1)
我无法断言表单所做的更新确实写入了数据库。
我解释一下,我首先做一个创建测试(testCreateLactationForm),与编辑测试非常相似。在第一个测试中,y 检查记录是否最终插入并且一切正常:
$finalRecords = $qb->where($qb->expr()->like('p.comments', ':comments'))
->setParameter('comments', '%'.$payload["form[comments]"].'%')
->getQuery()
->getResult();
$this->assertTrue(count($finalRecords) == (count($initialRecords) + 1));
在那之后,我做了一个依赖测试,我找到了 las 记录,导航到编辑表单,并更新了模态和评论字段。在最后一行,当断言 Modality 等于 ONE_HOUR_REDUCTION 时,实体模态字段保持原始创建值,因此失败,而在数据库中该值已成功更新。
class HhrrProceduresControllerTest extends WebTestCase
{
/** @var \Doctrine\ORM\EntityManager */
private $entityManager;
private $client;
public function setUp()
{
parent::setUp();
$this->client = self::createClient();
$kernel = self::bootKernel();
$this->entityManager = $kernel
->getContainer()
->get('doctrine')
->getManager();
$this->hrmLogger = $kernel
->getContainer()
->get('monolog.logger.hrm');
}
[...]
/**
* @depends testCreateLactationForm
*/
public function testEditLactationProcedure()
{
$payload = [
"hhrr_procedure_lactation[modality]" => HhrrProcedureLactationModality::ONE_HOUR_REDUCTION,
"hhrr_procedure_lactation[iniDate]" => "07/02/2022",
"hhrr_procedure_lactation[endDate]" => "13/02/2022",
"hhrr_procedure_lactation[hhrrProcedure][comments]" => "XXX--TEST--XXX | UPDATED",
];
$qb = $this->entityManager->getRepository(HhrrProcedure::class)->createQueryBuilder('p');
$initialRecord = $qb->orderBy('p.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
$this->client->followRedirects(true);
$this->client->request('GET', '/rrhh/tramites/'.$initialRecord[0]->getId().'/editar-tramite/', [], [], ['HTTP_X-AUTH-USERNAME' => $this->logged_in_user]);
$crawler = $this->client->submitForm('Siguiente', $payload);
$this->writeOutput(__FUNCTION__, $this->client->getResponse()->getContent());
// verificar retorno correcto de la página
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
/** @var HhrrProcedure $updatedRecord */
$updatedRecord = $this->entityManager->getRepository(HhrrProcedure::class)->find($initialRecord[0]->getId());
/** FAILS --> **/ $this->assertEquals($updatedRecord->getHhrrProcedureLactation()->getModality(),HhrrProcedureLactationModality::ONE_HOUR_REDUCTION);
}
除此之外,如果我尝试编写额外的依赖测试,断言是可以的...
/**
* @depends testEditLactationProcedure
*/
public function testEditLactationProcedureAssertion()
{
$qb = $this->entityManager->getRepository(HhrrProcedure::class)->createQueryBuilder('p');
$initialRecord = $qb->orderBy('p.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
$this->assertEquals($initialRecord[0]->getHhrrProcedureLactation()->getModality(),HhrrProcedureLactationModality::ONE_HOUR_REDUCTION);
}
有帮助吗?谢谢!!
Doctrine 实体保存在内部缓存中(当您使用加载新文档的 findBy
方法时情况并非如此)。你需要使用
EntityManagerInterface
的 refresh($entityObject)
或 clear()
以便从数据库中获取新数据。
我无法断言表单所做的更新确实写入了数据库。
我解释一下,我首先做一个创建测试(testCreateLactationForm),与编辑测试非常相似。在第一个测试中,y 检查记录是否最终插入并且一切正常:
$finalRecords = $qb->where($qb->expr()->like('p.comments', ':comments'))
->setParameter('comments', '%'.$payload["form[comments]"].'%')
->getQuery()
->getResult();
$this->assertTrue(count($finalRecords) == (count($initialRecords) + 1));
在那之后,我做了一个依赖测试,我找到了 las 记录,导航到编辑表单,并更新了模态和评论字段。在最后一行,当断言 Modality 等于 ONE_HOUR_REDUCTION 时,实体模态字段保持原始创建值,因此失败,而在数据库中该值已成功更新。
class HhrrProceduresControllerTest extends WebTestCase
{
/** @var \Doctrine\ORM\EntityManager */
private $entityManager;
private $client;
public function setUp()
{
parent::setUp();
$this->client = self::createClient();
$kernel = self::bootKernel();
$this->entityManager = $kernel
->getContainer()
->get('doctrine')
->getManager();
$this->hrmLogger = $kernel
->getContainer()
->get('monolog.logger.hrm');
}
[...]
/**
* @depends testCreateLactationForm
*/
public function testEditLactationProcedure()
{
$payload = [
"hhrr_procedure_lactation[modality]" => HhrrProcedureLactationModality::ONE_HOUR_REDUCTION,
"hhrr_procedure_lactation[iniDate]" => "07/02/2022",
"hhrr_procedure_lactation[endDate]" => "13/02/2022",
"hhrr_procedure_lactation[hhrrProcedure][comments]" => "XXX--TEST--XXX | UPDATED",
];
$qb = $this->entityManager->getRepository(HhrrProcedure::class)->createQueryBuilder('p');
$initialRecord = $qb->orderBy('p.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
$this->client->followRedirects(true);
$this->client->request('GET', '/rrhh/tramites/'.$initialRecord[0]->getId().'/editar-tramite/', [], [], ['HTTP_X-AUTH-USERNAME' => $this->logged_in_user]);
$crawler = $this->client->submitForm('Siguiente', $payload);
$this->writeOutput(__FUNCTION__, $this->client->getResponse()->getContent());
// verificar retorno correcto de la página
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
/** @var HhrrProcedure $updatedRecord */
$updatedRecord = $this->entityManager->getRepository(HhrrProcedure::class)->find($initialRecord[0]->getId());
/** FAILS --> **/ $this->assertEquals($updatedRecord->getHhrrProcedureLactation()->getModality(),HhrrProcedureLactationModality::ONE_HOUR_REDUCTION);
}
除此之外,如果我尝试编写额外的依赖测试,断言是可以的...
/**
* @depends testEditLactationProcedure
*/
public function testEditLactationProcedureAssertion()
{
$qb = $this->entityManager->getRepository(HhrrProcedure::class)->createQueryBuilder('p');
$initialRecord = $qb->orderBy('p.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
$this->assertEquals($initialRecord[0]->getHhrrProcedureLactation()->getModality(),HhrrProcedureLactationModality::ONE_HOUR_REDUCTION);
}
有帮助吗?谢谢!!
Doctrine 实体保存在内部缓存中(当您使用加载新文档的 findBy
方法时情况并非如此)。你需要使用
EntityManagerInterface
的 refresh($entityObject)
或 clear()
以便从数据库中获取新数据。