Skip to content

[PHPUnit120] Skip mocks configured through an alias in ExpressionCreateMockToCreateStubRector - #823

Merged
TomasVotruba merged 2 commits into
rectorphp:mainfrom
bbrala:fix-create-stub-mock-alias
Nov 14, 2026
Merged

TomasVotruba merged 2 commits into
rectorphp:mainfrom
bbrala:fix-create-stub-mock-alias

Conversation

@bbrala

@bbrala bbrala commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Fixes rectorphp/rector#9967.

ExpressionCreateMockToCreateStubRector turned mocks into stubs when their expectations are configured through another variable, e.g.

$a = $this->createMock(StorageInterface::class);
$b = $this->createMock(StorageInterface::class);
foreach ([$a, $b] as $mock) {
    $mock->expects($this->once())->method('createCollection');
}

After the change the test fails with Call to undefined method TestStub_...::expects().

Cause

MockObjectExprDetector::isUsedForMocking() only treats a variable as a mock when a method is called on it directly, or when it is passed to a MockObject-typed parameter. An expects() call on an alias is not connected back to the original variable.

Change

New MockObjectExprDetector::isEscapingToAlias(), used by ExpressionCreateMockToCreateStubRector only. It treats the variable as possibly mocked when it is:

  • an item of an array literal used as a foreach expression,
  • assigned to something else, directly or as an item of an array literal ($alias = $a, $x[] = $a, $mocks = [$a], $this->mocks = [$a], =&),
  • captured by reference in a closure (use (&$a)).

An array literal passed directly as a call or new argument is not treated as an alias, which keeps the behaviour consistent with CreateStubOverCreateMockArgRector (its handle_assigned_variable* fixtures convert that case). I kept the check out of isUsedForMocking() for the same reason: changing it there would break those fixtures.

Tests

New fixtures that fail without the change: skip_foreach_alias, skip_array_assigned_to_variable, skip_assigned_alias, skip_closure_by_reference. put_in_array_argument documents that an array passed as an argument is still converted.

Full suite passes (699 tests), plus ECS, class-leak and Rector.

Context

Found running the composer-based PHPUnit set on Drupal core (https://www.drupal.org/i/3638148), in RecipeConfigStorageWrapperTest::testCreateCollection(). With this branch that file is no longer changed.

CreateStubOverCreateMockArgRector has related escape cases (rectorphp/rector#9966); this PR doesn't change that rule.

@TomasVotruba
TomasVotruba merged commit ae99efa into rectorphp:main Nov 14, 2026
8 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

Well done, thank you 👍

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.

ExpressionCreateMockToCreateStubRector turns mocks into stubs when expects() is called through an alias (foreach, assignment)

2 participants