Skip to content

#[AllowMockObjectsWithoutExpectations] incorrectly added #9650

Closed
rectorphp/rector-phpunit
#678

Description

@philbates35

Bug Report

Subject Details
Rector version 2.3.6 (invoke vendor/bin/rector --version)

Any phpunit test case that extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase (symfony/validator 7.4.5), is having the AllowMockObjectsWithoutExpectations attribute added to it, but as per symfony/symfony#62669 and symfony/symfony#62745, since symfony/validator 7.4.3, its replaced all mocks that have no expectations with stubs so generates no warnings.

The fact that there are no phpunit warnings when runninng these tests, makes me think that this this is fixed on the symfony side and rector is flagging this as a false positive. I'm not sure exactly what it is about ConstraintValidatorTestCase that makes rector think that it still needs to add the AllowMockObjectsWithoutExpectations attribute though.

Minimal PHP Code Causing Issue

Install symfony/validator 7.4.5, write any custom test that extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase, run rector on it with the phpunit rules enabled, and you'll see that AllowMockObjectsWithoutExpectations is added to your test class.

Expected Behaviour

AllowMockObjectsWithoutExpectations attribute should not be added automatically to all tests that extend \Symfony\Component\Validator\Test\ConstraintValidatorTestCase.

Activity

  1. TomasVotruba commented on Feb 13, 2026

    @TomasVotruba
    Member

    Thank you for your report!

    We'll need an isolated failing demo link from: http://getrector.com/demo,
    that way we can reproduce the bug.

  2. samsonasik commented on Feb 13, 2026

    @samsonasik
    Member

    It may already resolved at PR:

    Please try latest "rector/rector": "dev-main", if the issue persists, please provide reproducible code, thank you.

  3. philbates35 commented on Feb 16, 2026

    @philbates35
    Author

    Hi @samsonasik, I did try on dev-master but I was still seeing the same false positives.

    I dug into it a little deeper, and its actually much simpler than I realised.

    The combination of these two:

    Means if your test class extends Symfony\Component\Validator\Test\ConstraintValidatorTestCase it will have the attribute added (its that simple), but AFAIK and as I linked to in the OP, after symfony/validator 6.4.31, 7.3.9, 7.4.3, 8.0.3 we don't need AllowMockObjectsWithoutExpectations attribute any more when extending that class.

  4. samsonasik commented on Feb 16, 2026

    @samsonasik
    Member

    If this due to combination on multiple rules, could you create failing test PR at

    https://github.com/rectorphp/rector-phpunit/tree/main/tests/Issues

    thank you.

  5. philbates35 commented on Feb 17, 2026

    @philbates35
    Author

    Hi @samsonasik, its not due to a combination of multiple rules, the issue that I'm seeing is this:

    https://getrector.com/demo/3329b168-6a16-4639-b881-ef8a8678984b

    You can see that Rector is adding the AllowMockObjectsWithoutExpectations attribute to any class that extends ConstraintValidatorTestCase - this was needed at one point, but since symfony validator 6.4.31, 7.3.9, 7.4.3, 8.0.3 its no longer needed because they fixed in in the parent ConstraintValidatorTestCase class.

  6. philbates35 commented on Feb 17, 2026

    @philbates35
    Author

    @samsonasik i'm not sure there's necessary and i the fact you're asking makes me think I've not got my point across well enough and its not clear what the issue actually is 😬 I'll try again:

    We're discussing AllowMockObjectsWhereParentClassRector, it was added in rectorphp/rector-phpunit#641 2 weeks ago (with no additional context as to why in the PR description / linked issue), and its whole purpose is to add #[AllowMockObjectsWithoutExpectations] attribute to all test classes that extend Symfony\Component\Validator\Test\ConstraintValidatorTestCase or Symfony\Component\Form\Test\TypeTestCase.

    There's no bug in the implementation of AllowMockObjectsWhereParentClassRector, it was indeed working as intended as can be seen here: https://github.com/rectorphp/rector-phpunit/blob/main/rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector/Fixture/some_class.php.inc

    What I'm trying to say is that AllowMockObjectsWhereParentClassRector doesn't need to exist since symfony 6.4.31, 7.3.9, 7.4.3, 8.0.3 as they've fixed the issue on their side. Any proposed PR fix that should be submitted to resolve this issue would essentially be to revert rectorphp/rector-phpunit#641 to remove it.

    Without any more changes, it means that all my tests that extend Symfony\Component\Validator\Test\ConstraintValidatorTestCase automatically get #[AllowMockObjectsWithoutExpectations] attribute added to it, which isn't necessary as I'm running latest symfony 7.4.* which contains the fixes I linked to above.

    Does that help explain the issue a little more clearly?

  7. github-actions commented on Apr 19, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale due to inactivity. It will be closed in next 30 days if no further activity occurs.

  8. github-actions commented on Nov 1, 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.

  9. locked as resolved and limited conversation to collaborators on Nov 1, 2026
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions