[Phpmyadmin-devel] Mocking static methods - Unit tests
madhura.cj at gmail.com
Tue Jul 16 02:46:16 CEST 2013
On Tue, Jul 9, 2013 at 5:43 PM, Madhura Jayaratne <madhura.cj at gmail.com>wrote:
> On Tue, Jul 9, 2013 at 2:21 PM, Ayush Chaudhary <ayushchd at gmail.com>wrote:
>> 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.
>> 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.
>> So, while the writer of PHPUnit specifies how to mock static methods
>> here, its only possible if the static calls are to the same class.
>> 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  to the FormDisplay.class.php and make all static calls via that
>> For this, we could have something of this sort in the constructor of
>> if (defined('TESTSUITE'))
>> $this->validator = $GLOBALS['dummyValidator'] or $this->validator =
>> $this->validator = 'PMA_Validator'
>> and the calls can be made like:
>> $caller = $this->validator; // $this->validator::config_validate won't
>> Another solution could be what is mentioned in a blog here. 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.
>> In either case, it looks like some sort of modification would be needed
>> to test these classes.
>> Please advice.
>>  http://www.deanspot.org/content/mocking-static-method-calls-phpunit
>> As Sebastian Bergmann says, "static methods are death to testability".
> 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?
> Hi all,
Any objection to this, otherwise Ayush can go ahead with this approach?
Thanks and Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Developers