PhpUnit::How can be __construct with protected variables tested?

(not always we should add public method getVal()- soo without add method that return protected variable value)

Example:

  class Example{
    protected $_val=null;
    function __construct($val){
      $this->_val=md5 ($val);
    }
   }

Edit:

also exist problem to test in function that return void


Edit2:

Example why we need test __construct:

class Example{
        protected $_val=null;
       //user write _constract instead __construct
        function _constract($val){
          $this->_val=md5 ($val);
        }

       function getLen($value){
         return strlen($value);
       }
 }

 class ExampleTest extends PHPUnit_Framework_TestCase{
     test_getLen(){
       $ob=new Example();//call to __construct and not to _constract
        $this->assertEquals( $ob->getLen('1234'), 4);
     }
 }

test run ok, but Example class "constructor" wasn't created!

Thanks

share|improve this question

feedback

2 Answers

up vote 3 down vote accepted

The main goal of unit testing is to test interface By default, you should test only public methods and their behaviour. If it's ok, then your class is OK for external using. But sometimes you need to test protected/private members - then you can use Reflection and setAccessible() method

share|improve this answer
1. also protected method should tested 2.__construct is public method – Yosef Feb 8 '11 at 13:56
Also you can read article from author of PHPUnit – Distdev Feb 8 '11 at 14:01
Thanks, good solution - I will read the article – Yosef Feb 8 '11 at 14:21
feedback

Create a derived class that exposes the value that you want to test.

share|improve this answer
this option should exist in phpunit if not this should be added. – Yosef Feb 8 '11 at 13:57
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.