ion-bazan / phpunit-extras
Java-style test automation for PHPUnit - using PHP 8.1+ attributes
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/ion-bazan/phpunit-extras
Requires
- php: ^8.1
- phpunit/phpunit: ^10.5 || ^11 || ^12
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