Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes with mock properties used in multiple methods but one, to avoid irrelevant notices in tests run
use PHPUnit\Framework\TestCase;
+use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
+
+#[AllowMockObjectsWithoutExpectations]
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
protected function setUp(): void
{
$this->someServiceMock = $this->createMock(SomeService::class);
}
public function testOne(): void
{
- // use $this->someServiceMock
+ $this->someServiceMock->expects($this->once())
+ ->method('someMethod')
+ ->willReturn('someValue');
}
public function testTwo(): void
{
- // use $this->someServiceMock
+ $this->someServiceMock->expects($this->once())
+ ->method('someMethod')
+ ->willReturn('anotherValue');
}
}
Configure your rector.php:
<?php
use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector;
return RectorConfig::configure()
->withRules([
AllowMockObjectsWithoutExpectationsAttributeRector::class,
]);