Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow addMethods on unknown type mock #4852

Closed
alamirault opened this issue Jan 4, 2022 · 2 comments
Closed

Allow addMethods on unknown type mock #4852

alamirault opened this issue Jan 4, 2022 · 2 comments
Labels
feature/test-doubles Stubs and Mock Objects type/enhancement A new idea that should be implemented

Comments

@alamirault
Copy link

Since #3687 setMethods is deprecated in favor of onlyMethods and addMethods

It was possible to do this

$this->getMockBuilder('UnknownClass')
            ->allowMockingUnknownTypes()
            ->setMethods(['execute'])
            ->getMock();

but with

$this->getMockBuilder('UnknownClass')
            ->allowMockingUnknownTypes()
            ->addMethods(['execute'])
            ->getMock();

An exception is thrown PHPUnit\Framework\MockObject\ReflectionException: Class UnknownClass does not exist.

So is not possible to mock unknown types.

A possibility is to check allowMockingUnknownTypes in mockBuilder and if is true not thrown exception (as suggested here #3687 (comment))

What do you think ? Or how can I mock methods on unknown type ? (if you are ok, I can make a PR)

@alamirault alamirault added the type/enhancement A new idea that should be implemented label Jan 4, 2022
@sebastianbergmann
Copy link
Owner

I would gladly consider a pull request. Thanks!

@sebastianbergmann
Copy link
Owner

getMockBuilder() will be soft-deprecated (@deprecated annotation) in PHPUnit 10.1, deprecated (triggering a deprecation warning) in PHPUnit 11, and removed in PHPUnit 12. Therefore no changes should be made to it anymore.

@sebastianbergmann sebastianbergmann added the feature/test-doubles Stubs and Mock Objects label Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Stubs and Mock Objects type/enhancement A new idea that should be implemented
Projects
None yet
Development

No branches or pull requests

2 participants