我有一个symfony网站,我正在尝试做一些单元测试。我有这样的测试,我尝试提交一些东西:
<?php
namespace Acme\AcmeBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class HomeControllerTest extends WebTestCase {
public function testrandomeThings() {
$client = static::createClient();
$crawler = $client->request(
'POST',
'/',
array(
"shopNumber" => 0099,
"cardNumber" => 231,
"cardPIN" => "adasd"),
array(),
array());
}但我不认为我发送的数据是在控制器中接收的:
class HomeController extends Controller
{
public function indexAction()
{
var_dump($_POST);
die;
return $this->render('AcmeBundle:Home:index.html.twig');
}
}var_dump实际上返回给我一个空数组。
我在通过POST请求发送信息时遗漏了什么?
发布于 2013-09-20 01:22:47
$_POST是一个由PHP填充的变量,只有在直接通过http调用时,symfony请求才是从这个全局变量创建的。symfony爬虫不会发出真正的请求,它会根据$client->request中提供的参数创建一个请求并执行它。您需要通过Request对象访问这些内容。不要直接使用$_POST、$_GET等。
use Symfony\Component\HttpFoundation\Request;
class HomeController extends CoralBaseController
{
public function indexAction(Request $request)
{
var_dump($request->request->all());
die;
return $this->render('CoralWalletBundle:Home:index.html.twig');
}
}使用$request->request->all()获取数组中的所有POST参数。要只获取特定的参数,可以使用$request->request->get('my_param')。如果需要访问GET参数,可以使用$request->query->get('my_param'),但最好设置路由模式中已有的查询参数。
发布于 2017-06-04 06:18:53
我觉得你是想这么做:
$client = static::createClient();
$client->request($method, $url, [], [], [], json_encode($content));
$this->assertEquals(
200,
$client->getResponse()
->getStatusCode()
);您将数据(内容)作为params数组放入,但是希望将其作为原始主体内容放入,这是一个JSON编码的字符串。
https://stackoverflow.com/questions/18900890
复制相似问题