承接 holgerk/mockery-simple-mock 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

holgerk/mockery-simple-mock

Composer 安装命令:

composer require holgerk/mockery-simple-mock

包简介

Simple mock helper for Mockery

README 文档

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

统计信息

  • 总下载量: 1.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固