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