symfony WebTestCase 客户端不发送 post 数据

symfony WebTestCase client does not send post data

我目前正在为我的 symfony 应用程序编写功能测试。我将 symfony 3 (3.1.6) 与 phpunit 5.6.1 一起使用。 编辑 :根据 Alvin Bunk 的要求,我的应用程序不是网站,它是一个 API,returns 只有 JSON。正如我在下面的两个更新中添加的那样,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());
}

调用了我的控制器的正确操作,但是当从测试套件调用操作时,请求对象中没有任何内容。使用普通的网络浏览器,一切正常。在控制器中,我使用 $request = Request::createFromGlobals(); 创建请求对象

我也尝试使用此代码 post 数据,我得到相同的结果:控制器中未收到 POST 数据。

不使用表单直接post请求

public function testSaveMediaFromMediaUrl()
{
    $client = static::createClient();
    $crawler = $client->request('POST', '/media', ['mediaUrl' => 'http://example.com']);

    var_dump($crawler->html());
}

在提交方法中添加数据

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_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) ""
  }
}

"test browser" 似乎将表单数据发送到应用程序...

通常是 post 对表单的请求 return 重定向,如果是这样,请检查重定向是否是正确的响应并遵循重定向,例如:

$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
}