-
-
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::getMockForTrait()
#5243
Labels
Milestone
Comments
sebastianbergmann
added
type/backward-compatibility
Something will be/is intentionally broken
feature/test-doubles
Test Stubs and Mock Objects
labels
Feb 23, 2023
sebastianbergmann
added a commit
that referenced
this issue
Feb 24, 2023
sebastianbergmann
added a commit
to sebastianbergmann/phpunit-documentation-english
that referenced
this issue
Feb 24, 2023
sebastianbergmann
changed the title
Deprecate TestCase::getMockForTrait()
Deprecate Mar 6, 2023
TestCase::getMockForTrait()
sebastianbergmann
added
type/deprecation
Something will be/is deprecated
and removed
type/backward-compatibility
Something will be/is intentionally broken
labels
Apr 11, 2023
sebastianbergmann
added a commit
that referenced
this issue
Aug 18, 2023
To convert existing tests without requiring a redesign of your code, adding a class that implements the trait and mock that seems to work. Before: namespace MyProject\Tests;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase {
public function test() {
$mock = $this->getMockForTrait('\MyProject\MyTrait');
}
} After: namespace MyProject\Tests;
use MyProject\MyTrait;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase {
public function test() {
$mock = $this->createMock(MyTraitMock::class);
}
}
/**
* Mock for MyTrait.
*/
class MyTraitMock {
use MyTrait;
} |
Or a bit more dynamic with the help of an anonymous class: /**
* Create an anonymous class for the provided trait.
*
* @param trait-string $traitName Name of the trait to create a mock for.
*/
public function createMockForTrait(string $traitName)
{
return eval('return new class { use ' . $traitName . '; };');
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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()
andcreateMock()
methods for creating test stubs and mock objects with best practice defaults. Furthermore, alternatives such asgetMockForTrait()
exist, but not all of them offer the same clear separation between test double and mock object. For instance, no method namedgetStubForTrait()
exists. Such an inconsistency can lead to confusion.The
getMockForTrait()
method returns a mock object for an otherwise empty class that uses a specified trait. All abstract methods of the given trait are mocked. This allows for testing the concrete methods of a trait.Having to use
getMockForTrait()
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::getMockForTrait()
will be deprecated and then removed:@deprecated
annotation to the method declaration)The text was updated successfully, but these errors were encountered: