承接 mnapoli/phpunit-easymock 相关项目开发

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

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

mnapoli/phpunit-easymock

最新稳定版本:1.5.0

Composer 安装命令:

composer require mnapoli/phpunit-easymock

包简介

Helpers to build PHPUnit mocks

README 文档

README

Helpers to build PHPUnit mock objects easily.

Total Downloads

Why?

This library is not a mocking library. It's just a few helpers to write the most common mocks more easily.

It doesn't reinvent anything and is not intended to cover every use case: only the most common ones.

Installation

$ composer require --dev mnapoli/phpunit-easymock

To be able to use EasyMock in your tests you must include the trait in your class:

class MyTest extends \PHPUnit\Framework\TestCase { use \EasyMock\EasyMock; // ... }

Usage

Here is what a very common PHPUnit mock looks like:

$mock = $this->createMock('My\Class'); $mock->expect($this->any()) ->method('sayHello') ->willReturn('Hello');

Yuck!

Here is how to write it with EasyMock:

$mock = $this->easyMock('My\Class', [ 'sayHello' => 'Hello', ]);

What if you want to assert that the method is called once (i.e. $mock->expect($this->once()))? Use spy() instead:

$mock = $this->easySpy('My\Class', [ 'sayHello' => 'Hello', ]);

Features

You can mock methods so that they return values:

$mock = $this->easyMock('My\Class', [ 'sayHello' => 'Hello', ]);

Or so that they use a callback:

$mock = $this->easyMock('My\Class', [ 'sayHello' => function ($name) { return 'Hello ' . $name; }, ]);

You can also have methods throw exceptions by providing an Exception instance:

$mock = $this->easyMock('My\Class', [ 'sayHello' => new \RuntimeException('Whoops'), ]);

It is possible to call the mock() method again on an existing mock:

$mock = $this->easyMock('My\Class'); $mock = $this->easyMock($mock, [ 'sayHello' => 'Hello', ]);

What if?

If you want to use assertions or other PHPUnit features, just do it:

$mock = $this->easyMock('My\Class', [ 'sayHello' => 'hello', ]); $mock->expects($this->once()) ->method('sayGoodbye') ->willReturn('Goodbye');

Mocks are plain PHPUnit mocks, nothing special here.

Contributing

See the CONTRIBUTING file.

License

Released under the MIT license.

统计信息

  • 总下载量: 72.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 38
  • 点击次数: 3
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固