Symfony2:通过phpunit功能测试的GET参数
在扩展WebTestCase的测试中,我想测试一些东西,这取决于传递GET参数 可在此处找到POST参数的解决方案: 但是如果我用这个模拟来做GET,它就不起作用了 测试代码:Symfony2:通过phpunit功能测试的GET参数,symfony,get,phpunit,Symfony,Get,Phpunit,在扩展WebTestCase的测试中,我想测试一些东西,这取决于传递GET参数 可在此处找到POST参数的解决方案: 但是如果我用这个模拟来做GET,它就不起作用了 测试代码: // given $client = static::createClient(); $paramValue = 'frwkuh437iwaw'; // when $client->request( 'GET', '/',
// given
$client = static::createClient();
$paramValue = 'frwkuh437iwaw';
// when
$client->request(
'GET',
'/',
['paramName' => $paramValue],
[],
[
'HTTP_Host' => 'boys2go.com',
'REMOTE_ADDR' => '127.0.0.1',
'HTTP_X-FORWARDED-FOR' => '127.0.0.1',
]
);
在课堂上进行评估的部分:
$crossSaleParametersValue = $requestStack
->getCurrentRequest()
->query->get('paramName');
if (!empty($crossSaleParametersValue)) {
echo 'crossSale';
}
有人知道如何传递GET参数吗
在“/”后面添加它们的肮脏尝试也不起作用
如何在测试中获取参数?解决方案可以是:
// Preferable, write this method in a parent class (that extends abstract
// class WebTestCase) to be used in other tests too
protected static function getUrl($route, $params = array())
{
return self::getKernel()->getContainer()->get('router')->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
}
public function testDemo() {
$client = static::createClient();
$paramValue = 'frwkuh437iwaw';
$url = $this->getUrl('route_name');
// when
$client->request(
'GET',
$url . '?paramName=' . $paramValue,
[],
[],
[
'HTTP_Host' => 'boys2go.com',
'REMOTE_ADDR' => '127.0.0.1',
'HTTP_X-FORWARDED-FOR' => '127.0.0.1',
]
);
}
解决办法可以是:
// Preferable, write this method in a parent class (that extends abstract
// class WebTestCase) to be used in other tests too
protected static function getUrl($route, $params = array())
{
return self::getKernel()->getContainer()->get('router')->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
}
public function testDemo() {
$client = static::createClient();
$paramValue = 'frwkuh437iwaw';
$url = $this->getUrl('route_name');
// when
$client->request(
'GET',
$url . '?paramName=' . $paramValue,
[],
[],
[
'HTTP_Host' => 'boys2go.com',
'REMOTE_ADDR' => '127.0.0.1',
'HTTP_X-FORWARDED-FOR' => '127.0.0.1',
]
);
}
只附加字符串而不调用getUrl有什么区别?getUrl的$params参数-应该放在哪里?我的带有GET参数的数组?如果在Symfony中有如下定义的路由:@route(“/edit/{id}”,name=“edit”),$params from getUrl()应该包含一个数组['id']=>99。在测试中,您发送一个查询参数,这是不同的。只附加字符串而不调用getUrl有什么区别?getUrl的$params参数-应该放在哪里?我的带有GET参数的数组?如果在Symfony中有如下定义的路由:@route(“/edit/{id}”,name=“edit”),$params from getUrl()应该包含一个数组['id']=>99。在测试中,您发送一个查询参数,这是不同的。