I'm trying to write a unit test for a controller using Zend and PHPUnit

我正在尝试使用Zend和PHPUnit为控制器编写单元测试

In the code I get data from php://input

在代码中我从php:// input获取数据

$req = new Zend_Controller_Request_Http();
$data = $req->getRawBody();

My code works fine when I test the real application, but unless I can supply data as a raw http post, $data will always be blank. The getRawBody() method basically calls file_get_contents('php://input'), but how do I override this in order to supply the test data to my application.

当我测试真实应用程序时,我的代码工作正常,但除非我可以提供数据作为原始http帖子,否则$ data将始终为空。 getRawBody()方法基本上调用file_get_contents('php:// input'),但是如何覆盖它以便将测试数据提供给我的应用程序。

4 个解决方案

#1


3

Provided the $req->getRawBody() is, as you say, the same as file_get_contents('php://input')...

假设$ req-> getRawBody()就像你说的那样,与file_get_contents('php:// input')相同......

$test = true; /* Set to TRUE when using Unit Tests */

$req = new Zend_Controller_Request_Http();
if( $test )
  $data = file_get_contents( 'testfile.txt' );
else
  $data = $req->getRawBody();

Not a perfect solution, but similar to what I have used in the past when designing scripts to handle piped emails with great success.

这不是一个完美的解决方案,但类似于我过去在设计脚本以处理管道电子邮件时取得的巨大成功。

更多相关文章

  1. 架构比MVC更适合Web应用程序?
  2. mysql主从同步报slave_sql_running:no的解决方案
  3. 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最
  4. win7下Django的MySql安装,问题解决方案
  5. 有没有办法确定是由Web应用程序(php进程)执行数据库行更新,还是通过
  6. mysql 事物没提交导致事物一直运行解决方案
  7. 无法连接远程MySQL数据库的解决方案
  8. [征集] MySQL交叉表解决方案及散分
  9. 如何在产品和类别应用程序树中将1个表连接到(2个不同的表作为一个

随机推荐

  1. Android实现游戏摇杆的源代码
  2. Android (SQLite 数据库与ContentProvide
  3. Android NDK相关的库方法
  4. Android shape使用和属性含义
  5. EditText的错误信息
  6. 【Android学习笔记】Android概述与开发环
  7. Android KitKat 4.4平台开发-添加USB ADB
  8. 如何使用Android SDK开发Android应用?
  9. Android NDK开发技巧二
  10. Android中文合集 最终版