Download the PHP package jcid/phpunit-mock-extension without Composer
On this page you can find all versions of the php package jcid/phpunit-mock-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jcid/phpunit-mock-extension
More information about jcid/phpunit-mock-extension
Files in jcid/phpunit-mock-extension
Package phpunit-mock-extension
Short Description Extensions for PHPUnit Mock
License MIT
Informations about the package phpunit-mock-extension
PHP Unit Mock Extension
An PHP extension to allow multiple calls to a method with different counts and different arguments.
Installation
The problem
Currently if you want to do multiple method calls to a PHP Unit mock object with different argument and return statements you can to use the $this->at($index)
method. The problem with this method is that you cannot check if the method is getting called to much for instance a third time like in the example below. This is the case because PHP Unit does not use the arguments to select the matcher.
An other solution is to use the logic or method's to define the arguments. But I personally don't like this method because it decouples the arguments from the return values and you have no control over the sequence or combination of arguments and returns.
But there is a third solution provided by PHP Unit the $this->returnValueMap(*array*)
method. This method is a bit strange because the last argument of the array is the return value. But it couples the arguments and the return values and that's nice. You can now also check the amount of times the method is called with the right combination.
The second problem with the $this->returnValueMap(*array*)
method is that the arguments use a strict comparison check and if you use a value holder like we do this will fail.
Our solution
We created this library which uses a custom MockObject Matcher and custom MockObject to decide how and how much times we should return a specific value. The idea is based on the idea of $this->at($index)
but has al the features we want.
We use a custom builder to create the sequence of calls to one particular method and then define the expects
with our custom matcher created by the builder and the use the return stub provided by it.
Inspiration
This library is inspired by etsy/phpunit-extensions