News Feed
Sections




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

Zumba Engineering Blog:
Mocking Singleton PHP classes with PHPUnit
November 26, 2012 @ 09:51:04

On the Zumba Engineering blog today Chris Taylor has a new post about mocking in PHPUnit, specifically how to handle those pesky Singleton methods lurking around your codebase.

In many of our projects, utilities and vendor classes are implemented with a singleton pattern. [...] In this post, we'll cover a nice way to inject a PHPUnit mock object for use in testing methods that utilize singleton classes.

He starts by introducing mocking and how to use mock classes in PHPUnit with a simple "sayHello" example. Adding on another layer, he creates a "SomeclassMock" class, defining its own "expects" and "cleanup" methods. This class forces the Singleton method to act more like a regular non-static method and "resets" it after each use.

0 comments voice your opinion now!
mocking phpunit class singleton expects cleanup tutorial


blog comments powered by Disqus

Similar Posts

php|architect: Two New ART Articles (CakePHP and More Flex)

Sebastian Bergmann's Blog: RC2 Is The New RC1 (PHPUnit)

NetTuts.com: Building With the Twitter API: Tweet Storms

Josh Adell's Blog: Phar Flung Phing

Padraic Brady's Blog: Unit Testing: One Test, One Assertion - Why It Works


Community Events

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


php7 composer programming language yii2 introduction community framework podcast example api part2 project application opinion list series symfony laravel interview

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