In January, Miguel Romero wrote a great article showing how to get started with Guzzle. If you missed the article, or are unfamiliar with Guzzle:
Guzzle is a PHP HTTP client & framework for building RESTful web service clients.
In Miguel’s article, he covered a range of the library’s features, including basic configuration for GET and POST requests, plugins, such as logging using monolog, and finished up with interacting with an OAuth service, specifically GitHub’s API.
In this tutorial, I want to show you how to use Guzzle from a different perspective, specifically how to do unit testing with it. To do this, we’re going to look at three specific approaches:
- Hand Crafting Custom Responses
- Using a ServiceClient with Mock Response Files
- Enqueueing a Server with Mock Responses
Getting Set Up
Like all things, we need to walk before we can run. In this case, we need to set up our test environment and test class. To get everything in place, we’ll be using Composer. If you’re not familiar with Composer, please read Alexander’s article here on SitePoint before continuing.
Our composer.json file will look as follows:
{
"require": {
"php": ">=5.3.3",
"phpunit/phpunit": "4.0.*",
"guzzle/guzzle": "~3.7"
}
}
I’ve stipulated a minimum PHP version of 5.3.3. To be fair, it likely should be higher, but this is a good start. Our only other requirements are PHPUnit and Guzzle. After adding these to composer.json, in your project run composer install and after a short wait, the dependencies will be ready to go.
Continue reading %Unit Testing with GuzzlePHP%
more
{ 0 comments... » Unit Testing with GuzzlePHP read them below or add one }
Post a Comment