2010-03-31 57 views
2

在我的单元测试中,我正在寻找存根的内置file_get_contents()方法的行为。存根PHP本地方法

有没有办法在PHP中存储本地方法(如file_get_contents()或print_r())等?

+1

这个问题可能会帮助你http://stackoverflow.com/questions/2326835/redefine-built-in-php-functions – 2010-03-31 13:43:09

+0

是的。重复。 – Gordon 2010-03-31 13:54:30

回答

0

你不清楚'stub'是什么意思。

你的意思是让文档的功能,如:

/** 
* (PHP 5 &gt;= 5.1.0)<br/> 
* Sets the default timezone used by all date/time functions in a script 
* @link http://php.net/manual/en/function.date-default-timezone-set.php 
* @param timezone_identifier string <p> 
* The timezone identifier, like UTC or 
* Europe/Lisbon. The list of valid identifiers is 
* available in the . 
* </p> 
* @return bool This function returns false if the 
* timezone_identifier isn't valid, or true 
* otherwise. 
*/ 
function date_default_timezone_set ($timezone_identifier) {} 

+2

'stub'是一种方法的替身。您通常在UnitTests中使用它:http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.mocking-the-filesystem,它是一个技术术语:http:// en。 wikipedia.org/wiki/Method_stub – Gordon 2010-03-31 13:53:02

+0

不明确的道歉;是的,我指的是方法存根的维基百科定义,即一个方法的替代方法,我期待在我的单元测试中使用它。 – DrPep 2010-03-31 13:58:27

2

有可能与runkit。使用runkit_function_copy和runkit_function_redefine函数来复制和重新定义函数。您应该将runkit.internal_override ini-setting设置为1以修改内部函数。

0

由于PHP-5.3's namespace fallback policy您可以在非全局命名空间范围内覆盖到不合格的函数名称调用:

对于函数[...],PHP将回落至全局函数[...]如果一个命名空间功能[...]不存在。

I.e.例如调用非限定函数名称file_get_contents()。命名空间foo可以通过提供foo\file_get_contents()来覆盖。


我最近创建的库PHP-Mock提供嘲笑像time()一些非确定性的PHP函数。