Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Unit Testing with GuzzlePHP
May 16, 2014 @ 16:56:46

The SitePoint PHP site has a new tutorial posted today showing how, if you're using the Guzzle PHP HTTP client, to work with it in your unit testing. He focuses on PHPUnit testing, but the concepts could be applied in other testing tools as well.

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 and enqueueing a Server with mock responses.

Matthew helps you get started by installing Guzzle and PHPUnit via Composer, including the "composer.json" contents you'll need. He shows you how to get PHPUnit all set up and how to create a first simple test that extends the "GuzzleTestCase" class. He then gets into the custom hand-crafted responses, showing how to push the contents of a text file into the client response. Next up he shows how to use the ServiceClient to create the same setup only simpler. Finally, he looks at the queue handling (via a small node.js background server) where the client passes requests and is returned the same mock body content. Code examples of both the requests and the PHPUnit tests are included through out the tutorial.

tagged: unittest guzzlephp http client tutorial phpunit

Link: http://www.sitepoint.com/unit-testing-guzzlephp


Trending Topics: