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

v0.2.0 2025-11-16 14:58 UTC

This package is auto-updated.

Last update: 2025-11-16 15:00:40 UTC


README

PHP 8.1+ Latest version GitHub Workflow Status Codecov Downloads License

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 WithExtras trait.

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 call
  • getLastValue() → last call
  • getAllValues() → all calls
  • count() → 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