PHPUnit: March 2008 Archives

Yes! Sebastian Bergmann did it: just one day after requesting a colorful output of PHPUnit's command line interface, he committed a patch to PHPUnit's trunk, which should be released with the next release. Thank you!

feature-request-completed.jpg

Update

Sebastian seems to like "the feature", too - he just blogged about it. Horray!

phpunit-output-success.jpg phpunit-output-failure.jpg

A few days ago I came across Josh Carters blog. I saw a very nice suggestion have your unit tests output the results in green or red bar - depending if the tests passed or failed.

unit_test_green_red_bar.jpg

I really like the idea and will be requesting the inclusion of ansi coloring to Sebastian Bergmann - I'd really like to see this feature being added to PHPUnit.

While it's absolutly correct that direct access to private properties is strictly forbidden in PHP, it's quite disturbing that even reflection cannot do it when you're writing Unit Tests. Sometimes you just want to test whether a private property contains the correct data. With the normal visibility rules you cannot access those from your Unit Tests.

Luckily, as I just read at Tobias Schlitt's Blog, Derick Rethans committed a patch to the PHP 5.3 repository, which allows you to get things done quite easily. You still need to explicitly state that you want to access the value of a protected/private property through reflection by the new method setAccessible(). It's a good thing to avoid people doing stupid things accidentally, but you finally get the access to it.

PHP 5.3 you'll be able to access private properties like this:

But until we'll see a stable PHP 5.3, there we're many working "hacks", which worked for some versions of PHP 5.x - the most current one would be this:

This should get you up and running for PHP 5.2.5 ("older hacks" won't work).

So how could you use it?

I hope those lines of code will help you until PHP 5.3 arrives, as it helps our developers.

Update

As Sebastian points out in his comment to this post, PHPUnit has had support for testing private and protected attributes for quite a while. It can be used via:

I should be reading the documentation much more thoroughly next time (even though I must admit, that none of our developers has known this method, too).

Recent Activity

Monday

  • I tweeted, "Just published a new blog post: How to make UILabel / UITableViewCell to have a dynamic height ... http://tinyurl.com/8s55nb"
Wednesday

  • I tweeted, "Flat is prepared for the party ..."
Tuesday

  • I tweeted, "Good morning Twitterverse!"

More ...

Conferences

Conferences I presented at:

Programming Blogs - BlogCatalog Blog Directory
Creative Commons License
This weblog is licensed under a Creative Commons License. blogoscoop

About this Archive

This page is a archive of entries in the PHPUnit category from March 2008.

PHPUnit: August 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.