Skip to content

CreateStubOverCreateMockArgRector turns a mock into a stub when the argument is stored and configured with expects() later #9966

Open

Description

@bbrala
Subject Details
Rector version 2.8.4 (rector-phpunit main has the same rule code)
PHPUnit 12.5.34

Minimal PHP Code Causing Issue

<?php

namespace Repro;

use PHPUnit\Framework\TestCase;

interface IdMapInterface
{
    public function delete(): void;
}

final class StubArgReproTest extends TestCase
{
    private $idMap;

    public function testDeleteIsCalled(): void
    {
        $this->setIdMap($this->createMock(IdMapInterface::class));

        $this->idMap->expects($this->once())->method('delete');

        $this->idMap->delete();
    }

    private function setIdMap($idMap): void
    {
        $this->idMap = $idMap;
    }
}

Responsible rules

  • CreateStubOverCreateMockArgRector

Actual Behavior

-        $this->setIdMap($this->createMock(IdMapInterface::class));
+        $this->setIdMap($this->createStub(IdMapInterface::class));

The test then fails:

Error: Call to undefined method TestStub_IdMapInterface_cfe43d7c::expects()

Expected Behavior

No change. The mock passed as an argument escapes into a property (or an array, or another object) and gets expectations configured later, so it has to stay a mock.

Context

Found running the composer-based PHPUnit set on Drupal core (https://www.drupal.org/i/3638148). It caused 10 unit test failures in 4 classes, with these patterns:

  • MigrateExecutableTest, MigrateSourceTest: $this->getMigration($this->createMock(MigrateIdMapInterface::class)) stores the argument in $this->idMap, and the test later calls $this->idMap->expects(...).

  • CronSuspendQueueDelayTest: 'queueManager' => $this->createMock(QueueWorkerManagerInterface::class) goes into a constructor-arguments array property, and the mock is configured from there later.

  • RecipeConfigStorageWrapperTest::testCreateCollection(): a second variant. The mocks are local variables, configured through a loop alias, then passed to a constructor:

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

    Both become createStub(), because the expects() call goes through $mock, not $a/$b.

Since the rule can't see whether a mock gets expects() once it leaves the expression or goes through an alias, skipping arguments to methods of the test class itself, array items assigned to properties, and variables that appear in an array/foreach would be a safe start.

Activity

  1. bbrala commented on Nov 14, 2026

    @bbrala
    Author

    Correction on the RecipeConfigStorageWrapperTest::testCreateCollection() case: re-running with CreateStubOverCreateMockArgRector skipped, the same change comes from ExpressionCreateMockToCreateStubRector:

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

    In the first run CreateStubOverCreateMockArgRector happened to make it first. So the loop-alias variant belongs to ExpressionCreateMockToCreateStubRector, which also misses expects() calls made through an alias like the foreach variable.

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

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