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

Requesting recipes on how to deal with setting properties on Mocks with PHP 8.2+ #1197

Closed
jrfnl opened this issue Nov 7, 2022 · 2 comments · Fixed by #1210
Closed

Requesting recipes on how to deal with setting properties on Mocks with PHP 8.2+ #1197

jrfnl opened this issue Nov 7, 2022 · 2 comments · Fixed by #1210
Labels
Bug An error or unexpected behavior.
Milestone

Comments

@jrfnl
Copy link
Contributor

jrfnl commented Nov 7, 2022

PHP 8.2 deprecated the use of dynamic properties. This impacts the use of Mockery when using anonymous mocks or mocks for classes which are not available.

The remaining 11 test failures for Mockery itself on PHP 8.2 are all related to this.

I'll be the first to admit I'm no Mockery expert, so I've been experimenting with how to work round the deprecation and so far I've not had much luck finding elegant solutions.

Now of course, one shouldn't mock what you do not own, but there are test situations where certain external dependencies (framework) may not be available during testing, so there's not much which can done about that.

Findings so far:

  • Making the mock extend stdClass doesn't seem to work as this seems to run into trouble when the method under test has a class based parameter type declaration.
  • Using Mockery::namedMock() and having the named mock extend stdClass, works for the first test case, but that's it. (also noted as such in the documentation).
    Re-using the mock object isn't really an option as the object won't be "clean" anymore if properties are being assigned from the tests.
  • Along the same lines, using alias: mocks would require most tests to run in a separate process, which makes the test suite a lot slower as well as less stable.

Suggestions and "recipes" to solve this very welcome!

@carloscz
Copy link

carloscz commented Nov 7, 2022

Wondering about the same thing, preparing migration of project of ours to the new PHP and mockery emits PHP warnings when setting up properties. 👍

@ghostwriter
Copy link
Member

Resolved via #1210

The solution we chose was to dynamically add the #[\AllowDynamicProperties] PHP attribute on a mock object for PHP >= 8.2.

This should allow Mockery to keep all of its previous functionality exactly the same on newer versions of PHP.

@ghostwriter ghostwriter added the Bug An error or unexpected behavior. label Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An error or unexpected behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants