-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
TestCase::getMockForAbstractClass()
See: sebastianbergmann/phpunit#5241 It covers a platform-specific use case in the base platform class, which is pointless.
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
`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]>
* 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
* 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
…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()`
Interesting! Unfortunately neither the docs nor the issue here mention a best practice what to use instead of We often need to test abstract classes outside our control (for example from 3rd party libraries), what is the best approach to do instead? |
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? |
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(); |
So the best practice is to replace |
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
, andstatic
methods cannot be doubled; they retain their original behavior except for static methods which will be replaced by a method throwing an exceptionenum
) arefinal
classes and therefore cannot be doubledreadonly
classes cannot be extended by classes that are notreadonly
and therefore cannot be doubledNot 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()
andcreateMock()
methods for creating test stubs and mock objects with best practice defaults. Furthermore, alternatives such asgetMockForAbstractClass()
exist, but not all of them offer the same clear separation between test double and mock object. For instance, no method namedgetStubForAbstractClass()
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:@deprecated
annotation to the method declaration)The text was updated successfully, but these errors were encountered: