PHPackages                             danrevah/shortifypunit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. danrevah/shortifypunit

ActiveLibrary[Testing &amp; Quality](/categories/testing)

danrevah/shortifypunit
======================

PHP Mocking framework

v1.1.1(10y ago)324.2k61MITPHPPHP &gt;=5.4.0

Since Dec 7Pushed 8y ago2 watchersCompare

[ Source](https://github.com/danrevah/ShortifyPunit)[ Packagist](https://packagist.org/packages/danrevah/shortifypunit)[ Docs](https://github.com/danrevah/ShortifyPunit)[ RSS](/packages/danrevah-shortifypunit/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (11)Used By (1)

ShortifyPunit
==============

[](#shortifypunit-)

[![Build Status](https://camo.githubusercontent.com/c1cb70489a616387e05d989e2397d4630657279141397775fb562fcf0102a5a1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64616e72657661682f53686f727469667950756e69742f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/danrevah/ShortifyPunit/build-status/master) [![Coverage Status](https://camo.githubusercontent.com/c697d1150b643d90efa28375e653396a07f6cdf9d0866751044e93a357baddd8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f64616e72657661682f53686f727469667950756e69742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/repos/danrevah/ShortifyPunit/badge.svg?branch=master)[![Code Quality](https://camo.githubusercontent.com/3cc682340ff73265457dd56a283279677696ae63f797bf849baf9508eb1da044/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64616e72657661682f53686f727469667950756e69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://camo.githubusercontent.com/3cc682340ff73265457dd56a283279677696ae63f797bf849baf9508eb1da044/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64616e72657661682f53686f727469667950756e69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572) [![Latest Stable Version](https://camo.githubusercontent.com/97c66dfe3ba7f03c426f45255788c8b4b61e0bff71f724d987ad908d031955e5/68747470733a2f2f706f7365722e707567782e6f72672f64616e72657661682f73686f727469667970756e69742f762f737461626c652e737667)](https://packagist.org/packages/danrevah/shortifypunit)

> PHP Mocking Framework, inspired by Mockito library for Java

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Mocking](#mocking-examples)
- [Stubbing](#stubbing)
- [Partial Mock](#partial-mock)
- [Stubbing Method Chaining](#stubbing-method-chaining)
- [Verifying](#verifying)
- [Argument Matcher](#argument-matcher)

Installation
------------

[](#installation)

> PHP Version &gt;= 5.4 is required!

The following instructions outline installation using Composer. If you don't have Composer, you can download it from

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

```

Mocking Examples
----------------

[](#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
--------

[](#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)

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

[](#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
---------

[](#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
----------------

[](#argument-matcher)

ShortifyPunit allows the use of 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 &amp;&amp;)
    - `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

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~42 days

Recently: every ~80 days

Total

10

Last Release

3797d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ec9b682c32daa6a301950c3cd98907682e465c9698b57898c5b03ad886e8452a?d=identicon)[danrevah](/maintainers/danrevah)

---

Top Contributors

[![danrevah](https://avatars.githubusercontent.com/u/7808742?v=4)](https://github.com/danrevah "danrevah (185 commits)")

---

Tags

mockmock-libraryphpphp-mocking-frameworkphp7

### Embed Badge

![Health badge](/badges/danrevah-shortifypunit/health.svg)

```
[![Health](https://phpackages.com/badges/danrevah-shortifypunit/health.svg)](https://phpackages.com/packages/danrevah-shortifypunit)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[pdepend/pdepend

Official version of pdepend to be handled with Composer

954110.9M815](/packages/pdepend-pdepend)[instaclick/php-webdriver

PHP WebDriver for Selenium 2

43761.8M22](/packages/instaclick-php-webdriver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
