recently on a blog devoted to testing on Google in one of the posts Fri Defeat "Static Cling" read an interesting suggestion about testing static methods in Java. As in Java and PHP can not create a static method Mock authors suggest creating a special post type calling static methods which would be encapsulated. I admit that, while in Java a compiled language such a solution is most acceptable in an interpreted PHP always try to avoid a proliferation of new classes and types. Using PHPUnit can easily create objects whose implementation is replaced by the test code only required by us and, besides, the whole class behaves just like the original.
Suppose that we have a sample Person class in which we find static method declarations:
class Person {public static function
find ($ id) {
/ / Code that gets such a database
return $ person;}
the code in other parts of the code can be used, for example, like this:
NotFoundException class extends Exception {} class
PersonsController {public
show_person ( $ id) {
$ p = Person:: find ($ id);
if ($ p) {throw new NotFoundException
();}
/ / some operations on the variable $ p
return $ p;
}}
now wanting to test the controller code will certainly want to be able to this static method pay at the request of different values \u200b\u200bor throw exceptions as such (for example, if we can not find the object with the given ID.) Unfortunately, not easy to get a Person object Mock.
simple solution is to close the static calls Person:: find () from the controller code:
class
PersonsController {public function find ($ id) {return
Person:: find ($ id);}
public
show_person ( $ id) {
$ p = $ this-> find ($ id);
if ($ p) {throw new NotFoundException
();}
//...
return $ p;
}}
Now the test code using PHPUnit3 might look like this
testShouldThrowAnExceptionIfPersonNotFound public function () {$ controller
= $ this-> getMock ('PersonsController', array ('find'));
$ controller-> expects ($ this-> any ())
-> method ('find')
-> will ($ this-> returnValue (false)) / / simulate situations in which the find is not the object
try
{$ controller-> find (1) / / search for a nonexistent object, to which the controller should react to a specific exception
$ this-> assertFail ('Exception not thrown ');
} catch (NotFoundException $ e) {return
;
} catch (Exception $ e) {
$ this-> assertFail (' Invalid Exception thrown ');}
}
Since functions in PHP have the static bind (this user functions as well as those of the modules and the core of the language for example, all functions working on arrays) and such calls should be enclosed in a non-static object methods and thereby gain the opportunity to exchange implementations for testing a variety of situations.
0 comments:
Post a Comment