PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

The Test Adapter

Sometimes, it is very hard to test code that relies on HTTP connections. For example, testing an application that pulls an RSS feed from a remote server will require a network connection, which is not always available.

For this reason, the Zend_Http_Client_Adapter_Test adapter is provided. You can write your application to use Zend_Http_Client, and just for testing purposes, for example in your unit testing suite, you can replace the default adapter with a Test adapter (a mock object), allowing you to run tests without actually performing server connections.

The Zend_Http_Client_Adapter_Test adapter provides an additional method, setResponse() method. This method takes one parameter, which represents an HTTP response as either text or a Zend_Http_Response object. Once set, your Test adapter will always return this response, without even performing an actual HTTP request.

Example 494. Testing Against a Single HTTP Response Stub

<?php
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    
'adapter' => $adapter
));

// Set the expected response
$adapter->setResponse(
    
"HTTP/1.1 200 OK"        "\r\n" .
    
"Content-type: text/xml" "\r\n" .
                               
"\r\n" .
    
'<?xml version="1.0" encoding="UTF-8"?>' .
    
'<rss version="2.0" ' .
    
'     xmlns:content="http://purl.org/rss/1.0/modules/content/"' .
    
'     xmlns:wfw="http://wellformedweb.org/CommentAPI/"' .
    
'     xmlns:dc="http://purl.org/dc/elements/1.1/">' .
    
'  <channel>' .
    
'    <title>Premature Optimization</title>' .
    
// and so on...
    
'</rss>');

$response $client->request('GET');
// .. continue parsing $response..

The above example shows how you can preset your HTTP client to return the response you need. Then, you can continue testing your own code, without being dependent on a network connection, the server's response, etc. In this case, the test would continue to check how the application parses the XML in the response body.

Sometimes, a single method call to an object can result in that object performing multiple HTTP transactions. In this case, it's not possible to use setResponse() alone because there's no opportunity to set the next response(s) your program might need before returning to the caller.

Example 495. Testing Against Multiple HTTP Response Stubs

<?php
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    
'adapter' => $adapter
));

// Set the first expected response
$adapter->setResponse(
    
"HTTP/1.1 302 Found"      "\r\n" .
    
"Location: /"             "\r\n" .
    
"Content-Type: text/html" "\r\n" .
                                
"\r\n" .
    
'<html>' .
    
'  <head><title>Moved</title></head>' .
    
'  <body><p>This page has moved.</p></body>' .
    
'</html>');

// Set the next successive response
$adapter->addResponse(
    
"HTTP/1.1 200 OK"         "\r\n" .
    
"Content-Type: text/html" "\r\n" .
                                
"\r\n" .
    
'<html>' .
    
'  <head><title>My Pet Store Home Page</title></head>' .
    
'  <body><p>...</p></body>' .
    
'</html>');

// inject the http client object ($client) into your object
// being tested and then test your object's behavior below

The setResponse() method clears any responses in the Zend_Http_Client_Adapter_Test's buffer and sets the first response that will be returned. The addResponse() method will add successive responses.

The responses will be replayed in the order that they were added. If more requests are made than the number of responses stored, the responses will cycle again in order.

In the example above, the adapter is configured to test your object's behavior when it encounters a 302 redirect. Depending on your application, following a redirect may or may not be desired behavior. In our example, we expect that the redirect will be followed and we configure the test adapter to help us test this. The initial 302 response is set up with the setResponse() method and the 200 response to be returned next is added with the addResponse() method. After configuring the test adapter, inject the HTTP client containing the adapter into your object under test and test its behavior.

If you need the adapter to fail on demand you can use setNextRequestWillFail($flag). The method will cause the next call to connect() to throw an Zend_Http_Client_Adapter_Exception exception. This can be useful when your application caches content from an external site (in case the site goes down) and you want to test this feature.

Example 496. Forcing the adapter to fail

<?php
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client('http://www.example.com', array(
    
'adapter' => $adapter
));

// Force the next request to fail with an exception
$adapter->setNextRequestWillFail(true);

try {
    
// This call will result in a Zend_Http_Client_Adapter_Exception
    
$client->request();
} catch (
Zend_Http_Client_Adapter_Exception $e) {
    
// ...
}

// Further requests will work as expected until
// you call setNextRequestWillFail(true) again

Zend Framework