嗨,我正在使用phpunit进行测试,使用Symfony''Bundle''FrameworkBundle''Test''WebTestCase进行单元测试。到目前为止没有问题,但现在我们开始使用https,我的测试不再有效。我开始为测试中的每个请求获得 301 响应代码。我的问题是我如何告诉Symfony''Component''HttpKernel''Client发出请求https://localhost.com/uri 而不是 http://localhost.com/uri ?
编辑
在symfony网站 http://symfony.com/doc/current/book/testing.html 中,他们展示了如何配置服务器参数,并且有一个代码和平,如
$client->request(
'GET',
'/demo/hello/Fabien',
array(),
array(),
array(
'CONTENT_TYPE' => 'application/json',
'HTTP_REFERER' => '/foo/bar',
'HTTP_X-Requested-With' => 'XMLHttpRequest',
)
);
我试图提供HTTPS元素,如中所述 http://php.net/manual/en/reserved.variables.server.php 更改了我的代码
$client->request('GET',
'/'.$version.'/agencies/'.$agencyId,
array(),
array(),
array('HTTPS' => 'on')
);
但是,它仍然不起作用?
多亏了@WouterJ我从以下更改了我的客户端创建:
static::createClient();
自:
static::createClient(array(),array('HTTPS' => true));
它解决了我的问题。事实证明,我无法在客户端 ->request 中给出HTTP_HOST和 HTTPS 参数。应在创建客户端时确定。
我正在使用Symfony4,这就是我创建功能测试的方式。我已经测试了以下代码,它工作正常。
namespace App'Tests'Controller;
use Symfony'Bundle'FrameworkBundle'Test'WebTestCase;
class CategoryControllerTest extends WebTestCase
{
public function testList()
{
$client = static::createClient();
$client->request('GET', '/category/list');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}