Skip to content

Feature request: Validate with and willReturn types on mocks #321

Description

@BackEndTea

Given the following example, i'd expect PHPStan to report the following issues:

  • The parameter types in with don't match the function signature
  • The return value does not match the function signature.

This would be usefull for projects with a large test suite, where you do a refactor of a method that is used in a lot of places. It would allow to fix these issues before running the entire test suite to get the errors from PHPUnit.

class T1 {
    public function myMethod(int $a, string $b): int {
        return 3;
    }
}

class T1Test extends TestCase {
    public function testStuff(): void {
        $mock = $this->createMock(T1::class);

        $mock->expects($this->once())
            ->method('myMethod')
            ->with(
                new \stdClass(),
                new \stdClass(),
            )->willReturn(true);
    }
}

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