Testing symfony WebTestCase客户端不发送post数据
我目前正在为我的symfony应用程序编写函数测试。我将symfony 3(3.1.6)与phpunit 5.6.1一起使用。 编辑:根据Alvin Bunk的要求,我的应用程序不是一个网站,它是一个只返回JSON的API。正如我在下面的两个更新中所添加的,Symfony测试客户端发送一个带有表单数据的适当请求对象,但应用程序的控制器接收到一个空对象 以下是我用来测试表单的代码:Testing symfony WebTestCase客户端不发送post数据,testing,phpunit,symfony,Testing,Phpunit,Symfony,我目前正在为我的symfony应用程序编写函数测试。我将symfony 3(3.1.6)与phpunit 5.6.1一起使用。 编辑:根据Alvin Bunk的要求,我的应用程序不是一个网站,它是一个只返回JSON的API。正如我在下面的两个更新中所添加的,Symfony测试客户端发送一个带有表单数据的适当请求对象,但应用程序的控制器接收到一个空对象 以下是我用来测试表单的代码: public function testSaveMediaFromMediaUrl() { $client
public function testSaveMediaFromMediaUrl()
{
$client = static::createClient();
$crawler = $client->request('GET', '/form');
$form = $crawler->selectButton('OK')->form();
$form['mediaUrl'] = 'http://example.com';
$client->submit($form);
var_dump($client->getResponse()->getContent());
}
调用了控制器的正确操作,但从测试套件调用该操作时,请求对象中没有任何内容。使用普通的网络浏览器,一切正常。在控制器中,我使用$request=request::createFromGlobals()代码>以创建请求对象
我还尝试了这段代码来发布数据,得到了相同的结果:控制器中没有接收到post数据
不使用表格直接提交请求
public function testSaveMediaFromMediaUrl()
{
$client = static::createClient();
$crawler = $client->request('POST', '/media', ['mediaUrl' => 'http://example.com']);
var_dump($crawler->html());
}
在submit方法中添加数据
public function testSaveMediaFromMediaUrl()
{
$client = static::createClient();
$crawler = $client->request('GET', '/form');
$form = $crawler->selectButton('OK')->form();
$client->submit($form, ['mediaUrl' => 'http://example.com']);
var_dump($client->getResponse()->getContent());
}
我做错什么了吗
编辑:
这是我在控制器操作中得到的请求对象的转储
.object(Symfony\Component\HttpFoundation\Request)#1047 (21) {
["attributes"]=>
object(Symfony\Component\HttpFoundation\ParameterBag)#1050 (1) {
["parameters":protected]=>
array(0) {
}
}
["request"]=>
object(Symfony\Component\HttpFoundation\ParameterBag)#1048 (1) {
["parameters":protected]=>
array(0) {
}
}
["query"]=>
object(Symfony\Component\HttpFoundation\ParameterBag)#1049 (1) {
["parameters":protected]=>
array(0) {
}
}
["server"]=>
object(Symfony\Component\HttpFoundation\ServerBag)#1053 (1) {
["parameters":protected]=>
array(35) {[...]}
}
["files"]=>
object(Symfony\Component\HttpFoundation\FileBag)#1052 (1) {
["parameters":protected]=>
array(0) {
}
}
["cookies"]=>
object(Symfony\Component\HttpFoundation\ParameterBag)#1051 (1) {
["parameters":protected]=>
array(0) {
}
}
["headers"]=>
object(Symfony\Component\HttpFoundation\HeaderBag)#1054 (2) {
["headers":protected]=>
array(0) {
}
["cacheControl":protected]=>
array(0) {
}
}
["content":protected]=>
NULL
["languages":protected]=>
NULL
["charsets":protected]=>
NULL
["encodings":protected]=>
NULL
["acceptableContentTypes":protected]=>
NULL
["pathInfo":protected]=>
NULL
["requestUri":protected]=>
NULL
["baseUrl":protected]=>
NULL
["basePath":protected]=>
NULL
["method":protected]=>
NULL
["format":protected]=>
NULL
["session":protected]=>
NULL
["locale":protected]=>
NULL
["defaultLocale":protected]=>
string(2) "en"
}
编辑-2:
下面是客户端发送的请求对象的转储(在测试用例中:var\u dump($client->getRequest()->request);
):
“测试浏览器”似乎将表单数据发送到应用程序…通常是对表单的post请求返回重定向,如果是,请检查重定向是否为正确响应,并遵循重定向,例如:
$client->submit($form);
$this->assertTrue($client->getResponse()->isRedirect());
$this->crawler = $client->followRedirect();
希望这有帮助
编辑:
另一种方法可以是:
$form = $crawler->selectButton('OK')->form(array(
'mediaUrl' => 'http://example.com')
);
$client->submit($form);
解决的问题:
在我的控制器中,我使用了文档中编写的$request=request::createFromGlobals()
。我删除了这一行并添加了$request
作为控制器操作的参数,现在请求包含测试客户端发送的POST数据
我的行为现在定义如下:
public function generateAction(Request $request) {
// no more $request = Request::createFromGlobals();
$request->request->get('mediaUrl'); // contains data
}
我没有重定向,该应用程序不是一个网站,它只是一个API,因此表单返回一个带有状态的json。在form()
中传递数据不会改变任何事情……您应该在帖子中提到它是一个restful api,并且您希望返回JSON-因为没有在你的帖子中显示这些信息,@Matteo给出了一个很好的答案(和往常一样),你应该投票。如果你更新了你的帖子以包含api信息,那么我会更新它。这不会改变我的问题。。。问题是,在测试客户端发送的请求和对应用程序内核的处理之间,POST数据似乎丢失了:我的控制器接收到一个空的请求对象,它抛出了一个错误,因此没有返回任何内容HTML或JSON(只是一个500错误)。我编辑了POST以向应用程序添加信息。马特奥给出的答案并没有改变问题的任何方面。。。
public function generateAction(Request $request) {
// no more $request = Request::createFromGlobals();
$request->request->get('mediaUrl'); // contains data
}