Unit testing 使用PHPUnit测试用例断言403访问被拒绝的http状态

Unit testing 使用PHPUnit测试用例断言403访问被拒绝的http状态,unit-testing,symfony,phpunit,http-error,Unit Testing,Symfony,Phpunit,Http Error,我在我的项目中有一个自定义错误模板,用于404、403和其他异常。我想创建单元测试用例来断言http错误。当我和用户一起登录并访问供应商的授权页面时,我在浏览器中得到403拒绝访问,但在单元测试案例中,我总是得到404未找到页面错误 以下是我的测试场景: class ErrorExceptionTest extends WebTestCase { public function testAccessDeniedException() { $server['HTTP

我在我的项目中有一个自定义错误模板,用于404、403和其他异常。我想创建单元测试用例来断言http错误。当我和用户一起登录并访问供应商的授权页面时,我在浏览器中得到403拒绝访问,但在单元测试案例中,我总是得到404未找到页面错误

以下是我的测试场景:

class ErrorExceptionTest extends WebTestCase
{
    public function testAccessDeniedException()
    {
        $server['HTTP_HOST'] = 'http://www.test.com/';
        $client = static::createClient(array('debug' => false), $server);
        $client->disableReboot();

        $session = $client->getContainer()->get('session');
        $firewall = 'main';

        $token = new UsernamePasswordToken('user', null, $firewall, array('ROLE_USER'));

        $session->set("_security_$firewall", serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $client->getCookieJar()->set($cookie);

        $client->request('GET', '/vendor/profile/edit');

        $this->assertEquals(403, $client->getResponse()->getStatusCode());
        $this->assertContains('Sorry! Access Denied',  $client->getResponse()->getContent());
    }
}

我的测试用例失败,当我打印响应内容时,它将显示404错误模板。

解决了这个问题并找到了问题。因此,我的解决方案是不需要使用http主机

public function testAccessDeniedException()
{
    $client = static::createClient(array('debug' => false));

    $session = $client->getContainer()->get('session');
    $firewall = 'main';

    $token = new UsernamePasswordToken('user', null, $firewall, array('ROLE_USER'));

    $session->set("_security_$firewall", serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());
    $client->getCookieJar()->set($cookie);

    $client->request('GET', 'fr/vendor/profile/edit');

    $this->assertEquals(403, $client->getResponse()->getStatusCode());
    $this->assertContains('Sorry! Access Denied',  $client->getResponse()->getContent());
}