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

Using willReturnOnConsecutiveCalls() together with throwException() #5469

Closed
Ugoku opened this issue Aug 14, 2023 · 3 comments
Closed

Using willReturnOnConsecutiveCalls() together with throwException() #5469

Ugoku opened this issue Aug 14, 2023 · 3 comments
Labels
feature/test-doubles Test Stubs and Mock Objects type/deprecation Something will be/is deprecated

Comments

@Ugoku
Copy link

Ugoku commented Aug 14, 2023

Q A
PHPUnit version 10.3.1
PHP version 8.2.8
Installation Method Composer

Summary

PHPUnit 10.3 deprecated willReturnOnConsecutiveCalls and throwException methods. I am unsure how to convert this piece of code:

$this->someclass
    ->method('save')
    ->willReturnOnConsecutiveCalls(
        $this->throwException(new SomeException('foo')),
        true,
    );

I can replace willReturnOnConsecutiveCalls with willReturn, but how can I declare that it should throw an exception the first time, and true the second time?

@sebastianbergmann sebastianbergmann added feature/test-doubles Test Stubs and Mock Objects type/deprecation Something will be/is deprecated labels Aug 14, 2023
@sebastianbergmann sebastianbergmann added this to the PHPUnit 11.0 milestone Aug 14, 2023
@sebastianbergmann sebastianbergmann changed the title 10.3 deprecation question Using willReturnOnConsecutiveCalls() together with throwException() Aug 14, 2023
@sebastianbergmann
Copy link
Owner

Please note that the functionality in question is currently "only" soft-deprecated. Your tests continue to work in exactly the same way as before and no deprecation is displayed by PHPUnit. This is also the reason why the deprecation is not mentioned in the ChangeLog.

The actual deprecation for the functionality in question is tracked in #5423 and #5425.

Thank you for bringing this to my attention, I was not aware that this works:

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

interface I
{
    public function m(): bool;
}

final class Test extends TestCase
{
    public function testOne(): void
    {
        $i = $this->createStub(I::class);
        
        $i->method('m')
          ->willReturnOnConsecutiveCalls(
            false,
            true,
            $this->throwException(new Exception('message'))
          );

        $this->assertFalse($i->m());
        $this->assertTrue($i->m());

        $this->expectException(Exception::class);

        $this->assertTrue($i->m());
    }
}

In the example shown above, the test stub $i is configured so that it

  • returns false when it is invoked the first time
  • returns true when it is invoked the second time
  • throws an exception of type Exception when it is invoked the third time

As of right now, I have no alternative to suggest to achieve the same result once #5424 and #5426 are implemented. I will need to investigate this further.

@Ugoku
Copy link
Author

Ugoku commented Aug 14, 2023

Your tests continue to work in exactly the same way as before and no deprecation is displayed by PHPUnit. This is also the reason why the deprecation is not mentioned in the ChangeLog.

No, you're right about that. It's our PHPStan configuration that warns about calling deprecated methods.

Thanks for the information. I will ignore the PHPStan message for now and will keep an eye out for future changes!

@sebastianbergmann
Copy link
Owner

The soft-deprecation of willReturnOnConsecutiveCalls() will be reverted in PHPUnit 10.3.2.

@sebastianbergmann sebastianbergmann removed this from the PHPUnit 11.0 milestone Aug 20, 2023
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