Symfony 4 - 功能测试 - 尽管 followRedirects 为 true,但客户端未遵循重定向

ZFL*_*ZFL 4 php phpunit symfony

我一直在尝试开始在我正在开发的 Symfony 4 应用程序中实现一些功能测试,但我无法被重定向,最终从我的索引页面获得 200 响应代码,即使我在访问它时最终得到 200 响应通过本地 symfony 服务器。

这是有问题的测试

class MainControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $client->request('GET', '/fr');
        $client->followRedirects(true);
        var_dump($client->isFollowingRedirects());
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 phpunit 的输出:

#!/usr/bin/env php
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite
Fbool(true)
...                                                                4 / 4 (100%)

Time: 888 ms, Memory: 22.00MB

There was 1 failure:

1) App\Tests\Controller\MainControllerTest::testIndex
Failed asserting that 301 matches expected 200.
Run Code Online (Sandbox Code Playgroud)

FollowRedirects 确实设置为 true,但当我应该得到 200 响应时,我仍然得到 301 响应。

预先感谢您提供有关如何解决此问题的任何建议或答案:)

Jak*_*umi 5

followRedirects是一个设置。它用于随后完成的每个请求。

因此,在完成请求后进行设置是……好吧……没用。

所以...改变

    $client->request('GET', '/fr');
    $client->followRedirects(true);
Run Code Online (Sandbox Code Playgroud)

    $client->followRedirects(true);
    $client->request('GET', '/fr');
Run Code Online (Sandbox Code Playgroud)

它应该有效。