In order to make the test run faster and more reliable, it is more convenient to write the test, people usually use mock instead of directly using real external dependencies.
Recently, Facebook wrote a new PHP mock tool that looks clean and tidy.
When using other PHP mock frameworksCodeMore statements are required, and the test is too dependent on the specific implementation details.
For example, when we use phpunit for mock, but simply return some values, the Code may be as follows:
$ User = $ this-> getmock ('user')-> expects ($ this-> Any ()-> method ('getid ') -> will ($ this-> returnvalue (1234 );
When fbmock is used, the Code is as follows:
$ User = mock ('user')-> mockreturn ('getid', 1234 );
In fact, fbmock is not a mock framework in the true sense, because it does not use the expected value check as in the above Code. Therefore, it can only be regarded as a pile with the spy function. In fact, it is better to use this Expected Value Check in the mock framework, because it is too restrictive. In the above fbmock example, it does not matter how many times GETID () is called.
Of course, it is also important to check which method has been called. In this case, you can simply rely on assertion in phpunit.
$ Logger = mock ('logger '); // run code that uses $ logger // make sure 'data' was logged $ this-> assertcalledonce ($ logger, 'log ', array ('data '));
Currently, fbmock supportsZend PHP 5.4 + andHiphop VM.