From d75ba254351b05a2b8d82c0b9c21e4c57785fcbe Mon Sep 17 00:00:00 2001 From: JDGrimes Date: Sat, 9 Aug 2014 15:42:01 -0400 Subject: [PATCH] Add information about testing requests --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 376f0dc..4d2721a 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ PHPUnit testcase for testing code that uses WordPress's `WP_Http` class. # Usage +## Mocking Responses + If you use `wp_remote_request()` or other wrappers for `WP_Http` methods in your code, this makes it difficult to test, especially if the remote server may not be reachable from your testing environment. This testcase solves this by letting you @@ -36,3 +38,29 @@ protected function mock_server_response( $request, $url ) { For a full list of the `$request` and response arguments, see [`WP_Http::request()`](http://developer.wordpress.org/reference/classes/wp_http/ request/#source-code) + +## Testing Requests + +You may also wish to test that your code is making requests as expected. You can do +this by checking the value of `$this->http_requests`, which is an array of requests. +Each entry in the array stores the request arguments (`'request'` key) and URL +(`'url'` key). + +To check that a request was made, you could do something like this: + +```php +$this->assertCount( 1, $this->http_requests ); +$this->assertEquals( 'http://example.com/', $this->http_requests[0]['url'] ); +``` + +When you just want to test the request and don't care about the response, you can +short-circuit the request before it is made, by setting the response mocker to be +`__return_true()`: + +```php +$this->http_responder = '__return_true'; + +my_prefix_make_request(); + +$this->assertCount( 1, $this->http_requests ); +```