'Database updates not readable while PHPunit WebTestCase (Symfony 5.1)
I have a problem asserting updates made by a form are really written in the database.
I explain, I first do a create test (testCreateLactationForm), very similar to the edit test. In that firs test y check the record is finally inserted and all works fine:
$finalRecords = $qb->where($qb->expr()->like('p.comments', ':comments'))
->setParameter('comments', '%'.$payload["form[comments]"].'%')
->getQuery()
->getResult();
$this->assertTrue(count($finalRecords) == (count($initialRecords) + 1));
After that, I made a dependant test where I locate las record, navigate to edit form, and update the modality and comments fields. In last line, when asserting Modality is equal to ONE_HOUR_REDUCTION, entity modality field remains with original creation value, so it fails, while in database the value was succesfully updated.
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);
}
In addition to that, if I try to write aditional dependant test, assertion is ok...
/**
* @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);
}
Some help? Thanks!!
Solution 1:[1]
Doctrine entities are persisted in an internal cache (which is not the case when you use findBy
methods, that load a new documents). You need to use
EntityManagerInterface
's refresh($entityObject)
or clear()
in order to get fresh data from the database.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | VinceOPS |