Symfony2:为 phpunit 功能测试传递 GET 参数
Symfony2: pass GET parameters for a phpunit functional test
在扩展 WebTestCase 的测试中,我想测试一些东西,这取决于传递 GET 参数。
可在此处找到 POST 参数的解决方案:
Symfony2 Functional test: Passing form data directly
但是,如果我将此模拟用于 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',
]
);
class 中的部分,它们被评估的地方:
$crossSaleParametersValue = $requestStack
->getCurrentRequest()
->query->get('paramName');
if (!empty($crossSaleParametersValue)) {
echo 'crossSale';
}
有人知道如何传递我的 GET 参数吗?
在“/”之后添加它们的肮脏尝试也不起作用。
如何将我的 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',
]
);
}
在扩展 WebTestCase 的测试中,我想测试一些东西,这取决于传递 GET 参数。
可在此处找到 POST 参数的解决方案: Symfony2 Functional test: Passing form data directly
但是,如果我将此模拟用于 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',
]
);
class 中的部分,它们被评估的地方:
$crossSaleParametersValue = $requestStack
->getCurrentRequest()
->query->get('paramName');
if (!empty($crossSaleParametersValue)) {
echo 'crossSale';
}
有人知道如何传递我的 GET 参数吗?
在“/”之后添加它们的肮脏尝试也不起作用。
如何将我的 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',
]
);
}