Symfony 2-PHPUnit测试和标头已发送

Symfony 2-PHPUnit测试和标头已发送,php,symfony,cookies,login,phpunit,Php,Symfony,Cookies,Login,Phpunit,我对PHPUnit和Symfony2有问题。我试图重现登录操作。在我的应用程序中,我使用web服务向日志用户获取数据,所以我创建了一个个人用户提供者。如果表单中的用户数据良好,我会创建一个cookie。当我连接时,会创建两个cookie:记住cookie(自动创建)和另一个包含一些数据的cookie 我创建了一个简单的函数,用以下代码测试我的登录页面: // TEST page de login avec de bonnes informations $crawler = $client->

我对PHPUnit和Symfony2有问题。我试图重现登录操作。在我的应用程序中,我使用web服务向日志用户获取数据,所以我创建了一个个人用户提供者。如果表单中的用户数据良好,我会创建一个cookie。当我连接时,会创建两个cookie:记住cookie(自动创建)和另一个包含一些数据的cookie

我创建了一个简单的函数,用以下代码测试我的登录页面:

// TEST page de login avec de bonnes informations
$crawler = $client->request('GET', '/login');
$form = $crawler->selectButton('submit')->form();
$form['_username'] = $numero;
$form['_password'] = $pass;
$client->submit($form);
$client->followRedirects();
当我执行
phpunit-c应用程序——覆盖率html报告时
控制台中没有bug,但文件
test.log
显示:

request.CRITICAL: Uncaught PHP Exception PHPUnit_Framework_Error_Warning: "Cannot modify header information - headers already sent by (output started at C:\_dev\php\EasyPHP\data\localweb\vendor\phpunit\phpunit\src\Util\Printer.php:139)" at C:\_dev\php\EasyPHP\data\localweb\myproject\src\project\projectBundle\Security\projectAuthenticator.php line 72 {"exception":"[object] (PHPUnit_Framework_Error_Warning(code: 2): Cannot modify header information - headers already sent by....
在文件
projectAuthenticator.php
的第72行,我创建了如下cookie:

setcookie('mycookie', $cookieData, time() + (3600 * 24 * 365));
我试着用了,但没用

如果我通过cookie的创建,我将返回一个
UsernamePasswordToken
对象,我将被连接

那么我如何创建我的cookie来通过PHPUnit呢?因为我需要cookie的数据和正确的会话来测试私有页面并继续我的测试

我希望你能理解


谢谢

在定义cookie时,您应该使用Symfony类:

// Create the cookie
$nextYear = new \Datetime('now');
$nextYear->add(new \DateInterval('P1Y'));

$cookie = new Symfony\Component\HttpFoundation\Cookie(
    'mycookie', $cookieData, $nextYear->format('U'));

$response = $this->render(
    'AppBundle:Default:index.html.twig',
    array(
        'variables' => $variables,
    )
);

// Add the cookie to the response
$response->headers->setCookie($cookie);

return $response;

这将避免PHPUnit报告的错误。

定义cookie时应使用Symfony类:

// Create the cookie
$nextYear = new \Datetime('now');
$nextYear->add(new \DateInterval('P1Y'));

$cookie = new Symfony\Component\HttpFoundation\Cookie(
    'mycookie', $cookieData, $nextYear->format('U'));

$response = $this->render(
    'AppBundle:Default:index.html.twig',
    array(
        'variables' => $variables,
    )
);

// Add the cookie to the response
$response->headers->setCookie($cookie);

return $response;

这将避免PHPUnit报告的错误。

Put
$client->followRedirects()首先。否则您的代码应该是精细的,您的意思是在
$crawler=$client->request('GET','/login')之前或在函数测试开始时?创建客户端后(在包含
static::createClient();
的行之后)谢谢,我在添加arg
--stderr
时没有出现错误,如下所示:
phpunit-c应用程序--coverage html报告--stderr
。所以我不知道这是否是一个解决方案…我误解了你的问题,如果你想测试你的私人页面,你应该看看这个,并寻找
PHP\u AUTH\u USER
。Put
$client->followRedirects()首先。否则您的代码应该是精细的,您的意思是在
$crawler=$client->request('GET','/login')之前或在函数测试开始时?创建客户端后(在包含
static::createClient();
的行之后)谢谢,我在添加arg
--stderr
时没有出现错误,如下所示:
phpunit-c应用程序--coverage html报告--stderr
。所以我不知道这是否是一个解决方案…我误解了你的问题,如果你想测试你的私人页面,你应该看看这个,并寻找
PHP\u AUTH\u USER