如何模拟内置的PHP套接字功能? [英] How to mock built-in php socket functions?

查看:71
本文介绍了如何模拟内置的PHP套接字功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些从套接字读取的代码,当它获得某个较大的输入时,它将出错.在修复它之前,我为此添加了一个单元测试,但由于无法模拟fread(以及我正在使用的其他PHP内置函数,例如fsockopenfeof等)而陷入困境.

I'm working on some code that reads from a socket, and it goes wrong when it gets a certain large input. I went to add a unit test for this, before fixing it, but got stuck because I cannot mock fread (and the other PHP built-in functions I'm using such as fsockopen, feof, etc.).

简单来说,我的问题是这段代码因"致命错误:无法重新声明fgets()... "而失败:

In simple terms my problem is that this code fails with "Fatal error: Cannot redeclare fgets() ...":

function fgets($fp){
return "xxx";
}

我意识到我可以创建我的实际代码使用的套接字包装类,然后可以为该包装类创建一个模拟对象.但这就是尾巴摇着狗",我可以想到的原因是,除了事物的原理之外,这是一个坏主意. (例如,使代码更复杂,更高效,必须重构尚未测试的代码.)

I realize I could create a socket wrapper class, that my real code uses, and then I could create a mock object for that wrapper class. But that is The Tail Wagging The Dog, and I can think of reasons it is a bad idea, beyond just the principle of the thing. (E.g. Making code more complex, efficiency, having to refactor code not under test yet.)

所以,我的问题是,如何在单元测试中用我自己的内置fgets()替代实现? (或者,如果您想在框外思考,可以将问题表述为:当$socket是对fsockopen的调用的返回值时,如何控制对fgets($socket)的调用返回的字符串? )

So, my question is how can I replace the built-in fgets() implementation with my own, within a unit test? (Or, if you want to think outside the box, the question can be phrased as: how can I control the string that a call to fgets($socket) returns, when $socket is the return value from a call to fsockopen?)

ASIDE

按照正确答案的要求安装apd是一项艰苦的工作;它的最新发布时间是2004年,不支持开箱即用的php 5.3.没有适用于它的Ubuntu软件包,并且pecl install apd也失败了.因此,这里是安装它的过程(这些是针对ubuntu 10.04的)(全部以root身份完成):

Installing apd, as required by the correct answer, is hard work; it was last released in 2004, and does not support php 5.3 out of the box. No Ubuntu package for it and also pecl install apd failed. So here are the procedures to install it (these are for ubuntu 10.04) (all done as root):

pecl download apd
tar xzf apd-1.0.1.tgz
cd apd-1.0.1
phpize
./configure
# Do edits (see below)
make install

请参见 https://bugs.php.net/bug.php?id=58798 获取所需的补丁.注意实际上,只有一行需要更改,因此您可以手动进行操作,如下所示:在文本编辑器中打开php_apd.c,转到967行,并用该行替换CG(extended_info) = 1行:

See https://bugs.php.net/bug.php?id=58798 for the patch you need to do. NB. there is only one line you really have to change, so you can do it by hand, as follows: open php_apd.c in a text editor, go to line 967, and replace the CG(extended_info) = 1 line with this one:

CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;

最后,您需要添加一个php.ini条目以将其激活.参见 http://php.net/manual/zh/apd.installation.php

Finally, you need to add a php.ini entry to activate it. See http://php.net/manual/en/apd.installation.php

推荐答案

如果您无权访问APD或Runkit但正在使用名称空间,请尝试以下操作:https://stackoverflow.com/a/5337635/664108 (链接中的答案指的是time(),但没有区别)

If you don't have access to APD or Runkit but are using namespaces, try this: https://stackoverflow.com/a/5337635/664108 (Answer in link refers to time() but it makes no difference)

这篇关于如何模拟内置的PHP套接字功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