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::getMockForAbstractClass() #5241

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

Deprecate TestCase::getMockForAbstractClass() #5241

sebastianbergmann opened this issue Feb 23, 2023 · 4 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.

When it comes to doubling classes, there are limitations:

  • final, private, and static methods cannot be doubled; they retain their original behavior except for static methods which will be replaced by a method throwing an exception
  • Enumerations (enum) are final classes and therefore cannot be doubled
  • readonly classes cannot be extended by classes that are not readonly and therefore cannot be doubled

Not only because of the limitations mentioned above, but also to improve your software design, PHPUnit's documentation recommends to favour the doubling of interfaces over the doubling of 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 getMockForAbstractClass() exist, but not all of them offer the same clear separation between test double and mock object. For instance, no method named getStubForAbstractClass() exists. Such an inconsistency can lead to confusion.

As its name suggests, the getMockForAbstractClass() method can be used to create a mock object for an abstract class: a mock object where all abstract methods of the original class are implemented in such a way that they can be configured to return a specified value, for instance, or to expect an invocation.

Having to use getMockForAbstractClass() 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::getMockForAbstractClass() 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 added a commit to sebastianbergmann/phpunit-documentation-english that referenced this issue Feb 24, 2023
@sebastianbergmann sebastianbergmann changed the title Deprecate TestCase::getMockForAbstractClass() Deprecate TestCase::getMockForAbstractClass() 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
morozov added a commit to morozov/dbal that referenced this issue Jun 14, 2023
morozov added a commit to morozov/dbal that referenced this issue Jun 14, 2023
morozov added a commit to morozov/dbal that referenced this issue Jun 16, 2023
See: sebastianbergmann/phpunit#5241

It covers a platform-specific use case in the base platform class,
which is pointless.
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80455
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80458
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80458
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/fluid that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80458
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/form that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80458
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80455
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/fluid that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80455
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/form that referenced this issue Aug 8, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow current best practices.

This changes tackles the classes where we can mock an interface
instead of an abstract class, allowing us to use `createMock` instead
of `getMockForAbstractClass`.

Resolves: #101609
Related: #101601
Releases: main, 12.4
Change-Id: I995f70d779a1bc6251bc4479c3dcb2ee548314f1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80455
Tested-by: core-ci <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Aug 10, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101630
Related: #101601
Releases: main
Change-Id: I27f526cb7ee4e1f2081c05befd3d70549dd0e2fd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80470
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: Anja Leichsenring <[email protected]>
Tested-by: Andreas Nedbal <[email protected]>
Reviewed-by: Lina Wolf <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Lina Wolf <[email protected]>
Reviewed-by: Anja Leichsenring <[email protected]>
Reviewed-by: Andreas Nedbal <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Aug 10, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101630
Related: #101601
Releases: main
Change-Id: I27f526cb7ee4e1f2081c05befd3d70549dd0e2fd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80470
Tested-by: Christian Kuhn <[email protected]>
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: Anja Leichsenring <[email protected]>
Tested-by: Andreas Nedbal <[email protected]>
Reviewed-by: Lina Wolf <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Lina Wolf <[email protected]>
Reviewed-by: Anja Leichsenring <[email protected]>
Reviewed-by: Andreas Nedbal <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Aug 16, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101666
Related: #101601
Releases: main
Change-Id: I0263ff7b0639d72ba1d4f30e3bee12276d364591
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80526
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Nikita Hovratov <[email protected]>
Tested-by: Nikita Hovratov <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Aug 16, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101666
Related: #101601
Releases: main
Change-Id: I0263ff7b0639d72ba1d4f30e3bee12276d364591
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80526
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Nikita Hovratov <[email protected]>
Tested-by: Nikita Hovratov <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101892
Related: #101601
Releases: main, 12.4
Change-Id: Id1977130c5e2a9bdc54fb92aea7dfc63ecf55724
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80940
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benni Mack <[email protected]>
Reviewed-by: Benni Mack <[email protected]>
Tested-by: core-ci <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101892
Related: #101601
Releases: main, 12.4
Change-Id: Id1977130c5e2a9bdc54fb92aea7dfc63ecf55724
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80952
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101892
Related: #101601
Releases: main, 12.4
Change-Id: Id1977130c5e2a9bdc54fb92aea7dfc63ecf55724
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80940
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benni Mack <[email protected]>
Reviewed-by: Benni Mack <[email protected]>
Tested-by: core-ci <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101892
Related: #101601
Releases: main, 12.4
Change-Id: Id1977130c5e2a9bdc54fb92aea7dfc63ecf55724
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80952
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101661
Related: #101601
Releases: main, 12.4
Change-Id: I3b9ba4511620aebf125ff207428adc5483d47154
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80944
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101630
Related: #101601
Releases: main, 12.4
Change-Id: I27f526cb7ee4e1f2081c05befd3d70549dd0e2fd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80945
Tested-by: core-ci <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101666
Related: #101601
Releases: main, 12.4
Change-Id: I0263ff7b0639d72ba1d4f30e3bee12276d364591
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80946
Tested-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101630
Related: #101601
Releases: main, 12.4
Change-Id: I27f526cb7ee4e1f2081c05befd3d70549dd0e2fd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80945
Tested-by: core-ci <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/form that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101661
Related: #101601
Releases: main, 12.4
Change-Id: I3b9ba4511620aebf125ff207428adc5483d47154
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80944
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this issue Sep 11, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

