Coder Social home page Coder Social logo

mockista's Introduction

Installation Build Status

Install via composer:

$ composer require --dev janmarek/mockista

It is recommended to create base test class with mockista functionality:

<?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:

<?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:

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

Or you can use smarter parameter matcher:

$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:

  • 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.