vendredi 7 juin 2019

Get Symfony flashes during functionnal test with profiler enabled

I am writing functionnal tests for a Symfony 4.2 app.
Sometimes I want to check if a flash message have been set, I created this method:

protected function hasFlash(string $type, int $min = 1) : bool
{
    $flashes = $this->client
        ->getContainer()
        ->get('session')
        ->getBag('flashes')
        ->all();

    return count($flashes[$type] ?? []) >= $min;
}

It worked pretty well during the first tests.
Sample code example:

// User can reset password
$crawler = $this->client->request('GET', $uri);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());

// Submit form
$form = $crawler->selectButton('Update')->form($form_data);
$this->client->submit($form);
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertTrue($this->hasFlash('success'));

I then needed to test for emails sent by SwiftMailer which means that the Profiler must be enabled, using $this->client->enableProfiler():

// Submit
$this->client->enableProfiler();
$this->client->submit($form);
$collector = $this->client->getProfile()->getCollector('swiftmailer');

// User created and emailed
$this->assertTrue($this->hasFlash('success'));
$this->assertSame(1, $collector->getMessageCount());

Works well too, but now the Flash Bag is empty.
How can/should I retrieve them ?

Aucun commentaire:

Enregistrer un commentaire