<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 9, 2013 at 5:49 PM, Ayush Chaudhary <span dir="ltr"><<a href="mailto:ayushchd@gmail.com" target="_blank">ayushchd@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">
<div><span style="color:rgb(160,160,168)">On Tuesday, 9 July 2013 at 5:58 PM, Madhura Jayaratne wrote:</span></div>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div><div dir="ltr"><br><div><br><br><div>On Tue, Jul 9, 2013 at 2:21 PM, Ayush Chaudhary <span dir="ltr"><<a href="mailto:ayushchd@gmail.com" target="_blank">ayushchd@gmail.com</a>></span> wrote:<br>
<blockquote type="cite"><div>
<div>
Hi,
</div><div><br></div><div>I have been writing Unit Tests for classes and libraries under libraries/config/*. For isolation between different libraries, its preferred to mock certain functions. Since some of the code is functional, I tried to convert (for a start) libraries/config/validate.lib.php into a class with static methods.</div>
<div><br></div><div>However, the issue here is that mocking static methods is not really possible with PHPUnit and there are a few workarounds I came across and would like to know which one should we go for, or if there is anything better that can be done.</div>
<div><br></div><div>So, while the writer of PHPUnit specifies how to mock static methods here[0], its only possible if the static calls are to the same class.</div><div><br></div><div>For example, for testing FormDisplay::_validate, I need to mock PMA_config_validate. After I convert validate.lib.php to a class, say, PMA_Validator, I will have to mock PMA_Validator::config_validate, but the above solution won't work here (since PMA_Validator cannot be mocked). A workaround for this could be to pass the mocked static class as mentioned in [0] to the FormDisplay.class.php and make all static calls via that class.</div>
<div><br></div><div>For this, we could have something of this sort in the constructor of FormDisplay:</div><div><br></div><div>if (defined('TESTSUITE'))</div><div><span style="white-space:pre-wrap"> </span>$this->validator = $GLOBALS['dummyValidator'] or $this->validator = <mockedClassPassedInTheArgument></div>
<div>else</div><div><span style="white-space:pre-wrap"> </span>$this->validator = 'PMA_Validator'</div><div><br></div><div>and the calls can be made like:</div><div><br></div><div>$caller = $this->validator; // $this->validator::config_validate won't work</div>
<div>$caller:config_validate();</div><div><br></div><div>Another solution could be what is mentioned in a blog here[1]. The problem with this solution is that the file, FormDisplay.class.php would include and hence declare the PMA_Validator class before our test suite tries to (re)declare it by extending MockProxy class.</div>
<div><br></div><div>In either case, it looks like some sort of modification would be needed to test these classes.</div><div><br></div><div>Please advice.</div><div><br></div><div>[0] <a href="http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html" target="_blank">http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html</a></div>
<div>[1] <a href="http://www.deanspot.org/content/mocking-static-method-calls-phpunit" target="_blank">http://www.deanspot.org/content/mocking-static-method-calls-phpunit</a></div><span><font color="#888888">
<div><div><br></div><div><br></div></div></font></span></div></blockquote><div>As Sebastian Bergmann says, "static methods are death to testability".</div></div>This makes me wonder what disadvantages we would have if we make the methods of PMA_Validator class non-static and use instantiated object of PMA_Validator class to do the validations?<br clear="all">
<div><br></div></div></div></div></div></span></blockquote></div></div><div>I doubt there would be any disadvantages. In any case, from the POV of Unit Testing, we will have to include the instantiated object or the class name, as a member of the class that calls the respective methods (in this example. FormDisplay)</div>
<div><br></div><div>Further, I'd like to know how useful this statement on PHP.net can be in this context:</div><div><br></div><div><span style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:24px;font-size:14px">'A property declared as static can not be accessed with an instantiated class object (<b>though a static method can</b>)'</span></div>
<div><br></div></blockquote><div style>I guess this [1] will help</div><div style><br></div><div style>[1] <a href="http://stackoverflow.com/questions/14331450/a-property-declared-as-static-can-not-be-accessed-with-an-instantiated-class-obj">http://stackoverflow.com/questions/14331450/a-property-declared-as-static-can-not-be-accessed-with-an-instantiated-class-obj</a> </div>
</div><div><br></div>-- <br>Thanks and Regards,<div><br></div><div>Madhura Jayaratne<br><div><br></div></div>
</div></div>