Skip to content

Deprecate AllowMockObjectsWithoutExpectationsAttributeRector - #768

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-allow-mock-objects-without-expectations
Aug 12, 2026
Merged

Deprecate AllowMockObjectsWithoutExpectationsAttributeRector#768
TomasVotruba merged 1 commit into
mainfrom
deprecate-allow-mock-objects-without-expectations

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

AllowMockObjectsWithoutExpectationsAttributeRector guessed 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(), implements DeprecatedInterface and throws from refactor(). It is also removed from the composer-based set, and its fixtures are dropped.

AllowMockObjectsWhereParentClassRector and AllowMockObjectsForDataProviderRector are untouched - there the attribute is the only available fix.

…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
TomasVotruba merged commit a1f2614 into main Aug 12, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-allow-mock-objects-without-expectations branch August 12, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant