Werner Echezuria
Werner Echezuria

Reputation: 622

Do Doctrine flush method works asynchronous?

I have a functional test that creates several records and then makes some request calls, the tests sometimes passes and others not, it's really weird, when I use var_dump it sometimes give me the amount of records I was requiring, and other times it just give me less than that.

This is the code:

    foreach (range(0, 80) as $number)
    {
        $citaDetalle = new CitasDetalle();
        $citaDetalle->setCodigo('FF#')
            ->setCitaGenerator($generator)
            ->setUidCreate($user)
            ->setFechaCita( DateExtension::nextLaborDay((new \DateTime())->modify("+5 Day"), false, false) )
            ->setCitaTurno($turno)
            ->setCitaPlace($place)
        ;
        $em->persist($citaDetalle);
    }
    foreach (range(0, 20) as $number)
    {
        $citaDetalle = new CitasDetalle();
        $citaDetalle->setCodigo('FF#')
            ->setCitaGenerator($generator)
            ->setUidCreate($user)
            ->setFechaCita( DateExtension::nextLaborDay((new \DateTime())->modify("+5 Day"), false, false) )
            ->setCitaTurno($turno2)
            ->setCitaPlace($place)
        ;
        $em->persist($citaDetalle);
    }
    $em->flush();

    $crawler = $this->client->request('GET', '/c/g/citas/new');
    $this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
        "Unexpected HTTP status code for GET /c/g/citas/new");

    $form = $crawler->selectButton('Generar Cita')->form([
        'core_gestion_bundle_citas_detalle_type[citaGenerator]' =>
            $crawler->filter('#core_gestion_bundle_citas_detalle_type_citaGenerator option:contains("Generator Test")')->attr('value')
    ]);
    $this->client->submit($form);

    $this->client->followRedirect();

    $lastDate = $em->getRepository('CoreGestionBundle:CitasDetalle')
        ->obtenerUltimaCita()[0]->getFechaCita();

    $compareDate = DateExtension::nextLaborDay((new \DateTime())->modify("+6 Day"));

    $this->assertEquals($compareDate->format('Y-m-d'), $lastDate->format('Y-m-d'));

Upvotes: 1

Views: 1873

Answers (1)

DonCallisto
DonCallisto

Reputation: 29912

This is not a proper way to test things. Why would you create over and over again records in your db? It's silly as with DataFixtures you can reach the same but you can do only once (and, more important, you don't need to "littering your test code").

Remember also that your db should be cleared and restored at every test (or, if you're able to do this, test "write" onto db with transaction and, in tearDown() function, discard changes)

Answer to your question

No, doctrine will not do things in async. way, your problem must be somewhere else.

Upvotes: 1

Related Questions