Download the PHP package facile-it/moka without Composer
On this page you can find all versions of the php package facile-it/moka. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download facile-it/moka
More information about facile-it/moka
Files in facile-it/moka
Package moka
Short Description Shorthand for creating mock objects
License MIT
Homepage http://github.com/facile-it/moka
Informations about the package moka
Moka - Shorthand for Creating Mock Objects
Tired of spending most of your testing time mocking objects like there's no tomorrow? Yes.
Moka provides you with two simple methods to reduce your effort on such a tedious task, and with an incredible abstraction layer between the most popular mock engines and you.
Installation
You can install the package via composer:
Usage
To use Moka in your tests simply use
function Moka\Plugin\PHPUnit\moka()
(see generators section below) and run Moka::clean()
before every test. A simple interface will let you create moka (mock) objects and decorate them with stub methods and properties via a fluent interface:
Alternatively, instead of using moka()
, you can call Moka::phpunit(string $fqcnOrAlias, string $alias = null): ProxyInterface
.
Being such a simple project, Moka can be integrated in an already existing test suite with no effort.
Notice: if you are extending PHPUnit TestCase
, to simplify the cleaning phase we provide a MokaCleanerTrait
which automatically runs Moka::clean()
after each test.
You can rely on the original mock object implementation to be accessible (in the example below, PHPUnit's - for Prophecy see below):
Reference
moka(string $fqcnOrAlias, string $alias = null): ProxyInterface
Creates a proxy containing a mock object (according to the selected strategy) for the provided FQCN and optionally assigns an $alias
to it to be able to get it later:
The $alias
allows you to store mock instances:
ProxyInterface::stub(array $namesWithValues): ProxyInterface
Accepts an array of method or property stubs with format [$name => $value]
, where $name
must be a string and $value
can be of any type, including another mock object.
Caution:
- Properties are identified by symbol
$
prepended to their names - An exception instance set as a method value will be thrown when the method is called
Notice: method stubs are valid for any invocation of the defined methods and cannot be overridden.
If you need more granular control over invocation strategies, you can get access to the original mock object implementation.
Supported mock object generators
Currently we ship Moka with built-in support for PHPUnit mock objects.
We support other generators as well, but you need to install the relevant packages to make them work:
We provide a specific moka()
function for each supported strategy, as well as a static method (self documented in the function itself):
Moka\Plugin\PHPUnit\moka
Moka\Plugin\Prophecy\moka
Moka\Plugin\Mockery\moka
Moka\Plugin\Phake\moka
Prophecy native behavior
Prophecy lets you stub methods by calling them directly on the ObjectProphecy
. Moka doesn't support such a behavior, but we provide an easy workaround:
Warning: this workaround cannot be used with methods having the same name as a previously stubbed property:
Plugin development
If you feel a genius and want to create your own mock generator (or add support for an existing one), just implement Moka\Plugin\PluginInterface
and the relative Moka\Strategy\MockingStrategyInterface
:
Extend AbstractMockingStrategy
for an easier (and stricter) implementation of your strategy:
Warning: your plugin FQCN must match the template Moka\Plugin\YourOwn\YourOwnPlugin
, where YourOwn
is the name of the plugin.
Both your plugin and your strategy must pass our test cases (please install phpunit/phpunit to run them):
MokaPluginTestCase
MokaMockingStrategyTestCase
Let us know of any Moka-related development!
Testing
We highly suggest using Paraunit for a faster execution of tests:
Credits
- Angelo Giuffredi
- Alberto Villa
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of moka with dependencies
phpcollection/phpcollection Version ^0.5
phpunit/phpunit Version ^7.0
psr/container Version ^1.0