Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Strange type casting behavior when mocking methods with a return type #332

Closed
guillemcanal opened this issue Oct 11, 2016 · 2 comments
Closed
Labels

Comments

@guillemcanal
Copy link

guillemcanal commented Oct 11, 2016

I'm facing a strange behavior when trying to mock a method that provide a return type which is an integer.

When you provide willReturn() with a boolean or float, The mocked object will type cast the given value into an integer silently:

PHP v7.0.10
PHPUnit v5.5.5

class Foo
{
    public function getAnInteger(): int
    {
        return 666;
    }
}

class SomeTest extends \PHPUnit\Framework\TestCase
{
    /** @test */
    public function itTypeCastABooleanIntoAnIntegerWhichIsWeird()
    {
        $foo = $this->getMockBuilder(Foo::class)->getMock();
        $foo->method('getAnInteger')->willReturn(false);

        self::assertThat($foo->getAnInteger(), self::identicalTo(0));
    }

    /** @test */
    public function itTypeCastAFloatIntoAnIntegerWhichIsWeird()
    {
        $foo = $this->getMockBuilder(Foo::class)->getMock();
        $foo->method('getAnInteger')->willReturn(666.666);

        self::assertThat($foo->getAnInteger(), self::identicalTo(666));
    }
}

Is that desired ?

@stale
Copy link

stale bot commented Apr 18, 2018

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 18, 2018
@stale
Copy link

stale bot commented Apr 25, 2018

This issue has been automatically closed because it has not had activity since it was marked as stale. Thank you for your contributions.

@stale stale bot closed this as completed Apr 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant