Mimic classes for functional testing
PHP
Permalink
Failed to load latest commit information.
.idea
src
tests Move tests to separate folder; PHP 7 Compatibility Jan 4, 2016
.gitignore
Doxyfile
LICENSE
README.md
build.xml
composer.json
composer.lock Composer update; Use Pbweb coding standard when building Feb 20, 2017
phpunit.xml.dist

README.md

Mimic

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.

Installation

Install with composer

Install mimic using composer:

composer require pbweb/mimic

Example

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'

Usage

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

$mimic->enableQueue();

enableQueue will enable the use of the queue.

disableQueue

$mimic->disableQueue();

disableQueue will stop the use of the queue.

isQueueEnabled

$isQueueEnabled = $mimic->isQueueEnabled();

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

enqueue

$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.

getQueueContent

$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

$isFinished = $mimic->isFinished();

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

clearQueue

$mimic->clearQueue();

clearQueue will remove all the remaining actions from the queue.