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

Return value generation interferes with proxying to original method #3607

Closed
sebastianbergmann opened this issue Apr 19, 2019 · 1 comment
Closed
Assignees
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken

Comments

@sebastianbergmann
Copy link
Owner

<?php declare(strict_types=1);
final class Foo
{
    private $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function value()
    {
        return $this->value;
    }
}

class Bar
{
    public function doSomething(): Foo
    {
        return new Foo('value');
    }
}

use PHPUnit\Framework\TestCase;

final class Test extends TestCase
{
    public function testOne(): void
    {
        $proxy = $this->createTestProxy(Bar::class);

        $proxy->expects($this->once())
              ->method('doSomething');

        $this->assertSame(
            'value',
            $proxy->doSomething()->value()
        );
    }
}
$ phpunit Test    
PHPUnit 7.5.8 by Sebastian Bergmann and contributors.

W                                                                   1 / 1 (100%)

Time: 52 ms, Memory: 4.00 MB

There was 1 warning:

1) Test::testOne
Class "Foo" is declared "final" and cannot be mocked.

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.
@sebastianbergmann sebastianbergmann added type/bug Something is broken feature/test-doubles Stubs and Mock Objects labels Apr 19, 2019
@sebastianbergmann sebastianbergmann self-assigned this Apr 19, 2019
@sebastianbergmann
Copy link
Owner Author

Invocation::generateReturnValue() is wrongly called to generate a default return value for : Foo. This does not work as Foo is final. If Foo were not final the call to Invocation::generateReturnValue() works but would still be superfluous as we are proxying and use the return value of the original method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

1 participant