{"id":16173399,"url":"https://github.com/jdgrimes/wp-http-testcase","last_synced_at":"2025-03-19T00:30:49.537Z","repository":{"id":19544619,"uuid":"22792868","full_name":"JDGrimes/wp-http-testcase","owner":"JDGrimes","description":"PHPUnit testcase for testing code that uses WordPress's WP_HTTP class","archived":false,"fork":false,"pushed_at":"2019-01-16T16:16:42.000Z","size":26,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T01:11:55.639Z","etag":null,"topics":["phpunit","phpunit-testcase","testing","wordpress"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JDGrimes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-09T18:51:17.000Z","updated_at":"2024-08-07T19:07:00.000Z","dependencies_parsed_at":"2022-08-21T12:11:00.831Z","dependency_job_id":null,"html_url":"https://github.com/JDGrimes/wp-http-testcase","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDGrimes%2Fwp-http-testcase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDGrimes%2Fwp-http-testcase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDGrimes%2Fwp-http-testcase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JDGrimes%2Fwp-http-testcase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JDGrimes","download_url":"https://codeload.github.com/JDGrimes/wp-http-testcase/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244331657,"owners_count":20435971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["phpunit","phpunit-testcase","testing","wordpress"],"created_at":"2024-10-10T04:08:36.979Z","updated_at":"2025-03-19T00:30:49.232Z","avatar_url":"https://github.com/JDGrimes.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"WP HTTP Testcase\n================\n\nPHPUnit testcase for testing code that uses WordPress's `WP_Http` class.\n\nIf you use `wp_remote_request()` or other wrappers for `WP_Http` methods in your\ncode, this makes it difficult to test, especially if the remote server may not be\nreachable from your testing environment. This testcase solves this by letting you\nroute your requests to a different host address, use a cached set of\nresponses, or just mock the remote responses by supplying artificial ones.\n\n# Installation\n\nYou can install this package using composer:\n\n```bash\ncomposer require --dev jdgrimes/wp-http-testcase\n```\n\n# Usage\n\nTo use it in your code, you need to first include the `wp-http-testcase.php` file in\nyour PHPUnit bootstrap file. (Or, to take advantage of Composer's autoloading, you \ncan just include `vendor/autoload.php`.)\n\nThen, in your tests that involve `WP_Http`, you need to extend `WP_HTTP_TestCase`\ninstead of `WP_UnitTestCase` as you normally would.\n\n## Mocking Responses\n\n### Using Response Caching\n\nThe best way of testing, when possible, it to set up a mock host to handle the\nrequests. In some cases, you may want or need to actually send the requests through\nto the real server, and that can be done as well. Which of these you do will depend\non the nature of the requests, and what side-effects they produce on the recipient\nhost.\n\n#### Setting Up a Test Host\n\nFor example, if you are testing a plugin that makes requests to an API provided\nby another plugin or other software, you probably don't want or need to test this on\na live site. Instead, you can set up a test site, or use a local server that is part\nof your development environment. There you can install the software that handles\nthe requests. Once this is done, you can run your tests against that test site like\nthis:\n\n```bash\nWP_HTTP_TC_HOST=localhost phpunit\n```\n\nJust replace `localhost` with the hostname of the local server. Note that the\n`WP_HTTP_TC_*` flags can be defined as PHP constants, or as bash environment\nvariables as above. The latter will take precedence.\n\n#### Enabling Caching\n\nOf course this will be much slower than most other unit tests, because the requests\nare bound to take a bit of time. That is where caching comes in. When caching is\nenabled, the response to each request is cached the first time it is run, and the\ncached version is used in the future. This means that your tests can remain lightning\nfast.\n\nTo enable caching, just add this to your bootstrap:\n\n```php\ndefine( 'WP_HTTP_TC_USE_CACHING', true );\n```\n\nYou'll probably also want to specify the directory to save the cache in, via\n`WP_HTTP_TC_CACHE_DIR`. You can utilize multiple cache groups and switch between them\nusing `WP_HTTP_TC_CACHE_GROUP`.\n\n#### Using the Live Host\n\nThere is the second case though, where you are unable to set up a test server. An\nexample where this would be the case would be if your plugin makes requests to the\nAPI provided by GitHub. Depending on the situation, it may be feasible to actually\nmake the requests to the \"live\" recipient. The main issue again is that the requests\nwill make the tests take a long time to complete. There is also the possibility that\nthe API isn't always accessible from your testing environment, or that your tests\nwill end up pounding the API too hard and you'll get blocked. This is where caching\ncan help you. You only need to run your tests against the \"live\" API once in a while,\nand the rest of the time you can test using the cached responses.\n\n### Supplying Artificial Responses\n\nOf course, there may be times when it isn't possible to create a test server, and it\nisn't feasible to run against the live server either. In this case, you may want to\nhard-code artificial responses into your tests. Here is how you can do that:\n\nBefore calling the code that will invoke the HTTP request, you need to set the\nfunction to mock the responses like so:\n\n```php\n$this-\u003ehttp_responder = array( $this, 'mock_server_response' );\n```\n\nThe HTTP responder function will be passed two arguments, the request arguments and\nthe URL the request was intended for.\n\n```php\nprotected function mock_server_response( $request, $url ) {\n\n\treturn array( 'body' =\u003e 'Test response.' );\n}\n```\n\nFor a full list of the `$request` and response arguments, see\n[`WP_Http::request()`](http://developer.wordpress.org/reference/classes/wp_http/request/#source-code)\n\n## Testing Requests\n\nYou may also wish to test that your code is making requests as expected. You can do\nthis by checking the value of `$this-\u003ehttp_requests`, which is an array of requests.\nEach entry in the array stores the request arguments (`'request'` key) and URL\n(`'url'` key).\n\nTo check that a request was made, you could do something like this:\n\n```php\n$this-\u003eassertCount( 1, $this-\u003ehttp_requests );\n$this-\u003eassertEquals( 'http://example.com/', $this-\u003ehttp_requests[0]['url'] );\n```\n\nWhen you just want to test the request and don't care about the response, you can\nshort-circuit the request before it is made, by setting the response mocker to be\n`__return_true()`:\n\n```php\n$this-\u003ehttp_responder = '__return_true';\n\nmy_prefix_make_request();\n\n$this-\u003eassertCount( 1, $this-\u003ehttp_requests );\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdgrimes%2Fwp-http-testcase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdgrimes%2Fwp-http-testcase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdgrimes%2Fwp-http-testcase/lists"}