PHPUnit LogicException:请求未被重定向。 Symfony的

时间:2017-06-05 11:48:03

标签: symfony phpunit

我正在尝试使用带有重定向的phpunit来测试一个简单的注册,我一直都有这个错误:

enter image description here

测试类:

<?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());

    }
}

我也尝试使用只有3个输入的联系表格进行测试,但我仍然有同样的错误。

谢谢

1 个答案:

答案 0 :(得分:2)

我建议你添加关于流程各个方面的更多测试,例如http通信的状态。例如,您可以在每次服务器交互后添加请求正常:

    $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());

以同样的方式,在按照重定向测试之前,帖子工作正常并且不会给出像500这样的错误:

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

希望这个帮助