Skip to content

[Feature Request] Add support for function stubs (without invocation count checks) for PHPUnit 10+ compatibility #96

@Voral

Description

@Voral

Hi there!

Thank you for this great library. I’m actively using php-mock/php-mock-phpunit in my project.

The Problem:
Starting with PHPUnit 10 (and leading up to PHPUnit 14), the self::any() matcher is deprecated. PHPUnit now recommends avoiding invocation count expectations (expects) when we only care about the return value, not the interaction itself (i.e., using stubs instead of mocks).

However, the current php-mock API requires calling ->expects() before ->willReturn().

  • If I remove ->expects(), the chain breaks.
  • If I use ->expects(self::any()), I get a deprecation warning.
  • The only current workaround is to use an internal PHPUnit class:
    ->expects(new \PHPUnit\Framework\MockObject\Rule\AnyInvokedCount())
    This works but relies on PHPUnit’s internal implementation, which is fragile and not recommended.

Proposal:
It would be great to add a method to create function "stubs" where invocation count checks are skipped, similar to how modern PHPUnit handles object stubs.

For example:

// Hypothetical API option 1: Dedicated helper
PHPMock::getFunctionStub('namespace', 'functionName')
    ->willReturn('value');

// Hypothetical API option 2: Chain modifier
self::getFunctionMock('namespace', 'functionName')
    ->stub() // Skips expects and allows direct return configuration
    ->willReturn('value');

Benefits:

  1. Eliminates deprecation warnings in tests.
  2. Makes test code cleaner and semantically correct (clear distinction between Mocks and Stubs).
  3. Future-proofs the library for upcoming PHPUnit versions where any() might be completely removed.

Are there any plans to evolve the API in this direction?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions