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">
> 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>
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"></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 ?<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>