Skip to content

[phpunit] WithConsecutiveRector should be removed from PHPUnitSetList::PHPUNIT_90 #9366

Closed
rectorphp/rector-phpunit
#546

Description

@dczech

Processing file with this rule ends up in the not valid code (at least on PHPUnit v 9.6.23)

Sample change

1) tests/SampleTest.php:79

    ---------- begin diff ----------
@@ @@
        $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['dispatch'])
             ->getMock()
         ;
+        /* @phpstan-ignore-next-line method.deprecated (no alternative?) */
+        $matcher = self::exactly(2);

         /* @phpstan-ignore-next-line method.deprecated (no alternative?) */
-        $eventDispatcher->expects(self::exactly(2))
-            ->method('dispatch')
-            ->withConsecutive(
-                [self::isInstanceOf(SubscribedEvent::class)],
-                [self::isInstanceOf(UnsubscribedEvent::class)]
-            )
+        $eventDispatcher->expects($matcher)
+            ->method('dispatch')->willReturnCallback(function (...$parameters) use ($matcher): void {
+            if ($matcher->numberOfInvocations() === 1) {
+                $this->assertSame(self::isInstanceOf(SubscribedEvent::class), $parameters[0]);
+            }
+            if ($matcher->numberOfInvocations() === 2) {
+                $this->assertSame(self::isInstanceOf(UnsubscribedEvent::class), $parameters[0]);
+            }
+        })

Error: Call to undefined method PHPUnit\Framework\MockObject\Rule\InvokedCount::numberOfInvocations()

Activity

  1. TomasVotruba commented on Sep 7, 2025

    @TomasVotruba
    Member

    This method was deprecated in PHPUnit 9 and removed in 10, so set is correct.

    The rule should check for correct new method name instead and apply existing one based on real project code.

  2. dczech commented on Sep 7, 2025

    @dczech
    Author

    The project is based on Symfony 6.4 + symfony/phpunit-bridge and installs PHPUnit through bin/phpunit helper script. This is probably the reason why rector assumes PHPUnit 10 is available.

  3. TomasVotruba commented on Sep 7, 2025

    @TomasVotruba
    Member

    With reflection check, the version doesn't really matter. The first existing method should be used.

    Btw, PHPUnit bridge is pretty messy package, that should be used only in symfony/symfony monorepo to allow running many PHPUnit versions. Removed anywhere else. Unfortunatelly, legacy Symfony skeletons spread it in many projects by default with no gain.

  4. changed the title [-]WithConsecutiveRector should be removed from PHPUnitSetList::PHPUNIT_90[/-] [+][phpunit] WithConsecutiveRector should be removed from PHPUnitSetList::PHPUNIT_90[/+] on Sep 10, 2025
  5. self-assigned this
    on Sep 10, 2025
  6. TomasVotruba commented on Sep 10, 2025

    @TomasVotruba
    Member
  7. github-actions commented on Feb 8, 2026

    @github-actions
    Contributor

    This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

  8. locked as resolved and limited conversation to collaborators on Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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