holgerk/mockery-simple-mock

Simple mock helper for Mockery

Maintainers

Package info

github.com/holgerk/mockery-simple-mock

pkg:composer/holgerk/mockery-simple-mock

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-03-02 22:27 UTC

This package is auto-updated.

Last update: 2026-03-02 22:34:49 UTC


README

Tests Latest Version on Packagist PHP Version License

A single helper function that wraps Mockery to make mocking and call capturing straightforward.

Installation

composer require holgerk/mockery-simple-mock

Usage

use function Holgerk\MockerySimpleMock\simpleMock;

Basic mock

$mock = simpleMock(MyService::class);

Capture calls

Pass a variable by reference to collect every call made to any public method, keyed by method name and then by parameter name:

$mock = simpleMock(MyService::class, $capturedCalls);

$mock->greet('Alice', 42);

// $capturedCalls['greet'] === [['name' => 'Alice', 'age' => 42]]

Multiple calls to the same method are appended in order:

$mock->greet('Alice', 1);
$mock->greet('Bob', 2);

// $capturedCalls['greet'] === [
//     ['name' => 'Alice', 'age' => 1],
//     ['name' => 'Bob', 'age' => 2],
// ]

Asserting calls with assertEquals

A key advantage of captured calls over traditional mock expectations is that assertEquals shows you a full diff between what you expected and what actually happened. With expectation-style assertions you typically only learn that a call was wrong; with captured calls you see exactly which argument differed and by how much.

$mock = simpleMock(MyService::class, $capturedCalls);

$mock->greet('Alice', 42);
$mock->greet('Bob', 7);

assertEquals([
    ['name' => 'Alice', 'age' => 42],
    ['name' => 'Bob',   'age' => 7],
], $capturedCalls['greet']);

Tip: Writing out the expected array by hand can be tedious. My holgerk/assert-golden package removes that step by recording the actual value directly into your source code on the first run, turning it into the expectation automatically.

When a call is wrong, PHPUnit prints a structured diff like:

Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 [
     ['name' => 'Alice', 'age' => 42],
-    ['name' => 'Bob', 'age' => 7],
+    ['name' => 'Bob', 'age' => 99],
 ]

Configure return values

Pass an array of methodName => returnValue pairs as the third argument:

$mock = simpleMock(MyService::class, $capturedCalls, [
    'greet' => 'Hello, Alice!',
]);

$result = $mock->greet('Alice', 42); // 'Hello, Alice!'

Requirements

  • PHP >= 8.1
  • mockery/mockery >= 1.5