PHPackages                             krakjoe/mimus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. krakjoe/mimus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

krakjoe/mimus
=============

464022[2 PRs](https://github.com/krakjoe/mimus/pulls)PHP

Since Jan 29Pushed 3y ago3 watchersCompare

[ Source](https://github.com/krakjoe/mimus)[ Packagist](https://packagist.org/packages/krakjoe/mimus)[ RSS](/packages/krakjoe-mimus/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (3)Used By (0)

mimus
=====

[](#mimus)

[![Build Status](https://camo.githubusercontent.com/c9980f1e7277908b78b9c129c87bea10f3a2867524012c8a81a9c7cbd91b8ff7/68747470733a2f2f7472617669732d63692e6f72672f6b72616b6a6f652f6d696d75732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/krakjoe/mimus)[![Coverage Status](https://camo.githubusercontent.com/fa2e24c0492d933a761313d3a0484649a3156d70fb1d019e35c1e738e23a290f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6b72616b6a6f652f6d696d75732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/krakjoe/mimus?branch=master)

Requirements
============

[](#requirements)

- PHP 7.2+
- [Componere](https://github.com/krakjoe/componere)

Doubles
=======

[](#doubles)

A test double is an object that takes the place of an object of a formal type while a system is under test:

```

```

At this time,`$object` is `instanceof Foo` with the same interface as declaration `Foo`, but none of it's methods do anything - they have been stubbed.

It's important to note that while mimus supports a familiar pattern (`getInstance`) to allow injecting dependencies, **mimus has replaced the declaration of `Foo` internally**, so that subsequent calls to `new Foo` will create a test double, making the code above and the following code functionally equivalent:

```

```

Given that `Foo` was already declared when `double::class` was called, this behaviour is impossible to achieve in userland PHP alone: This is the reason that mimus must depend on Componere, and is one of the main differences between mimus and any other mocking framwork for PHP.

Stubs
=====

[](#stubs)

To make the stubs do something, you must tell `mimus` what the method should, or will do:

```

```

In some cases, our method needs to return a different value for different input:

```

```

At this time, we have defined two valid paths through the method based on the arguments given at runtime, should the method be invoked like this:

```

```

While the first call will succeed, the second will raise `\mimus\Exception: return value expected to be bool(false), got bool(true)`.

Execute Different Implementation
--------------------------------

[](#execute-different-implementation)

Suppose we want to execute a different implementation in place of the original:

```

```

While the first call will invoke the original implementation, the second will invoke the given implementation.

Exceptions
----------

[](#exceptions)

Suppose we want to verify that a Path throws an exception:

```

```

While the first call will succeed and the resulting exception caught, the second will raise (uncaught): `mimus\Exception: expected exception of type Exception, nothing thrown`.

Limits
------

[](#limits)

Suppose we want to limit the number of times a method is entered:

```

```

While the first call will succeed, the second will raise: `mimus\Exception: limit of 1 exceeded`.

Partial Mocks
=============

[](#partial-mocks)

Partial mocks are used, for example, to allow an object of a mocked type to execute an interface as implemented:

```

```

*Note that `use` does not imply that the double should be partialized.*

Life Cycle of a Double
======================

[](#life-cycle-of-a-double)

The named constructors `Double::class` and `Double::make` will try to return a cached double based on the `$name` passed to the constructor, they may optionally `$reset` the double as they retrieve it.

From the first call to `Double::getInstance` or `Double::rule` the class exists in the engine with exactly the `$name` given; Certain actions such as implementing interfaces and using traits are no longer possible and must be performed previously to these calls taking place.

The class remains present until it is explicitly removed with `Double::unlink`: When a double is removed any class which it replaced is restored to it's original implementation.

API
===

[](#api)

```
