ᴄʀᴏ*_*ᴢᴇᴛ 5 testing phpunit symfony
我目前正在为我的 symfony 应用程序编写功能测试。我使用 symfony 3 (3.1.6) 和 phpunit 5.6.1。 编辑:根据 Alvin Bunk 的要求,我的应用程序不是一个网站,它是一个仅返回 JSON 的 API。正如我在下面的两个更新中添加的那样,Symfony 测试客户端发送带有表单数据的正确请求对象,但应用程序的控制器收到一个空对象。
这是我用来测试我的表单的代码:
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());
}
Run Code Online (Sandbox Code Playgroud)
我的控制器的正确操作被调用,但当从测试套件调用该操作时,请求对象中没有任何内容。使用常规网络浏览器,一切正常。在控制器中,我用来$request = Request::createFromGlobals();
创建请求对象
我还尝试使用此代码来发布数据,并得到相同的结果:控制器中未收到 POST 数据。
不使用表格直接邮寄请求
public function testSaveMediaFromMediaUrl()
{
$client = static::createClient();
$crawler = $client->request('POST', '/media', ['mediaUrl' => 'http://example.com']);
var_dump($crawler->html());
}
Run Code Online (Sandbox Code Playgroud)
在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());
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么吗?
这是我在控制器操作中获得的请求对象的转储。
.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"
}
Run Code Online (Sandbox Code Playgroud)
这是客户端发送的请求对象的转储(在测试用例中var_dump($client->getRequest()->request);
:):
object(Symfony\Component\HttpFoundation\ParameterBag)#753 (1) {
["parameters":protected]=>
array(4) {
["mediaUrl"]=>
string(41) "http://example.com"
["url"]=>
string(0) ""
["token"]=>
string(0) ""
["sizes"]=>
string(0) ""
}
}
Run Code Online (Sandbox Code Playgroud)
“测试浏览器”似乎将表单数据发送到应用程序......
问题解决了 :
在我的控制器中,我$request = Request::createFromGlobals()
按照文档中所写的那样使用。我删除了这一行并添加$request
为控制器操作的参数,现在请求包含测试客户端发送的 POST 数据。
我的行动现在定义如下:
public function generateAction(Request $request) {
// no more $request = Request::createFromGlobals();
$request->request->get('mediaUrl'); // contains data
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2972 次 |
最近记录: |