testing.rst 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. =======
  2. Testing
  3. =======
  4. RingPHP tests client handlers using `PHPUnit <https://phpunit.de/>`_ and a
  5. built-in node.js web server.
  6. Running Tests
  7. -------------
  8. First, install the dependencies using `Composer <https://getcomposer.org>`_.
  9. composer.phar install
  10. Next, run the unit tests using ``Make``.
  11. make test
  12. The tests are also run on Travis-CI on each commit: https://travis-ci.org/guzzle/guzzle-ring
  13. Test Server
  14. -----------
  15. Testing client handlers usually involves actually sending HTTP requests.
  16. RingPHP provides a node.js web server that returns canned responses and
  17. keep a list of the requests that have been received. The server can then
  18. be queried to get a list of the requests that were sent by the client so that
  19. you can ensure that the client serialized and transferred requests as intended.
  20. The server keeps a list of queued responses and returns responses that are
  21. popped off of the queue as HTTP requests are received. When there are not
  22. more responses to serve, the server returns a 500 error response.
  23. The test server uses the ``GuzzleHttp\Tests\Ring\Client\Server`` class to
  24. control the server.
  25. .. code-block:: php
  26. use GuzzleHttp\Ring\Client\StreamHandler;
  27. use GuzzleHttp\Tests\Ring\Client\Server;
  28. // First return a 200 followed by a 404 response.
  29. Server::enqueue([
  30. ['status' => 200],
  31. ['status' => 404]
  32. ]);
  33. $handler = new StreamHandler();
  34. $response = $handler([
  35. 'http_method' => 'GET',
  36. 'headers' => ['host' => [Server::$host]],
  37. 'uri' => '/'
  38. ]);
  39. assert(200 == $response['status']);
  40. $response = $handler([
  41. 'http_method' => 'HEAD',
  42. 'headers' => ['host' => [Server::$host]],
  43. 'uri' => '/'
  44. ]);
  45. assert(404 == $response['status']);
  46. After requests have been sent, you can get a list of the requests as they
  47. were sent over the wire to ensure they were sent correctly.
  48. .. code-block:: php
  49. $received = Server::received();
  50. assert('GET' == $received[0]['http_method']);
  51. assert('HEAD' == $received[1]['http_method']);