我正在用Laravel 5制作API,我正在用PHPUnit测试它.我需要测试遗留功能的兼容性,这是一个XML POST.到目前为止,我的第一个测试看起来是这样的:

public function testPostMessages()
{
    $response = $this->call('POST', 'xml');

    $this->assertEquals(200, $response->getStatusCode());
}

这很好.我列表中的下一步实际上是随帖子一起发送XML数据.所以在我的第二次测试中,我有:

public function testPostMessagesContent()
{
    $response = $this->call('POST', 'xml');

    $this->assertTrue(is_valid_xml($response->getContent()));
}

This test fails. However, I am not sending my XML data. How do I send my XML?
Now, after that I need to add in the functionality to get the content from the request. I know there is a Request object that I can interact with but I don't exactly know which method to call on it. How do I get my XML from inside my controller?


更新#1

同时我也得到了一些结果.我目前的测试是这样的:

public function testPostMessagesContent()
{
    $response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));
    $this->assertContains('~', $response->getContent());
}

我只有瓷砖,因为我知道它不匹配,所以我可以看到整个react .在XmlController.php年中,我有:

class XmlController extends Controller {
public function index(Request $request)
{
    return $request;
    }
}

我对PHPUnit的输出如下:

1) XmlTest::testPostMessagesContent
Failed asserting that 'POST xml HTTP/1.1
Accept:          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Content-Type:    application/x-www-form-urlencoded
Host:            localhost
User-Agent:      Symfony/2.X
' contains "~".

我的参数和内容在哪里?我觉得我只是错误地使用了call().


更新#2

我将我的控制器更新为使用Request:all(),如下所示:

class XmlController extends Controller
{
    public function index()
   {
        $content = Request::all();
        return $content;
    }
}

这返回了我的键值对,但没有返回内容.

1) XmlTest::testPostMessagesContent
Failed asserting that '{"key":"value"}' contains "~".

键-值对很好;这是一种进步.但是,我真正需要的是内容,因为我将在请求的内容部分接收该数据.如果我使用Request::getContent(),我会返回一个空字符串.这是我在测试中的预测:

$response = $this->call('POST', 'xml', array('key' => 'value'), array(), array(), array('content' => 'content'));

以下是我的测试结果:

1) XmlTest::testPostMessagesContent
Failed asserting that '' contains "~".

更新#3

我根本无法获取HTTP请求的内容体.由于XML不起作用,我继续使用API的其余部分,它使用JSON.以下是我的一个测试:

public function testPostMessagesContent()
{
    $response = $this->call('POST', 'messages', ['content' => 'content']);

    $this->assertEquals('saved!', $response->getContent());
}

这个测试通过了.如果我使用curl,我也会接到一个成功的电话:

curl -X POST -d "content=my_new_content" "http://localhost:8000/messages"

这将返回'saved!',这太棒了,但是如果我try 在独立的PHP脚本中使用cURL(以模拟客户端),则返回以下内容:

Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 302 [header_size] => 603 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.063977 [namelookup_time] => 0.000738 [connect_time] => 0.000866 [pretransfer_time] => 0.000943 [size_upload] => 12 [size_download] => 328 [speed_download] => 5126 [speed_upload] => 187 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.057606 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63248 [redirect_url] => http://localhost:8000 [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) Redirecting to http://localhost:8000. 

这是我的curl命令,添加了POST字段:

curl_setopt($ch, CURLOPT_POSTFIELDS, 'content=test');

在我看来,POSTFIELDS正在被添加到请求的主体中.在这种情况下,我仍然有同样的问题.我无法获取HTTP头的正文内容.在 comments 了我的验证之后,我得到:

Array ( [url] => http://localhost:8000/messages [content_type] => text/html; charset=UTF-8 [http_code] => 200 [header_size] => 565 [request_size] => 118 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.070225 [namelookup_time] => 0.000867 [connect_time] => 0.00099 [pretransfer_time] => 0.001141 [size_upload] => 12 [size_download] => 6 [speed_download] => 85 [speed_upload] => 170 [download_content_length] => -1 [upload_content_length] => 12 [starttransfer_time] => 0.065204 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => ::1 [primary_port] => 8000 [local_ip] => ::1 [local_port] => 63257 [redirect_url] => [request_header] => POST /messages HTTP/1.1 Host: localhost:8000 Accept: */* Content-type: text/xml Content-length: 12 ) saved!

So I have my 'saved!' message. Great! But, in my database now I have a blank row. Not great. It is still not seeing the body content, just the headers.


回答标准

我在寻找这些问题的答案:

  1. 为什么我不能得到我请求的正文内容?
  2. 如何获取请求的正文内容?

推荐答案

内部控制器注入请求对象.因此,如果您希望访问控制器方法‘foo’内的请求体,请执行以下操作:

public function foo(Request $request){
    $bodyContent = $request->getContent();
}

Php相关问答推荐

如果活动订阅者的购物车中有订阅项目,则在WooCommerce签出中显示通知

如何减少Laravel分页中的链接数

通过注册在Laravel中分配角色

如何将WooCommerce产品属性术语名称显示为链接术语?

首先在WooCommerce我的帐户订单页面中移动操作按钮

在指定的约束内使用随机量填充数据集

fpm-php + nginx + POST数据

使用DOMPDF获取PDF格式的本 map 像

显示 woocommerce 中所选变体的属性术语描述

Font Awesome 加载图标未显示在 WooCommerce 管理员列表中

对产品/库存结果集进行分组和计数,形成多维分层数组

Filament PHP v3:在禁用的表单字段上,ID 不会写入数据库

调用未定义的方法 mysqli::execute_query()

WooCommerce 相关产品(按 children 类别)作为排名数学主要类别的后备

PHP:数组的等边

根据页面的最后修订日期设置 MediaWiki 内部链接的样式

$_SERVER 超全局变量在 PHP 中是否保证可写?

我怎样才能在 .htaccess 中有 2 个参数?

如何将数字文本文件添加到 PHP 数学方程式中?

转发和非转发呼叫 - 后期静态绑定