问题
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.).
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.)
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
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
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;
Finally, you need to add a php.ini entry to activate it. See http://php.net/manual/en/apd.installation.php
回答1:
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)
回答2:
Have a look at these:
bool rename_function ( string $original_name , string $new_name )
bool override_function ( string $function_name , string $function_args , string $function_code )
回答3:
Change fsockopen
to fopen
to do mock, and don't change any other functions.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
to
$fp = fopen("/path/to/your/dummy_data_file");
回答4:
I blogged about my experiences which contains full working code showing how to use override_function to achieve the desired goal; both file reading and socket reading. I won't repeat that whole article here, but will just point out how you have to use the functions:
- Use
rename_function
to give a name to the old, original function, so we can find it later. - Use
override_function
to define the new behaviour - Use
rename_function
to give a dummy name to__overridden__
Step 1 is critical if you want to be able to restore the original behaviour afterwards.
In the Food For Thought section at the end I show an alternative approach that I think is more robust, and therefore I think it is safer for replacing file functions when using phpUnit. It creates a permanent function replacement who's default behaviour is to forward to the built-in function. It then checks parameters (the resource $handle
in this case) to decide if it is being called on a stream we want different behaviour for. (I think you could call this an example of the Chain Of Responsibility design pattern.)
来源:https://stackoverflow.com/questions/11280506/how-to-mock-built-in-php-socket-functions