ion-bazan/phpunit-extras
Composer 安装命令:
composer require ion-bazan/phpunit-extras
包简介
Java-style test automation for PHPUnit - using PHP 8.1+ attributes
README 文档
README
Java-style test automation for PHPUnit - using PHP 8.1+ attributes.
Write cleaner, declarative, and expressive unit tests with:
#[Mock] private Logger $logger; #[Stub] private Payment $paymentService; #[InjectMocks] private OrderService $service; #[Captor] private ArgumentCaptor $message;
Inspired by Mockito, JUnit 5, and Spring Boot Testing.
Installation
composer require ion-bazan/phpunit-extras
Features
| Feature | Status | Java Equivalent |
|---|---|---|
#[Mock] |
Done | @Mock |
#[Stub] |
Done | @Mock |
#[InjectMocks] |
Done | @InjectMocks |
#[Captor] |
Done | ArgumentCaptor |
#[Spy] |
Planned | @Spy |
#[TempDir] |
Planned | @TempDir |
#[ValueSource] |
Planned | @ValueSource |
Usage
1. Use WithExtras trait
use IonBazan\PHPUnitExtras\WithExtras; class OrderServiceTest extends TestCase { use WithExtras; #[Stub] private Payment $payment; #[Mock] private Logger $logger; #[Captor] private ArgumentCaptor $message; #[InjectMocks] private OrderService $service; public function testOrderLogsCharge(): void { $this->logger->expects($this->once()) ->method('log') ->with($this->message); $this->service->place(150); $this->assertSame('Charged 150', $this->message->getValue()); } }
All attributes are processed automatically via
WithExtrastrait.
2. Manual Processing (for fine control) TBD
#[Captor] – Capture Method Arguments
#[Captor] private ArgumentCaptor $message; $this->logger->expects($this->once()) ->method('log') ->with($this->message); $this->logger->log('hello world'); $this->assertSame('hello world', $this->message->getValue()); $this->assertSame(['hello world'], $this->message->getAllValues());
getValue()→ first callgetLastValue()→ last callgetAllValues()→ all callscount()→ number of calls
Requirements
- PHP 8.1+
- PHPUnit 10+
Why phpunit-extras?
| Benefit | Description |
|---|---|
| Less boilerplate | No setUp(), no manual mocks |
| Declarative | Intent in attributes |
| IDE-friendly | Autocomplete, refactoring |
| Extensible | Add new handlers easily |
Roadmap
-
#[Spy]– partial mocks -
#[TempDir]– auto-cleaned temp dirs -
#[TestFactory]– dynamic tests
License
MIT © Ion Bazan
ion-bazan/phpunit-extras 适用场景与选型建议
ion-bazan/phpunit-extras 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ion-bazan/phpunit-extras 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ion-bazan/phpunit-extras 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-14