<br><br><div class="gmail_quote">On Sat, Jul 7, 2012 at 8:00 PM, Chanaka Dharmarathna <span dir="ltr"><<a href="mailto:pe.chanaka.ck@gmail.com" target="_blank">pe.chanaka.ck@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Dieter,<br><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><div>
> I almost reduce the use of super global variables inside functions of<br>
> PMA_DisplayResults class.<br>
> Most of the fields are set while executing the getTable() function since<br>
> they cannot set while executing the constructor.<br>
><br>
> Now I'm move into writing tests.<br>
> I am facing to a problem with writing tests.<br>
> How can I access or set the private variables, by a test ?<br>
> The already existed tests also failing due to this change.<br>
> Your suggestions will very helpful.<br>
<br>
</div><br>
<br></div><div><div class="h5">
I didn't have a look at the current tests and at PMA_DisplayResults<br>
class, so I won't be able to go into specifics about your tests, but I<br>
will try to answer your questions, and point you in a direction.<br>
<br>
Accessing or setting private variables in a class/method is not<br>
possible during a unit test.<br>
The idea of unit tests is to test the functionality of a class method<br>
or global function by testing the output of it, or catching errors.<br>
You expect a certain behaviour of the method/function depending on the<br>
value of the input parameters (or environmental states, like global<br>
variables, database content, or config settings).<br>
So basically you think of all different situations that can affect the<br>
behaviour of the method/function, define what kind of result this<br>
would give and write a test that sets up the situation and test<br>
(assert) the output of the function/method.<br>
<br>
When writing unit tests, you should keep in mind that you are only<br>
testing the behaviour of that function/method. Any functions/methods<br>
used by a function/method should be tested seperately. By which I mean<br>
that you must assume when testing a function/method that the<br>
functions/methods that are used in that method, work correctly.<br>
To make things easier for you, you can define stubs/mocks [0] that<br>
replace the methods/functions used in a function/method you are<br>
testing. This way you don't have to set up a complete environment, but<br>
can concentrate on what the function/method does.<br>
<br>
As an example<br>
<br>
If you have a class that gets values from a database :<br>
<br>
class DbValues<br>
{<br>
   private $_values;<br>
<br>
   __construct()<br>
   {<br>
       $this->_values = array();<br>
   }<br>
<br>
   public function getValuesFromDb()<br>
   {<br>
       // functionality making a connection to a database, executing a<br>
query and setting the resulting values to $_values.<br>
   }<br>
<br>
   public function getValues()<br>
   {<br>
       return $this->_values;<br>
   }<br>
<br>
  public function count()<br>
  {<br>
     return count($this->getValues());<br>
  }<br>
}<br>
<br>
<br>
And you want to test the behaviour of the count() method. The only way<br>
to populate the _values variable in this example would be by setting<br>
up a database with different sets of values, for each case you want to<br>
test and call the getValuesFromDb() method, OR, you can  define a stub<br>
that replaces the getValues() method and let it return an array with<br>
the necessary amount of values (depending on what you want to test).<br>
This way you avoid having to set up a complicated testing environment,<br>
when testing a single class method. (Note : when testing the<br>
getValuesFromDb() method you will probably have to set up a database,<br>
but that's because that method is all about connecting to a database<br>
and getting data from it, but all other methods in this class can<br>
assume that the getValuesFromDb works, so there is no need to<br>
replicate the complicated test environment every time)<br>
<br>
I suggest you read up a bit on testing class methods and the use of<br>
stubs/mocks. I hope my example didn't confuse you too much.<br>
<br>
[0] <a href="http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs" target="_blank">http://www.phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs</a><br>
<br clear="all"></div></div></blockquote></div><br>Thank you very much for spending your time for clarify this with valuable and enough information.<br>I completely agree with you, on writing unit tests independent/separate from another layer/function.<br>

Mock objects/stubs are helpful in that case.<br><br>At the moment there are around 25 private fields in PMA_DisplayResults class.<br>What I am thinking of implement getter setter methods for all those and use getters and setters where needed.<br>

In that way I will be able to set those private fields even from the tests.<br>What do you think on this ?<div class="HOEnZb"><div class="h5"><br><br>Regards !<br>-- <br>____________________________________<br><br>Chanaka Indrajith<br>
Bsc.Computer Engineering Undergraduate<br>
Faculty of Engineering<br>University of Peradeniya<br>Sri Lanka<br>____________________________________<br><br>
</div></div><br>------------------------------------------------------------------------------<br>
Live Security Virtual Conference<br>
Exclusive live event will cover all the ways today's security and<br>
threat landscape has changed and how IT managers can respond. Discussions<br>
will include endpoint security, mobile security and the latest in malware<br>
threats. <a href="http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/" target="_blank">http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/</a><br>_______________________________________________<br>
Phpmyadmin-devel mailing list<br>
<a href="mailto:Phpmyadmin-devel@lists.sourceforge.net">Phpmyadmin-devel@lists.sourceforge.net</a><br>
<a href="https://lists.sourceforge.net/lists/listinfo/phpmyadmin-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/phpmyadmin-devel</a><br>
<br></blockquote></div><br>Hi Chanaka,<div><br></div><div>When writing test cases we might have to test protected/private functions as well. The method used in PMA is by using php Refection class. They used setAccessible method to make them public for test class. There is a method in reflection class called getProperties which can used for access protected and private attributes. Refer this [1] for more details. But I am not sure is it a good approach when testing. </div>
<div><br></div><div>[1] - <a href="http://php.net/manual/en/reflectionproperty.setaccessible.php">http://php.net/manual/en/reflectionproperty.setaccessible.php</a> </div><div><div><br></div><div>Regards,</div>-- <br><span style="font-family:arial,sans-serif;font-size:13.2px;border-collapse:collapse;color:rgb(136,136,136)">Yasitha Pandithawatta <br>
Undergraduate<br>Computer Science and Engineering Department<br>University of Moratuwa<br>Sri Lanka</span><br>
</div>