Skip to content

Fix PHPUnit warning for mocks without expectation, use stubs instead#488

Merged
greg0ire merged 1 commit into
doctrine:4.1.xfrom
GromNaN:phpunit-stub
Dec 7, 2025
Merged

Fix PHPUnit warning for mocks without expectation, use stubs instead#488
greg0ire merged 1 commit into
doctrine:4.1.xfrom
GromNaN:phpunit-stub

Conversation

@GromNaN

@GromNaN GromNaN commented Dec 6, 2025

Copy link
Copy Markdown
Member

Fix the new notice of PHPUnit 12.5.0.

A PHPUnit notice is now emitted for test methods that create a mock object but do not configure an expectation for it

Example of PHPUnit notice when running ./vendor/bin/phpunit --display-all-issues:

1) Doctrine\Tests\Persistence\Mapping\AbstractClassMetadataFactoryTest::testItGetsTheSameMetadataForBackslashedClassName
No expectations were configured for the mock object for Doctrine\Persistence\Mapping\Driver\MappingDriver. You should refactor your test code and use a test stub instead.

@GromNaN GromNaN added this to the 4.1.2 milestone Dec 6, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a new PHPUnit 12.5.0 notice by replacing createMock() with createStub() for test doubles that don't require expectation configuration. The changes eliminate warnings about mock objects created without expectations, following PHPUnit's best practice of using stubs for simple return values and mocks only when verifying behavior.

Key changes:

  • Replaced createMock() with createStub() for objects used as simple return values or dependencies without behavior expectations
  • Added atLeast(0) expectations in FileDriverTest for methods that need to be called but don't require strict verification
  • Added a never() expectation in ManagerRegistryTest to properly configure a mock that should not have a specific method called

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/ObjectManagerDecoratorTest.php Converted repository, class metadata, and metadata factory mocks to stubs as they serve as return values
tests/Mapping/FileDriverTest.php Converted class metadata mocks to stubs; added atLeast(0) expectations to file locator for methods that act as stubs
tests/Mapping/DriverChainTest.php Converted class metadata and mapping driver mocks to stubs for test fixtures
tests/Mapping/ClassMetadataFactoryTest.php Converted driver and metadata mocks to stubs in setup and test methods
tests/Mapping/AbstractClassMetadataFactoryTest.php Converted driver and metadata mocks to stubs for simple return values
tests/ManagerRegistryTest.php Converted repository and metadata mocks to stubs; added never() expectation to object manager mock
tests/Event/PreUpdateEventArgsTest.php Converted object manager mock to stub in test setup

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/Mapping/FileDriverTest.php Outdated
1) Doctrine\Tests\Persistence\Mapping\AbstractClassMetadataFactoryTest::testItGetsTheSameMetadataForBackslashedClassName
No expectations were configured for the mock object for Doctrine\Persistence\Mapping\Driver\MappingDriver. You should refactor your test code and use a test stub instead.
@greg0ire greg0ire merged commit abd2c9e into doctrine:4.1.x Dec 7, 2025
11 checks passed
@greg0ire

greg0ire commented Dec 7, 2025

Copy link
Copy Markdown
Member

Thanks @GromNaN !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants