Mimic classes for functional testing

2.3 2020-06-11 08:10 UTC

This package is auto-updated.

Last update: 2020-10-17 07:29:22 UTC


This library can be used to create mimics of your classes, which can be used for (functional) testing.

A mimicked class is similar to mock objects with stub methods in phpunit, but on a functional level.


Install with composer

Install mimic using composer:

composer require pbweb/mimic


Lets say you have a client class which talks to an external REST service. The interface might look like this:

interface RestClient
    public function get($something);
    public function put($something);

Now, using a class which really connects to the REST server in a (functional) test is a bad idea, since that server may not be in the scope of your test and influence the results.

To create a mimic client you need to extend MimicActionHandler like this:

class MimicRestClient extends MimicActionHandler implements RestClient
    public function get($something)
        return $this->handleAction(__FUNCTION__, func_get_args());
    public function put($something)
        return $this->handleAction(__FUNCTION__, func_get_args());

Now you can mimic the behaviour of the RestClient in your tests:

// In your dependency injection container:
$mimicClient = new MimicRestClient();

// In your test setup:
$mimicClient->enqueue('get', ['cheese'], 'cheese result');

// In your test or in a class which you are testing:
$result = $mimicClient->get('cheese'); // returns 'cheese result'

Argument Matchers

In case you want more control over the arguments that are expected for a call, you can use argument matchers.

The ArgumentMatchers class instantiates several matchers which can be passed to the expected argument list of enqueue.

// Matches any call to update that has 1 as its first argument and any value as its second argument.
$mimicClient->enqueue('update', [1, ArgumentMatchers::any(), 'result');


Extending MimicActionHandler will allow your class to have the mimic enqueue system. Every method you want to mimic should have this as the body:

return $this->handleAction(__FUNCTION__, func_get_args());

See tests/Service/SampleMimic for an example.



enableQueue will enable the use of the queue.



disableQueue will stop the use of the queue.


$isQueueEnabled = $mimic->isQueueEnabled();

isQueueEnabled will return a boolean value with the state of the queue.


$mimic->enqueue($method, array $argumentList = [], $response = null, $throw = false);

enqueue will add a method call to the expected queue. You can add as many method prediction as you like. If the next call to the mimic is the expected call then the given response will be returned and it will be removed from the queue. If the next call to the mimic is not the same as the expected call added to the queue then an exception will be thrown.

If you set throw to true then the response will be thrown instead of returned.


$actionList = $mimic->getQueueContent();

getQueueContent will return all the remaining actions added to the queue as Action models. See the Action class for more information about the model.


$isFinished = $mimic->isFinished();

isFinished will return true if there are no more action left in the queue. false otherwise.



clearQueue will remove all the remaining actions from the queue.