Skip to content

[1.x] Type createMockForIntersectionOfInterfaces() and createStubForIntersectionOfInterfaces() - #207

Merged
alies-dev merged 5 commits into
psalm:masterfrom
alies-dev:feat/185-intersection-mocks
Nov 9, 2026
Merged

alies-dev merged 5 commits into
psalm:masterfrom
alies-dev:feat/185-intersection-mocks

Conversation

@alies-dev

Copy link
Copy Markdown
Contributor

createMockForIntersectionOfInterfaces() and createStubForIntersectionOfInterfaces() return a plain MockObject / Stub, so calling a method of the mocked interfaces, or passing the mock as one of them, is reported.

Before

<?php
final class MyTestCase extends TestCase {
    public function testMock(): void {
        $mock = $this->createMockForIntersectionOfInterfaces([Clock::class, Logger::class]);
        $mock->log('tick'); // UndefinedInterfaceMethod: Method PHPUnit\Framework\MockObject\MockObject::log does not exist
        $this->assertSame(1, $mock->now()); // UndefinedInterfaceMethod: Method PHPUnit\Framework\MockObject\MockObject::now does not exist
        $this->assertSame(1, useClock($mock)); // InvalidArgument: Argument 1 of NS\useClock expects NS\Clock, but PHPUnit\Framework\MockObject\MockObject provided
    }
}

After

        $mock->log('tick'); // no issue
        $this->assertSame(1, $mock->now()); // no issue
        $this->assertSame(1, useClock($mock)); // no issue

The list must be a literal array of at least two distinct X::class entries (directly or via a local variable); anything else keeps the PHPUnit return type.

Fixes #185

@alies-dev alies-dev self-assigned this Nov 9, 2026
@alies-dev
alies-dev marked this pull request as ready for review November 9, 2026 12:53
@alies-dev
alies-dev changed the base branch from 1.x to master November 9, 2026 20:55
@alies-dev
alies-dev merged commit 13e5494 into psalm:master Nov 9, 2026
8 checks passed
@alies-dev
alies-dev deleted the feat/185-intersection-mocks branch November 9, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type createMockForIntersectionOfInterfaces() and createStubForIntersectionOfInterfaces()

1 participant