PHPUnit LogicException: The request was not redirected. Symfony

I am trying to test a simple registration with phpunit with redirects, I have this all the time:

enter image description here

Test class:

<?php

namespace Tests\AppBundle\Services;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecuriteTest extends WebTestCase
{

    private $user;

    public function __construct() {
        $this->user = static::createClient();
    }

    public function testsendCreateAccountMail(){

        $crawler = $this->user->request('GET', '/inscription');

        $this->assertEquals(1, $crawler->filter('h1:contains("Informations personnelles")')->count());

        $form = $crawler->selectButton('submitInscription')->form();

        $form['inscription[gender]']     = 'Masculin';
        $form['inscription[name]']       = 'Test';
        $form['inscription[firstName]']      = 'Test';
        $form['inscription[username]']    = 'Test-25@gmail.com';
        $form['inscription[birthDate]']    =  '05/10/1992';
        $form['inscription[pseudo]']    = 'Test';
        $form['inscription[password][first]']    = 'blablabla';
        $form['inscription[password][second]']    = 'blablabla';
        $form['inscription[account]']    = 'Particulier';
        $form['inscription[mentionsLegales]']    = '1';

        $this->user->submit($form);

        $crawler = $this->user->followRedirect();

        $this->assertEquals(1, $crawler->filter('.testFlash:contains("Vous allez recevoir une demande de confirmation sur votre adresse email")')->count());

    }
}

      

I am also trying to validate a contact form with only 3 inputs, I still have the same error.

thank

+3


source to share


1 answer


I suggest you add more tests about every aspect of the stream, for example as the status of the http link. As an example, you can add after each interaction with the server that the request was ok:

    $crawler = $this->user->request('GET', '/inscription');
    // check server response:
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());
    // or more simply:
    $this->assertTrue($client->getResponse()->isSuccessful());
    // then check the response content
    $this->assertEquals(1, $crawler->filter('h1:contains("Informations personnelles")')->count());

      

Likewise, before checking the redirect, check that the mail is working fine and does not give an error like 500:



    $this->user->submit($form);
    $this->assertTrue($client->getResponse()->isRedirection());
    $crawler = $this->user->followRedirect();

      

Hope for this help

+2


source







All Articles