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

createStub(), createConfiguredStub(), createPartialStub() #3120

Closed
sebastianbergmann opened this issue May 12, 2018 · 2 comments · Fixed by #3810
Closed

createStub(), createConfiguredStub(), createPartialStub() #3120

sebastianbergmann opened this issue May 12, 2018 · 2 comments · Fixed by #3810
Assignees
Labels
event/code-sprint/2019-09 PHPUnit Code Sprint: September 2019 feature/test-doubles Stubs and Mock Objects type/enhancement A new idea that should be implemented

Comments

@sebastianbergmann
Copy link
Owner

sebastianbergmann commented May 12, 2018

PHPUnit's built-in test doubles functionality pre-dates the de-facto standard classification for test doubles established by Gerard Meszaros in his book "xUnit Test Patterns".

  • A stub is used to "replace a real component on which the [system under test] depends so that the test has a control point for the indirect inputs of the [system under test]. This allows the test to force the [system under test] down paths it might not otherwise execute."

  • A mock is used "as an observation point that is used to verify the indirect outputs of the [system under test] as it is exercised. Typically, the [mock] also includes the functionality of a [stub] in that it must return values to the [system under test] if it hasn't already failed the tests but the emphasis is on the verification of the indirect outputs. Therefore, a [mock] is lot more than just a [stub] plus assertions; it is used a fundamentally different way."

The API of PHPUnit's built-in test doubles functionality for creating test doubles — originally getMock(), then getMockBuilder(), and now createMock() etc. — creates a stub. If, and only if, the expects() method is called on this stub to set up expectations then that stub becomes a mock.

The fact that the test double returned by createMock() is initially "just" a stub can be confusing, especially for developers new to PHPUnit in particular and/or unit testing and test doubles in general.

It should be explored whether the addition of createStub(), createConfiguredStub(), createPartialStub() methods is feasible. These methods should create test double objects that do not have the expects() method and therefore are stubs that cannot become mocks.

@Ocramius
Copy link
Sponsor Contributor

Ocramius commented Sep 6, 2019

Mine.

@Ocramius
Copy link
Sponsor Contributor

Ocramius commented Sep 6, 2019

Note: #3810 does only implement TestCase#createStub().

I will create separate patches for createConfiguredStub() and createPartialStub() after that one has been discussed in detail.

sebastianbergmann pushed a commit that referenced this issue Sep 6, 2019
… false positive

The method `InvocationMocker#will()` is LSP-compliant with the parent
types, but a bug in psalm reports it as a false positive.

Ref: vimeo/psalm#2102
sebastianbergmann pushed a commit that referenced this issue Sep 6, 2019
I was too eager in using an appropriate type declaration on `InvocationMocker`,
but that has to wait for newer PHP releases.
sebastianbergmann pushed a commit that referenced this issue Sep 6, 2019
Once we have PHP 7.4, we can move these type declarations into the interface
signature again, but for now, the existing inheritance leads to a crash
on PHP < 7.4.
sebastianbergmann pushed a commit that referenced this issue Sep 6, 2019
`InvocationStubber` methods cannot have return type declarations until
then.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event/code-sprint/2019-09 PHPUnit Code Sprint: September 2019 feature/test-doubles Stubs and Mock Objects type/enhancement A new idea that should be implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants