guzzle/guzzle — pull request #307 by thewilkybarkid (2013-04-29 18:40:24+00:00)
Title: [WIP] MockManager plugin

As discussed at https://groups.google.com/forum/?fromgroups#!topic/guzzle/LzEOQSai7Hg here's my initial idea for mocking an entire web service (as opposed to individual requests). This is useful for more 'serious' testing, since you don't have to rely on requests being made once in a certain order and it actually blocks network access (so you can't accidentally send a request, which could be dangerous). It's also required for functionally testing Symfony apps (for example) where you don't have access to queue individual requests. This is far from complete, but the basic idea is here.

The `MockManagerPlugin` stores implementations of `RequestHandlerInterface`, which convert requests into responses. When a request is made it cycles through the handlers to find one that can handle the request; if one isn't found an `UnhandledRequestException` is thrown.

The only implementation (so far at least) is `UriMatchingRequestHandler`, which takes an array of URIs (actually regexs, so you can do partial matching) and responses. This should probably come with a few different implementations, but the user can write their own, and since you have access to the request object you could do all kinds of things (eg header/query string matching).

For example:

```php
$mockManager = new \Guzzle\Plugin\MockManager\MockManagerPlugin();

$handler = new \Guzzle\Plugin\MockManager\RequestHandler\UriMatchingRequestHandler('GET', array(
'index.php$' => 'HTTP/1.1 200 OK',
'http://example.com/second.php' => 'HTTP/1.1 204 No Content',
));
$mockManager->addHandler($handler);

$client = new \Guzzle\Http\Client();
$client->addSubscriber($mockManager);

echo $client->createRequest('GET', 'http://example.com/index.php')->send(); // returns the 200
echo $client->createRequest('GET', 'http://example.com/second.php')->send(); // returns the 204
echo $client->createRequest('GET', 'http://example.com/broken.php')->send(); // isn't handled, so an exception is thrown
```