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

Deprecate TestCase::getObjectForTrait() #5244

Closed
sebastianbergmann opened this issue Feb 23, 2023 · 0 comments
Closed

Deprecate TestCase::getObjectForTrait() #5244

sebastianbergmann opened this issue Feb 23, 2023 · 0 comments
Assignees
Labels
feature/test-doubles Test Stubs and Mock Objects type/deprecation Something will be/is deprecated
Milestone

Comments

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented Feb 23, 2023

PHPUnit can automatically generate test stubs and mock objects (test doubles) based on interfaces and classes.

For quite a while, PHPUnit has offered the createStub() and createMock() methods for creating test stubs and mock objects with best practice defaults. Furthermore, alternatives such as getMockForTrait() exist, but not all of them offer the same clear separation between test double and mock object. For instance, no method named getStubForTrait() exists. Such an inconsistency can lead to confusion.

The getObjectForTrait() method returns a mock object for an otherwise empty class that uses a specified trait. This allows for testing the concrete methods of a trait.

Having to use getObjectForTrait() to test something is almost always a code smell: something is not quite right with the software design of the system-under-test.

To promote better software design, improve the readability of test code, and to reduce complexity inside PHPUnit's test double functionality, TestCase::getObjectForTrait() will be deprecated and then removed:

  • soft deprecation in PHPUnit 10.1 (add @deprecated annotation to the method declaration)
  • deprecation in PHPUnit 11 (using the method will trigger a deprecation)
  • removal in PHPUnit 12
@sebastianbergmann sebastianbergmann added type/backward-compatibility Something will be/is intentionally broken feature/test-doubles Test Stubs and Mock Objects labels Feb 23, 2023
@sebastianbergmann sebastianbergmann added this to the PHPUnit 11.0 milestone Feb 23, 2023
@sebastianbergmann sebastianbergmann self-assigned this Feb 23, 2023
@sebastianbergmann sebastianbergmann changed the title Deprecate TestCase::getObjectForTrait() Deprecate TestCase::getObjectForTrait() Mar 6, 2023
@sebastianbergmann sebastianbergmann added type/deprecation Something will be/is deprecated and removed type/backward-compatibility Something will be/is intentionally broken labels Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Test Stubs and Mock Objects type/deprecation Something will be/is deprecated
Projects
None yet
Development

No branches or pull requests

1 participant