2017 © Pedro Peláez
 

library mokka

Mokka PHP Mocking Framework

image

belanur/mokka

Mokka PHP Mocking Framework

  • Saturday, May 9, 2015
  • by belanur
  • Repository
  • 4 Watchers
  • 4 Stars
  • 28 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Mokka PHP Mocking Framework

Build Status ![Gitter](https://badges.gitter.im/Join Chat.svg), (*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)

Prerequisites

Mokka needs PHP 5.4.0+. PHP 5.5.0+ is recommended., (*4)

Installing with composer

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"
  }
}

Building a Phar

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';

Usage

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(); 

Using Mokka in PHPUnit

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);
  }
}

Code Completion in IntelliJ / PHPStorm

The DynamicReturnTypeValue Plugin provides improved code completion support for methods like Mokka::mock() or Mokka::verify(). A dynamicReturnTypeMeta.json file is included., (*13)

The Versions

09/05 2015

dev-master

9999999-dev

Mokka PHP Mocking Framework

  Sources   Download

Apache-2.0

The Requires

  • php >=5.4.0

 

The Development Requires

mocking mokka stubbing