All Projects → danrevah → Shortify Punit

danrevah / Shortify Punit

Licence: mit
🔨 PHP Mocking Framework, includes method chaining stubbing.

Labels

Projects that are alternatives of or similar to Shortify Punit

Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+16003.33%)
Mutual labels:  mock
Vue Admin Webapp
this is a admin project
Stars: ✭ 673 (+2143.33%)
Mutual labels:  mock
Pytest Responsemock
Simplified requests calls mocking for pytest
Stars: ✭ 24 (-20%)
Mutual labels:  mock
Mmock
Mmock is an HTTP mocking application for testing and fast prototyping
Stars: ✭ 537 (+1690%)
Mutual labels:  mock
Android Testing Guide
[Examples] Complete reference for Android Testing with examples.
Stars: ✭ 652 (+2073.33%)
Mutual labels:  mock
Swiftymocky
Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
Stars: ✭ 748 (+2393.33%)
Mutual labels:  mock
Easy Mock Cli
Create api.js for Easy-Mock. https://easy-mock.github.io/easy-mock-cli/
Stars: ✭ 506 (+1586.67%)
Mutual labels:  mock
Reactspa
combination of react teconology stack
Stars: ✭ 911 (+2936.67%)
Mutual labels:  mock
Lyrebird
移动应用插件化测试工作台
Stars: ✭ 663 (+2110%)
Mutual labels:  mock
Hel
Hel rules over Helheim, where the souls unworthy of Valhalla reside. It's also a mock generator for Go.
Stars: ✭ 16 (-46.67%)
Mutual labels:  mock
Fes.js
Fes.js 是一套优秀的中后台前端解决方案。提供初始项目、开发调试、Mock接口、编译打包的命令行工具。内置布局、权限、数据字典、状态管理、存储、Api等多个模块。以约定、配置化、组件化的设计思想,让用户仅仅关心用组件搭建页面内容。基于Vue.js,上手简单。经过多个项目中打磨,趋于稳定。
Stars: ✭ 579 (+1830%)
Mutual labels:  mock
Retrofitcache
RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。
Stars: ✭ 647 (+2056.67%)
Mutual labels:  mock
Nei Toolkit
NEI 接口文档管理平台配套自动化工具
Stars: ✭ 781 (+2503.33%)
Mutual labels:  mock
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (+1683.33%)
Mutual labels:  mock
Offit
Simple but powerful API mocking library. Make mocks great again.
Stars: ✭ 25 (-16.67%)
Mutual labels:  mock
Swiftmockgeneratorforxcode
An Xcode extension (plugin) to generate Swift test doubles automatically.
Stars: ✭ 522 (+1640%)
Mutual labels:  mock
Bypass
Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.
Stars: ✭ 731 (+2336.67%)
Mutual labels:  mock
Peeplus
python+vue3前后端分离项目
Stars: ✭ 28 (-6.67%)
Mutual labels:  mock
Iustudiomock
mock server for front end
Stars: ✭ 8 (-73.33%)
Mutual labels:  mock
Dyson
Node server for dynamic, fake JSON.
Stars: ✭ 814 (+2613.33%)
Mutual labels:  mock

ShortifyPunit  

Build Status Coverage Status Code Quality Latest Stable Version

PHP Mocking Framework, inspired by Mockito library for Java

Table of contents

Installation

PHP Version >= 5.4 is required!

The following instructions outline installation using Composer. If you don't have Composer, you can download it from http://getcomposer.org/

$ composer require "danrevah/shortifypunit":"dev-master" 
$ php composer.phar require "danrevah/shortifypunit":"dev-master"

Mocking Examples

// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Returns NULL, was not stubbed yet
$mock->first_method();

Basic mocking example, if a function wasn't stubbed the return value will always be NULL.

Stubbing

// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Stubbing first_method() function without arguments
ShortifyPunit::when($mock)->first_method()->returns(1);
echo $mock->first_method(); // prints '1'

