ion-bazan/phpunit-extras
最新稳定版本:v0.2.0
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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-14