Symfony 如何测试用户登录页面?

Symfony 如何测试用户登录页面?,symfony,phpunit,fosuserbundle,Symfony,Phpunit,Fosuserbundle,我已经安装了FosUserBundle,我想用PhpUnit做一个测试,检查用户是否可以登录,但我的代码肯定是错误的,因为我无法重定向 我的代码: public function testLogin() { $client = static::createClient(); $crawler = $client->request('GET', '/crm/login'); $buttonCrawlerNode = $crawler-&

我已经安装了FosUserBundle,我想用PhpUnit做一个测试,检查用户是否可以登录,但我的代码肯定是错误的,因为我无法重定向

我的代码:

public function testLogin()
    {
        $client = static::createClient();
        $crawler = $client->request('GET', '/crm/login');
        $buttonCrawlerNode = $crawler->selectButton('_submit');
        $form = $buttonCrawlerNode->form();
        $data = array('_username' => 'root','_password' => 'toor');
        $client->submit($form,$data);
        $crawler = $this->client->followRedirect();
        $crawler = $client->request('GET', '/crm/home');
    }

似乎您正在使用两个不同的
client

public function testLogin()
{
    $client = static::createClient();
    $crawler = $client->request('GET', '/crm/login');
    $buttonCrawlerNode = $crawler->selectButton('_submit');
    $form = $buttonCrawlerNode->form();
    $data = array('_username' => 'root','_password' => 'toor');
    $client->submit($form,$data);

    //here you're using $this->client not $client
    $crawler = $this->client->followRedirect();
    $crawler = $client->request('GET', '/crm/home');
}

可能您正在使用不同的客户端实例<代码>$this->client->followRedirect()->
$client->followRedirect()