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

Prevent runtime type error due to wrong return value configuration #3122

Closed
sebastianbergmann opened this issue May 12, 2018 · 0 comments · Fixed by #3673
Closed

Prevent runtime type error due to wrong return value configuration #3122

sebastianbergmann opened this issue May 12, 2018 · 0 comments · Fixed by #3673
Labels
feature/test-doubles Test Stubs and Mock Objects type/enhancement A new idea that should be implemented

Comments

@sebastianbergmann
Copy link
Owner

willReturn() should raise an error when a return value is configured that does not match the stubbed method's return value.

<?php declare(strict_types=1);

class C
{
    public function m(): int
    {
        return 1;
    }
}

final class Test extends PHPUnit\Framework\TestCase
{
    public function testOne(): void
    {
        $o = $this->createMock(C::class);
        
        $o->method('m')->willReturn('not an integer');
        
        $o->m();
    }
}

Actual: TypeError when stubbed method is invoked

PHPUnit 7.1-gfafd3c555 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 68 ms, Memory: 4.00MB

There was 1 error:

1) Test::testOne
TypeError: Return value of Mock_C_06141907::m() must be of the type integer, string returned

/home/sb/Test.php:19

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
@sebastianbergmann sebastianbergmann added type/enhancement A new idea that should be implemented feature/test-doubles Test Stubs and Mock Objects labels May 12, 2018
MichelHartmann added a commit to MichelHartmann/phpunit that referenced this issue Apr 27, 2019
PHPUnit Code Sprint with bschulze
MichelHartmann added a commit to MichelHartmann/phpunit that referenced this issue Apr 28, 2019
PHPUnit Code Sprint with bschulze
MichelHartmann added a commit to MichelHartmann/phpunit that referenced this issue Apr 30, 2019
PHPUnit Code Sprint with bschulze
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/enhancement A new idea that should be implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant