mock-plugin.rst 870 B

123456789101112131415161718192021222324252627
  1. ===========
  2. Mock plugin
  3. ===========
  4. The mock plugin is useful for testing Guzzle clients. The mock plugin allows you to queue an array of responses that
  5. will satisfy requests sent from a client by consuming the request queue in FIFO order.
  6. .. code-block:: php
  7. use Guzzle\Http\Client;
  8. use Guzzle\Plugin\Mock\MockPlugin;
  9. use Guzzle\Http\Message\Response;
  10. $client = new Client('http://www.test.com/');
  11. $mock = new MockPlugin();
  12. $mock->addResponse(new Response(200))
  13. ->addResponse(new Response(404));
  14. // Add the mock plugin to the client object
  15. $client->addSubscriber($mock);
  16. // The following request will receive a 200 response from the plugin
  17. $client->get('http://www.example.com/')->send();
  18. // The following request will receive a 404 response from the plugin
  19. $client->get('http://www.test.com/')->send();