// Stubbing first_method() function with arguments
ShortifyPunit::when($mock)->first_method(1,2)->returns(2);
echo $mock->first_method(); // still prints '1'
echo $mock->first_method(1,2); // prints '2'

// Stubbing callback
ShortifyPunit::when($mock)->first_method()->callback(function() { echo 'Foo Bar'; });
echo $mock->first_method(); // prints 'Foo Bar'

// Stubbing throws exception
ShortifyPunit::when($mock)->second_method()->throws(new Exception());
$mock->second_method(); // throws Exception

The when function is used to stubbing methods with specific parameters, following a throws, returns or a callback action.

Methods:

  • throws($exception) - Throws an exception
  • returns($response) - Returns a $response
  • callback(function() { /*...*/ }) - Calling a callback

Partial Mock

partial mock is used when you need some of the methods to behave normally except from that one method you need to test. that can be done with partial mock, it keeps the logic unless you stub the method.

class Foo {
  function bar() { return 'bar'; }
}

$mock = ShortifyPunit::mock('Foo');
$partialMock = ShortifyPunit::partialMock('Foo');

$mock->bar(); // returns NULL
echo $partialMock->bar(); // prints 'bar'

ShortifyPunit::when($partialMock)->bar()->returns('foo'); // stubbing partialMock
echo $partialMock->bar(); // prints 'foo'

Stubbing Method Chaining

 // Creating a new mock for SimpleClassForMocking
 $mock = ShortifyPunit::mock('SimpleClassForMocking');

  ShortifyPunit::when($mock)->first_method()->second_method(1)->returns(1);
  ShortifyPunit::when($mock)->first_method()->second_method(2)->returns(2);
  ShortifyPunit::when($mock)->first_method(1)->second_method(1)->returns(3);
  ShortifyPunit::when($mock)->first_method(2)->second_method(2)->third_method()->returns(4);
  
  echo $mock->first_method()->second_method(1); // prints '1'
  echo $mock->first_method()->second_method(2); // prints '2'
  echo $mock->first_method(1)->second_method(1); // prints '3'
  echo $mock->first_method(2)->second_method(2)->third_method(); // prints '4'

when function is also used to stub chained methods, follows the same actions as the single function stubbing return, throw or callback.

Verifying

Once created, mock will remember all invocations. Then you can selectively verify some interaction you are inserted in.

    $mock = ShortifyPunit::mock('SimpleClassForMocking');

    ShortifyPunit::when($mock)->first_method()->returns(1);
    echo $mock->first_method(); // method called once

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(1); // returns TRUE

    echo $mock->first_method(); // method has been called twice

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns TRUE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(2); // returns TRUE

Methods:

  • atLeast($times) - Verify called at least $times
  • atLeastOnce() - Alias of atLeast(1)
  • calledTimes($times) - Verify called exactly $times
  • neverCalled() - Alias of calledTimes(0)
  • lessThan($times) - Verify called less than $times

Argument Matcher

ShortifyPunit allows the use of Hamcrest PHP (https://github.com/hamcrest/hamcrest-php) matcher on any argument. Hamcrest is a library of "matching functions" that, given a value, return true if that value matches some rule.

Hamcrest matchers are included by default.

Examples:

class Foo
{
  function bar($arg){}
}

$stub = ShortifyPunit::mock('Foo');
ShortifyPunit::when($stub)->bar(anything())->return('FooBar');

Some common Hamcrest matchers:

  • Core
    • anything - always matches, useful if you don't care what the object under test is
  • Logical
    • allOf - matches if all matchers match, short circuits (like PHP &&)
    • anyOf - matches if any matchers match, short circuits (like PHP ||)
    • not - matches if the wrapped matcher doesn't match and vice versa
  • Object
    • equalTo - test object equality using the == operator
    • anInstanceOf - test type
    • notNullValue, nullValue - test for null
  • Number
    • closeTo - test floating point values are close to a given value
    • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - test ordering
  • Text
    • equalToIgnoringCase - test string equality ignoring case
    • equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
    • containsString, endsWith, startsWith - test string matching
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].