2017 © Pedro Peláez
 

library mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

image

janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

  • Thursday, May 11, 2017
  • by janmarek
  • Repository
  • 8 Watchers
  • 30 Stars
  • 137,123 Installations
  • PHP
  • 46 Dependents
  • 3 Suggesters
  • 15 Forks
  • 7 Open issues
  • 4 Versions
  • 4 % Grown

The README.md

Installation Build Status

Install via composer:, (*1)

$ composer require --dev janmarek/mockista

It is recommended to create base test class with mockista functionality:, (*2)

<?php
abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
{

    /** @var \Mockista\Registry */
    protected $mockista;

    protected function setUp()
    {
        $this->mockista = new \Mockista\Registry();
    }

    protected function tearDown()
    {
        $this->mockista->assertExpectations();
    }

}

Quick Start

Basic syntax:, (*3)

<?php
class SomeTestCase extends BaseTestCase
{

    private $mock1;

    private $mock2;

    protected function setUp()
    {
        parent::setUp();

        $this->mock1 = $this->mockista->create();
        $this->mock1->expects('method')->andReturn(5);
        $this->mock1->expects('method')->once()->with(1, 2, 3)->andReturn(4);

        // or you can use mock builder with nicer syntax
        $builder = $this->mockista->createBuilder();
        $builder->method()->andReturn(5);
        $builder->method(1, 2, 3)->once->andReturn(4);
        $this->mock2 = $builder->getMock();

        // you can create mock of existing class
        $this->mock3 = $this->mockista->create('ExistingClass', array(
            'abc' => 1,              // you can define return values easily
            'def' => function ($a) {
                return $a * 2;
            }
        ));
    }

    public function testMock1()
    {
        $this->assertEquals(5, $this->mock1->method());
        $this->assertEquals(5, $this->mock1->method('abc'));
        $this->assertEquals(4, $this->mock1->method(1, 2, 3));
    }

    public function testMock2()
    {
        $this->assertEquals(5, $this->mock1->method());
        $this->assertEquals(5, $this->mock1->method('abc'));
        $this->assertEquals(4, $this->mock1->method(1, 2, 3));
    }

    public function testMock3()
    {
        $this->assertEquals(1, $this->mock1->abc());
        $this->assertEquals(4, $this->mock1->def(2));
    }

}

Parameter matching

Parameters can be matched by value:, (*4)

$mock->expects('method')->once()->with(1, 'abc', TRUE)->andReturn(4);
$builder->method(1, 'abc', TRUE)->andReturn(4);

Or you can use smarter parameter matcher:, (*5)

$mock->expects('method')->once()->with(Matchers::isInt(), Matchers::isString(), Matchers::isBool())->andReturn(4);
$builder->method(Matchers::isInt(), Matchers::isString(), Matchers::isBool())->andReturn(4);

Available matchers are:, (*6)

  • Matchers::isBool()
  • Matchers::isNumeric()
  • Matchers::isInt()
  • Matchers::isFloat()
  • Matchers::isString()
  • Matchers::isArray()
  • Matchers::regexp($pattern) - check string parameter by regular expression
  • Matchers::callback($callback) - check parameter by your custom logic passed in a callback

The Versions

11/05 2017

dev-master

9999999-dev https://github.com/janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

  Sources   Download

BSD-3-Clause

The Development Requires

bdd tdd mock phpunit testing tests mocking mockista

26/01 2015

v1.1.0

1.1.0.0 https://github.com/janmarek/mockista

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

  Sources   Download

BSD-3-Clause

The Development Requires

bdd tdd mock phpunit testing tests mocking mockista

14/07 2014

dev-jm-partial-mocks

dev-jm-partial-mocks http://www.mockista.com/

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

  Sources   Download

BSD-3-Clause

The Development Requires

bdd tdd mock phpunit testing tests mocking mockista

22/01 2013

v1.0.0

1.0.0.0 http://www.mockista.com/

Mockista is library for mocking, which I've written, because I find mocking in PHPUnit awful.

  Sources   Download

BSD-3-Clause

bdd tdd mock phpunit testing tests mocking mockista