Re: Re: [PHPUnit] testing protected functions

From: Date: Sun, 23 Apr 2006 19:32:02 +0000
Subject: Re: Re: [PHPUnit] testing protected functions
References: 1 2 3 4  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-23812@lists.php.net to get a copy of this message
Hi Ron, I can follow your argumentations and protected methods are part of the public interface of (public) classes - at least IMHO. It would be different, if you would declare the classes with a pseudo visibility of "package restricted". If you really want to test/have the necessity of testing these methods, why do you not add to the tests something along the lines of e.g.: <?php // do the imports ... class MyClass { protected function doTestMe() {
       echo 'You got me!';
} } ?> <?php // do the imports ... class MockMyClass extends MyClass { public function doTestMe() {
       parent::doTestMe();
} } class MyClassTest extends PHPUnit_Framework_TestCase { // have fun and don't forget to instantiate MockMyClass instead of MyClass ... } ?> -- Stefano Ron Rademaker wrote:
The main reason I want to test my protected method is because of inheritance. I always write each class while bearing in mind that someday I, or somebody else, will make a subclass of that class. When I extend these classes I very often reuse and override a pool of small protected functions. So in a way, all protected functions provide a public API to its descendants and as such I wish to test them. Ron Paul Scott wrote:
On Fri, 2006-04-07 at 11:40 +0200, Sebastian Bergmann wrote:
Ron Rademaker schrieb:
Is it somehow possible to test protected functions with PHPUnit2?
      
No.
LOL, simple answer to a simple question? Ron, when doing unit tests, half the point is that you only test your public functions. The way that I do it, is to write the tests first, and when the pass, ship the code. Basically you should have a bunch of tests for your public API written, running constantly on a cron. You then start writing the public functions and get them to use private and protected functions as they *need* to. Once your public API passes all tests, it works, so you ship it. Who cares how your private/protected methods work? as long as they work! I would recommend reading up a bit on TDD then asking this question again... --Paul


« previous php.pear.general (#23812) next »