Deprecate AllowMockObjectsWithoutExpectationsAttributeRector - #768
Merged
TomasVotruba merged 1 commit intoAug 12, 2026
Merged
Conversation
…ve it from set The attribute only silences the "mock created without expectations" notice. The correct fix is to add the missing expects() to the test methods that use the mock, which depends on the test intent and cannot be automated.
TomasVotruba
deleted the
deprecate-allow-mock-objects-without-expectations
branch
August 12, 2026 14:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AllowMockObjectsWithoutExpectationsAttributeRectorguessed when a test class should get#[AllowMockObjectsWithoutExpectations], based on mock properties being used in some test methods but not others.That attribute only silences the notice. It does not fix anything:
+#[AllowMockObjectsWithoutExpectations] final class SomeTest extends TestCase { private MockObject $someServiceMock; protected function setUp(): void { $this->someServiceMock = $this->createMock(SomeService::class); } public function testOne(): void { // $this->someServiceMock is never given an expectation } }The correct fix is to add the missing expectation to the test method that uses the mock:
public function testOne(): void { - // $this->someServiceMock is never given an expectation + $this->someServiceMock->expects($this->once()) + ->method('someMethod') + ->willReturn('someValue'); }That cannot be automated - which method, how many calls and what arguments are the test intent, not a mechanical transform. Guessing
expects($this->once())would break passing tests.So the rule is deprecated the same way as other deprecated rules here: it keeps
getRuleDefinition(), implementsDeprecatedInterfaceand throws fromrefactor(). It is also removed from thecomposer-basedset, and its fixtures are dropped.AllowMockObjectsWhereParentClassRectorandAllowMockObjectsForDataProviderRectorare untouched - there the attribute is the only available fix.