News Feed
Sections




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

DZone.com:
Self-Initializing Fakes in PHP
December 05, 2011 @ 10:03:53

In a new post over on DZone.com Giorgio Sironi shows how to create a fake, an interface that mocks the interface to an external tool and provides cached results back to the calling script.

We can specialize our Fake in a Self-Initializing Fake, which will provide an alternative implementation with the aid of the real one. In our Google Maps case, the Fake will use the real web service for its first response, and maintain an internal cache. This mechanism provides insurance about out-of-sync responses, and lets you enjoy the speed of unit tests after the initial warmup: if you always use the same data, no duplicate requests will be made to the external resource.

He includes example code for creating a test (with PHPUnit) that loads in the information from the Google Maps web service and caches it into a private variable inside the Fake. His test fails the first time checking for a difference in the time between fetching the real version and the cached version (fails the first time, but passes after the cache is implemented).

0 comments voice your opinion now!
selfinitializing fake double test unittest phpunit


blog comments powered by Disqus

Similar Posts

Change Garcia's Blog: expected exceptions annotations, mocked object calls, oh my.

Robert Basic's Blog: Helping out with Zend Framework 2

SitePoint.com: Be More Asssertive: Getting to Know PHPUnit's Assertions

Emran Hasan's Blog: Using Mockery as a mocking framework with PHPUnit

NetTuts.com: Refactoring Legacy Code: Part 4 - Our First Unit Tests


Community Events





Don't see your event here?
Let us know!


project bugfix framework api list language introduction podcast interview series library community application configure release threedevsandamaybe developer code wordpress laravel

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework