Brad
Brad

Reputation: 163478

Programmatically disable specific PHP functions for testing

I have a function that makes HTTP requests with cURL that falls back to file_get_contents() if cURL is not available on the system.

I would like to write unit tests for this function (utilizing PHPUnit) where cURL is available for some tests, and not available for others.

Is it possible to programmatically disable PHP functions, such as curl_init()?

I know I can use the disable_functions setting in php.ini, but I was hoping to find a way to do unit tests without reconfiguring PHP in between runs.

Upvotes: 5

Views: 1164

Answers (1)

Ry-
Ry-

Reputation: 225115

You can use runkit_function_remove to remove any defined function, I think:

runkit_function_remove('curl_init');

And, as per the documentation:

Note: By default, only userspace functions may be removed, renamed, or modified. In order to override internal functions, you must enable the runkit.internal_override setting in php.ini.

Upvotes: 4

Related Questions