Skip to content

AllowMockObjectsAttribute rector rules are applied on stubs when used in setup #9636

@tandev

Description

@tandev

Bug Report

Subject Details
Rector version 2.3.6

AllowMockObjectsForDataProviderRector & AllowMockObjectsWithoutExpectationsAttributeRector are applied on stub usage when they should only be applied on mock usage. This seems to only happen when stub is setup in setUp method.

Minimal PHP Code Causing Issue

https://getrector.com/demo/457a19c3-f0d0-4d4f-9a44-a567c8250736

<?php

use PHPUnit\Framework\TestCase;

+#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class FooTest extends TestCase
{
    private ClassToStub $foo;

    public function setUp(): void
    {
        parent::setUp();

        $foo = self::createStub(\ClassToStub::class);
        $foo->method('foo')->willReturn('error');

        $this->foo = $foo;
    }

    public function test()
    {
       $this->foo->foo();
    }
}

class ClassToStub
{
    public function foo(): string
    {
        return 'error';
    }
};

Expected Behaviour

Rector should not apply the rule

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions