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 Your Privates
February 09, 2010 @ 11:48:42

In a new post to his blog today Sebastian Bergmann looks at a way you can test your privates (no, not that) - private methods in your classes with PHPUnit. The key lies in the Reflection API.

One question I get over and over again when talking about Unit Testing is this: "How do I test the private attributes and methods of my objects?" [...] PHP 5.3.2 introduces the ReflectionMethod::setAccessible() method to allow the invocation of protected and private methods through the Reflection API.

This method lets you, at runtime, change the access level on the method away from private or protected and down to public so the contents can be executed normally. Though he warns one thing about doing it this way - just because you can, doesn't mean it's a good thing. You application is meant to be tested in a certain way and you should probably stick to that.

0 comments voice your opinion now!
test private method tutorial reflection


blog comments powered by Disqus

Similar Posts

Bnnx.com: Installing the Zend Optimizer

NETTUTS.com: How To Build a Widget to Display your Buzzing

Sameer Borate's Blog: Read the version of a PDF in PHP

Sudheer Satyanarayana's Blog: XML RPC Client Using PHP PEAR - A Real World Example: Ping Technorati

PHPBuilder.com: Beginning PHP: One Goal at a Time


Community Events





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


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

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