Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a plugin that makes requests to the Facebook graph API. As I don't want my unit tests to actually make these requests, how would I overcome this? My method calls both wp_remote_get and wp_remote_post. Searching there does seem to be a way to mock functions using runkit, here

I want to avoid having contributers requiring too many dependencies so would like to avoid the above method. Is there any other options? My class extends the WP_UnitTestCase so I'm hoping maybe there's something from the wp unit-tests that I could use?

share|improve this question

4 Answers 4

up vote 5 down vote accepted

If you take a look at WP_HTTP->request() (which all related functions wrap) it provides a filter hook for the purpose of overriding making a request in favor of returning arbitrary data as response:

// Allow plugins to short-circuit the request
$pre = apply_filters( 'pre_http_request', false, $r, $url );
if ( false !== $pre )
    return $pre;
share|improve this answer

Take the results you get from a valid or invalid request, serialize them into strings, then add code that unserializes the string back into the variable instead of doing the request.

share|improve this answer

In order to isolate your code further, I would wrap the wp_remote_get etc calls in an interface with two implementations. One implementation calls wp_remote_get, and the other returns test data.

Using a tool such as runkit in this situation sidesteps the actual problem you have, which is that your code and the APIs are too tightly coupled, and a level of encapsulation and abstraction would be beneficial.

share|improve this answer

In one (two) word(s): Mock Data. PHPUnit got getMock() available exactly for that. As the other answers already have perfectly summoned…

  • where to get valid mock data (Otto) - make sure you occasionally fetch fresh data
  • and where to intercept the data (Rarst)
  • and what tools to use (TomJNowell)

…there only (might be) the issue left that your local SSL certificate verification might fail. WP ships with a filter for that:

add_filter( 'https_local_ssl_verify', '__return_false' );

For any further information about the WP HTTP API, you might want to dig into this answer, this answer, this answer by @Wyck and this answer by @toscho.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.