views:

178

answers:

3

In my unit test, i'm looking to stub the behaviour of php's inbuilt file_get_contents() method.

Is there a way to stub native methods in PHP (such as file_get_contents() or print_r()) and the likes?

+1  A: 

You aren't clear about what 'stub' means.

Do you mean getting doc for functions, like:

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

?

Coronatus
A `stub` is a stand-in for a method. You usually use it in UnitTests: http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.mocking-the-filesystem and is a technical term: http://en.wikipedia.org/wiki/Method_stub
Gordon
Apologies for the lack of clarity; yes, I refer to the wikipedia definition of a method stub i.e. a standin for a method, which i'm looking to use in my unit test.
DrPep
A: 

i've heard this should be possible with runkit.

stereofrog
A: 

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

Ricket