holgerk/mockery-simple-mock
Composer 安装命令:
composer require holgerk/mockery-simple-mock
包简介
Simple mock helper for Mockery
README 文档
README
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
holgerk/mockery-simple-mock 适用场景与选型建议
holgerk/mockery-simple-mock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.29k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 holgerk/mockery-simple-mock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 holgerk/mockery-simple-mock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-02