News Feed
Sections




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

Chris Hartjes' Blog:
Metatesting Understanding Mock Objects
March 28, 2012 @ 08:19:20

In this new post to his blog Chris Hartjes gets into some details about some complex mocking he recently had to do in a project for work. He includes code snippets to illustrate.

With such an extensive array of tests [at work], I have received an education in what it really means to write unit tests for live, production-ready code that really takes unit testing seriously. [...] If you are really writing your unit tests the way you should, each test is focusing on testing one bit of functionality in isolation, which means that you will be heavily relying on mock objects to make things work. I've come up with an example scenario that I hope goes a long way to explaining how to effectively use mock objects.

In his example, he shows how to mock out two objects, "Foo" and "Bar" that are passed into a method in his "Alpha" class. He looks at the code for the "Foo" class and creates a mock object based on its contents - two methods: "getId" and "getDetails". He does the same with the "Bar" class, mocking the "getStartDate" and "getEndDate" methods and showing how to pass those into the "munge" method on the "Alpha" class, complete with return values to match the tests.

For more information on PHPUnit's mock object support, see this page in its manual. You might also explore another popular option, Mockery.

0 comments voice your opinion now!
mock object unittest phpunit class tutorial


blog comments powered by Disqus

Similar Posts

SitePoint PHP Blog: Google App Engine and PHP: Getting Started

Chris Shiflett's Blog: PHP Session Debugging

DevShed: Working with MySQL Result Sets and the Decorator Pattern in PHP

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

NetTuts.com: Form Validation in Symfony 2


Community Events





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


code community laravel framework testing symfony2 unittest install threedevsandamaybe release opinion developer series refactor podcast language introduction api interview list

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