vendredi 28 octobre 2016

Functional test of form Symfony3 mocked session

I'm writing functional tests for my symfony3 application and I have a problem that when I'm trying to test sending forms, by crawling the submit button.

Method what I want to test:

$personalDataForm->handleRequest($request);
if ($personalDataForm->isValid()) {
  return $this->handlePersonalForm($personalDataForm, $request);
}

return $this->render('members/personal-form.html.twig', [
  'personalDataForm' => $personalDataForm->createView(),
]);

Functional test:

    $client = static::createClient();
    $client->getCookieJar()->set($this->cookie);
    $client->followRedirects();

    $container = $client->getContainer();
    $container->set('session', $this->session);

    $crawler = $client->request('GET', '/');

    $form = $crawler->selectButton('Save')->form();
    $client->submit($form);

Declaration of $this->session:

$this->session = new Session(new MockArraySessionStorage(), new AttributeBag(), new FlashBag())

The crawler is correctly sending a request, but I have an error Failed to start the session because headers have already been sent by "phar:///usr/local/bin/phpunit/phpunit/Util/Printer.php" at line 134.

When I'm looking at test result I see that my test didn't pass the $personalDataForm->isValid() condition.

I think the problem is that after submitting form, application is using Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage to store a session instead of mocked session. How can I pass mocked session when I'm submitting form?

Aucun commentaire:

Enregistrer un commentaire