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 methods for creating return stub configuration objects #5423

Closed
sebastianbergmann opened this issue Jun 24, 2023 · 3 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 Jun 24, 2023

To reduce complexity inside PHPUnit's test double functionality, these methods will be deprecated and then removed:

  • TestCase::returnValue() (use $double->willReturn() instead of $double->will($this->returnValue()))
  • TestCase::onConsecutiveCalls() (use $double->willReturn() instead of $double->will($this->onConsecutiveCalls()))
  • TestCase::returnValueMap() (use $double->willReturnMap() instead of $double->will($this->returnValueMap()))
  • TestCase::returnArgument() (use $double->willReturnArgument() instead of $double->will($this->returnArgument()))
  • TestCase::returnSelf() (use $double->willReturnSelf() instead of $double->will($this->returnSelf()))
  • TestCase::returnCallback() (use $double->willReturnCallback() instead of $double->will($this->returnCallback()))
  • TestCase::throwException() (use $double->willThrowException() instead of $double->will($this->throwException()))

Timeline

  • soft deprecation in PHPUnit 10.3 (add @deprecated annotation to the method declaration)
  • deprecation in PHPUnit 11 (using the method will trigger a deprecation)
  • removal in PHPUnit 12
@InvisibleSmiley
Copy link

Two questions:

  1. Why is this not mentioned in the changelog for 10.3? With tools like phpstan-deprecation-rules even "soft deprecations" as you call them cause some trouble and it would be nice to find a pointer to this issue there.
  2. How exactly is willReturn a replacement for onConsecutiveCalls? Please describe the migration path (maybe give an example).

@sebastianbergmann
Copy link
Owner Author

  1. Why is this not mentioned in the changelog for 10.3?

Because I forgot to, my bad. The deprecations made in PHPUnit 10.3.0 are now documented: https://github.com/sebastianbergmann/phpunit/blob/10.3.2/ChangeLog-10.3.md#deprecated-1

@sebastianbergmann
Copy link
Owner Author

  1. How exactly is willReturn a replacement for onConsecutiveCalls?

I hope this example from PHPUnit's own test suite is enough:

final public function testMethodCanBeConfiguredToReturnDifferentValuesOnConsecutiveCalls(): void
{
    $double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);

    $double->method('doSomething')->willReturn(false, true, false, true);

    $this->assertFalse($double->doSomething());
    $this->assertTrue($double->doSomething());
    $this->assertFalse($double->doSomething());
    $this->assertTrue($double->doSomething());
}

github-merge-queue bot pushed a commit to Kitware/CDash that referenced this issue Mar 17, 2024
This PR is meant to serve as a first step for addressing changes that
will need to be made before #1866
can be merged. The PHPUnit function `returnCallback()` is
[deprecated](sebastianbergmann/phpunit#5423),
so our usage of it was switched to its shorthand syntax, as recommended
by the docs.
github-merge-queue bot pushed a commit to Kitware/CDash that referenced this issue Mar 17, 2024
This PR is meant to serve as a first step for addressing changes that
will need to be made before #1866
can be merged. The PHPUnit function `returnCallback()` is
[deprecated](sebastianbergmann/phpunit#5423),
so our usage of it was switched to its shorthand syntax, as recommended
by the docs.
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
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