News Feed
Sections




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

Sebastian Bergmann's Blog:
Testing Code That Uses Singletons
February 12, 2010 @ 11:28:45

Sebastian Bergmann has written up a post for his blog recently that looks at unit testing singletons, something that's been rumored as being some of the hardest to test.

I frequently quote Miško Hevery with "It is hard to test code that uses singletons." And then my audience asks me... Why is it hard to test code that uses singletons? Lets have a look at the default implementation of the Singleton design pattern in PHP.

He defines a singleton first, showing how it's used to create single instances of objects and, because of this, cannot be tested easily because there's no way to know you're getting a clean instance every time. Dependency injection can help with the problem by allowing you to pass in a "mock" instance of the singleton class each time. He also mentions some variations on singletons that could make it easier to test in the future.

0 comments voice your opinion now!
singleton unittest technique dependency injection


blog comments powered by Disqus

Similar Posts

Charles Sprayberry's Blog: DI and global state

PHPMaster.com: Testing Error Conditions with PHPUnit

Fabien Potencier's Blog: Create your own framework... on top of the Symfony2 Components (part 12)

NetTuts.com: Better Workflow in PHP With Composer, Namespacing, and PHPUnit

SecWatch.org: CJ Tag Board Multiple Parameter Handling PHP Code Injection Vulnerabilities


Community Events

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


podcast configure performance laravel example php7 opinion introduction install interview framework series community application conference library release api language symfony2

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