Enr*_*ent 5 php tdd post phpunit symfony
我有一个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());
}
Run Code Online (Sandbox Code Playgroud)
但我不认为我发送的数据是在控制器中收到的:
class HomeController extends Controller
{
public function indexAction()
{
var_dump($_POST);
die;
return $this->render('AcmeBundle:Home:index.html.twig');
}
}
Run Code Online (Sandbox Code Playgroud)
在var_dump实际上我返回一个空数组.
通过我的POST请求发送信息我错过了什么?
$_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');
}
}
Run Code Online (Sandbox Code Playgroud)
用于$request->request->all()获取数组中的所有POST参数.要获得只能使用的特定参数$request->request->get('my_param').如果您需要访问可以使用的GET参数$request->query->get('my_param'),但更好地设置路由模式中已有的查询参数.
小智 7
我认为你正在尝试这样做:
$client = static::createClient();
$client->request($method, $url, [], [], [], json_encode($content));
$this->assertEquals(
200,
$client->getResponse()
->getStatusCode()
);
Run Code Online (Sandbox Code Playgroud)
您将数据(内容)作为 params 数组放入,但希望将其作为原始正文内容放入,该内容是 JSON 编码的字符串。
| 归档时间: |
|
| 查看次数: |
11516 次 |
| 最近记录: |