[Phpmyadmin-devel] Mocking static methods - Unit tests

Ayush Chaudhary ayushchd at gmail.com
Tue Jul 9 10:51:42 CEST 2013


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>
$this->validator = 'PMA_Validator'

and the calls can be made like:

$caller = $this->validator; // $this->validator::config_validate won't work

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

Ayush Chaudhary

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.phpmyadmin.net/pipermail/developers/attachments/20130709/86c929ec/attachment.html>

More information about the Developers mailing list