dev-master
9999999-devMokka PHP Mocking Framework
Apache-2.0
The Requires
- php >=5.4.0
The Development Requires
mocking mokka stubbing
Mokka PHP Mocking Framework
, (*1)
The goal of Mokka is to provide a lightweight framework for creating mocks and stubs. It's syntax is heavily inspired by the Mockito Framework., (*2)
Documentation can be found on readthedocs.org., (*3)
Mokka needs PHP 5.4.0+. PHP 5.5.0+ is recommended., (*4)
Simply add belanur/mokka to the composer.json
of your project. Since there are no stable versions yet, you'll have to use "dev-master":, (*5)
{ "require-dev": { "belanur/mokka": "dev-master" } }
Note: Make sure to have phar.readonly = Off
in your php.ini. Otherwise building Phars is not possible., (*6)
You can run php build/buildPhar.php
to build a Phar package. It will be put in /build/mokka.phar
. You can then include it in your projects:, (*7)
<?php require __DIR__ . '/mokka.phar';
After adding Mokka to your project, you can start creating Mocks right away:, (*8)
<?php // Create a mock of any class or interface $foo = Mokka::mock(\Acme\Foo::class); // requires PHP 5.5 $bar = Mokka::mock(\Acme\BarInterface::class); // If you are on PHP 5.4, just use the class name (and consider upgrading your PHP version) $foo = Mokka::mock('\Acme\Foo'); // By default, all methods of the mocked class will return NULL $foo->getBar(); // => NULL
You can stub methods with when()
and thenReturn()
:, (*9)
<?php Mokka::when($mock)->getBar('baz')->thenReturn('foobar'); // This will still return NULL, because the stub is set for the argument 'baz' only $foo->getBar('foo'); // => NULL $foo->getBar('baz'); // => 'foobar'
You can verify if and how often a method was called with verify()
:, (*10)
<?php // The mock will throw a VerificationException if this method was not called once Mokka::verify($foo)->getBar(); // The mock will throw a VerificationException if this method was not called three times Mokka::verify($foo, 3)->getBar(); // The mock will throw a VerificationException if this method was called Mokka::verify($foo, Mokka::never())->getBar(); // The mock will throw a VerificationException if this method was not called at least two times Mokka::verify($foo, Mokka::atLeast(2))->getBar();
Since Mokka's methods can be called statically (e.g. Mokka::mock(\Acme\Foo::class)
), you can just start using Mokka in PHPUnit:, (*11)
<?php class FooTest extends PHPUnit_Framework_TestCase { public function testFoo() { $mockedBar = Mokka::mock(\Acme\Bar::class); $foo = new \Acme\Foo($mockedBar); } }
However it is recommended to use the MokkaTestCase
class, which acts as a proxy:, (*12)
<?php class FooTest extends MokkaTestCase { public function testFoo() { $mockedBar = $this->mock(\Acme\Bar::class); $foo = new \Acme\Foo($mockedBar); } }
The DynamicReturnTypeValue Plugin provides improved code completion support for methods like Mokka::mock()
or Mokka::verify()
. A dynamicReturnTypeMeta.json
file is included., (*13)
Mokka PHP Mocking Framework
Apache-2.0
mocking mokka stubbing