2

在我的单元测试中,我希望存根 php 的内置 file_get_contents() 方法的行为。

有没有办法在 PHP 中存根原生方法(例如 file_get_contents() 或 print_r())等?

4

4 回答 4

3

If by "stub" you mean replace, there is a PHP override_function function; it's part of PECL though. You can also rename them.

于 2010-03-31T13:42:00.777 回答
2

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

于 2010-03-31T13:39:50.680 回答
0

由于PHP-5.3 的命名空间回退策略,您可以在非全局命名空间上下文中覆盖对非限定函数名的调用:

对于函数 […],如果命名空间函数 […] 不存在,PHP 将回退到全局函数 […]。

即,file_get_contents()可以foo通过提供一个foo\file_get_contents().


我最近创建了PHP-Mock库,它为一些非确定性 PHP 函数(如time().

于 2014-11-26T22:59:25.847 回答
0

您不清楚“存根”是什么意思。

您的意思是获取函数的文档,例如:

/**
 * (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) {}

?

于 2010-03-31T13:37:51.563 回答