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

Replace deprecated setMethods() #207

Closed
solventt opened this issue Oct 19, 2021 · 1 comment · Fixed by #252
Closed

Replace deprecated setMethods() #207

solventt opened this issue Oct 19, 2021 · 1 comment · Fixed by #252

Comments

@solventt
Copy link

In PhpUnit setMethods() is deprecated (sebastianbergmann/phpunit#3687) and will be removed in version 10 (sebastianbergmann/phpunit#3769). The method was split into two methods (onlyMethods and addMethods) according to their responsibilities.

But it's not that simple: setMethods() allows to mock non-existent class. So in PhpUnit 10 we will lose this feature, because onlyMethods and addMethods work with ReflectionClass internally. In this package setMethods() is used to mock non-existent classes, see:

  • UriTest (strings: 140, 306, 360)
  • ResponseTest (string 147)

What's the solution?

  1. To use \stdClass instead of a non-existent class:
$this->getMockBuilder(\stdClass::class)->addMethods(['__invoke'])->getMock();
  1. To create an empty "physical" class

Source - https://stackoverflow.com/questions/60535649/mock-non-existing-class-in-phpunit-10?rq=1

@t0mmy742
Copy link
Contributor

Mocks that we want to create only need to have __toString method.
Third solution: create a mock of \Stringable interface, introduced with PHP 8.0.

$mock = $this->createMock(\Stringable::class);

PHP 7.3 and 7.4 can also work since we already have symfony/polyfill-php80 in our dependencies.

I don't think your second solution is a good idea for maintainability, but first solution is a good one.

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 a pull request may close this issue.

2 participants