Resolves: #101666
Related: #101601
Releases: main, 12.4
Change-Id: I0263ff7b0639d72ba1d4f30e3bee12276d364591
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80946
Tested-by: Benjamin Franzke <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Oct 15, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

This change also cleans up the affected test to better match the
setup/execute/verify structure.

Resolves: #102117
Related: #101601
Releases: main, 12.4
Change-Id: Iaa6cee9893d41cb95e20cda80e03f044d52d82cd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81350
Reviewed-by: Stefan Bürk <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
Tested-by: core-ci <[email protected]>
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this issue Oct 15, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

This change also cleans up the affected test to better match the
setup/execute/verify structure.

Resolves: #102117
Related: #101601
Releases: main, 12.4
Change-Id: Iaa6cee9893d41cb95e20cda80e03f044d52d82cd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81374
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Stefan Bürk <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/form that referenced this issue Oct 15, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

This change also cleans up the affected test to better match the
setup/execute/verify structure.

Resolves: #102117
Related: #101601
Releases: main, 12.4
Change-Id: Iaa6cee9893d41cb95e20cda80e03f044d52d82cd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81350
Reviewed-by: Stefan Bürk <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
Tested-by: core-ci <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/form that referenced this issue Oct 15, 2023
`getMockForAbstractClass` has been (soft-)deprecated in PHPUnit 10.1:
sebastianbergmann/phpunit#5241

Hence, we should replace its usages to follow best practices and
avoid deprecation warnings later with PHPUnit 11.

We do this by creating dedicated fixture subclasses of the affected
abstract classes.

This change also cleans up the affected test to better match the
setup/execute/verify structure.

Resolves: #102117
Related: #101601
Releases: main, 12.4
Change-Id: Iaa6cee9893d41cb95e20cda80e03f044d52d82cd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81374
Reviewed-by: Christian Kuhn <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
Tested-by: Christian Kuhn <[email protected]>
Tested-by: core-ci <[email protected]>
Reviewed-by: Stefan Bürk <[email protected]>
cookieguru added a commit to MergePHP/mergephp-website that referenced this issue Apr 13, 2024
* Remove calls to getMockForAbstractClass(), see [1]
* Remove calls to PHPUnit method returnValueMap, see [2]
* Work around issue with deprecated test_suffix CSV, see [3]
* Do not explicitly add `-colors=always` since it is already added [4]

[1]: sebastianbergmann/phpunit#5241
[2]: sebastianbergmann/phpunit#5423
[3]: php-actions/phpunit#64
[4]: https://github.com/php-actions/phpunit/blob/c27e49b5fd8cd59d032b7b4441d2e15f23cf6519/phpunit-action.bash#L154
cookieguru added a commit to MergePHP/mergephp-website that referenced this issue Apr 13, 2024
* Remove calls to getMockForAbstractClass(), see [1]
* Remove calls to PHPUnit method returnValueMap, see [2]
* Work around issue with deprecated test_suffix CSV, see [3]
* Do not explicitly add `-colors=always` since it is already added [4]

[1]: sebastianbergmann/phpunit#5241
[2]: sebastianbergmann/phpunit#5423
[3]: php-actions/phpunit#64
[4]: https://github.com/php-actions/phpunit/blob/c27e49b5fd8cd59d032b7b4441d2e15f23cf6519/phpunit-action.bash#L154
xabbuh added a commit to symfony/symfony that referenced this issue May 15, 2024
…aubois)

This PR was merged into the 5.4 branch.

Discussion
----------

Remove calls to `getMockForAbstractClass()`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

This method is deprecated (see sebastianbergmann/phpunit#5241), and must be replaced where used.

Commits
-------

a1843f3 Remove calls to `getMockForAbstractClass()`
@klausi
Copy link

klausi commented Aug 2, 2024

Interesting! Unfortunately neither the docs nor the issue here mention a best practice what to use instead of getMockForAbstractClass().

We often need to test abstract classes outside our control (for example from 3rd party libraries), what is the best approach to do instead?

@klausi
Copy link

klausi commented Aug 2, 2024

To answer my own question: you need to specify now all unimplemented methods of the abstract class, which is a bit annoying but not too bad.

Simplified example from Drupal's graphql module:

abstract class SdlSchemaExtensionPluginBase implements SchemaExtensionPluginInterface {}

interface SchemaExtensionPluginInterface {
  public function registerResolvers(ResolverRegistryInterface $registry);
}

Previous test code:

$extensions['graphql_alterable_schema_test'] = $this->getMockBuilder(SdlSchemaExtensionPluginBase::class)
  ->disableOriginalConstructor()
  ->onlyMethods(['getBaseDefinition', 'getExtensionDefinition'])
  ->getMockForAbstractClass();

New test code:

$extensions['graphql_alterable_schema_test'] = $this->getMockBuilder(SdlSchemaExtensionPluginBase::class)
  ->disableOriginalConstructor()
  ->onlyMethods(['getBaseDefinition', 'getExtensionDefinition', 'registerResolvers'])
  ->getMock();

Which gets me thinking now that we could probably mock the interface instead?

@klausi
Copy link

klausi commented Aug 2, 2024

Welp, I was right! Because we were smart and built on interfaces anyway (yay interfaces!) we can just mock the interface and the test keeps working:

$extensions['graphql_alterable_schema_test'] = $this->getMockBuilder(SchemaExtensionPluginInterface::class)
  ->getMock();

@klausi
Copy link

klausi commented Aug 2, 2024

So the best practice is to replace getMockForAbstractClass() with getMock(). 3 examples: drupal-graphql/graphql#1415

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

2 participants