[Phpmyadmin-devel] Mocking static methods - Unit tests

Dieter Adriaenssens dieter.adriaenssens at gmail.com
Tue Jul 16 09:58:37 CEST 2013

2013/7/16 Madhura Jayaratne <madhura.cj at gmail.com>:
> 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:
>>> Hi,
>>> 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[0], 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
>>> [0] to the FormDisplay.class.php and make all static calls via that class.
>>> For this, we could have something of this sort in the constructor of
>>> FormDisplay:
>>> if (defined('TESTSUITE'))
>>> $this->validator = $GLOBALS['dummyValidator'] or $this->validator =
>>> <mockedClassPassedInTheArgument>
>>> else
>>> $this->validator = 'PMA_Validator'
>>> and the calls can be made like:
>>> $caller = $this->validator; // $this->validator::config_validate won't
>>> work
>>> $caller:config_validate();
>>> 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.
>>> In either case, it looks like some sort of modification would be needed
>>> to test these classes.
>>> Please advice.
>>> [0]
>>> http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
>>> [1] 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?

No objections.

Kind regards,

Dieter Adriaenssens

More information about the Developers mailing list