-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Mocking method with object return type throws warning #3706
Comments
I believe this PR should fix the problem |
I can still reproduce the warning in the most recent version of PHPUnit (3e9a165) using the example above.
|
I was able to reproduce the issue with <?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
interface I
{
public function m();
}
final class Test extends TestCase
{
public function testOne(): void
{
$i = $this->createMock(I::class);
$i->method('m')->willReturn(new stdClass);
$this->assertInstanceOf(stdClass::class, $i->m());
}
} |
sebastianbergmann/type@26a5e76 (part of version 1.1.2 of the type package) fixes #3706 (comment) for me. |
@sebastianbergmann it's still happening to me when a mock of an interface returns another mock... interface FeatureRepositoryInterface
{
public function findOneByNameAndContext(string $name, Context $context): ?FeatureInterface;
} $featureMock = $this->createMock(FeatureInterface::class)
->expects($this->once())
->method('isActive')
->willReturn($expectedIsActive);
$this->createMock(FeatureRepositoryInterface::class)
->expects($this->once())
->method('findOneByNameAndContext')
->with($featureName, $context)
->willReturn($featureMock);
Version of phpunit: 8.2.3 |
@davidkmenta Your example was incomplete. I have completed it like so: <?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
interface FeatureInterface
{
public function isActive(): bool;
}
interface FeatureRepositoryInterface
{
public function findOneByNameAndContext(string $name, Context $context): ?FeatureInterface;
}
final class Test extends TestCase
{
public function testOne(): void
{
$featureMock = $this->createMock(FeatureInterface::class)
->expects($this->once())
->method('isActive')
->willReturn(true);
$x = $this->createMock(FeatureRepositoryInterface::class)
->expects($this->once())
->method('findOneByNameAndContext')
->willReturn($featureMock);
}
}
The warning shown above is triggered because the <?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
interface FeatureInterface
{
public function isActive(): bool;
}
interface FeatureRepositoryInterface
{
public function findOneByNameAndContext(string $name, Context $context): ?FeatureInterface;
}
final class Test extends TestCase
{
public function testOne(): void
{
$featureMock = $this->createMock(FeatureInterface::class);
$featureMock->expects($this->once())
->method('isActive')
->willReturn(true);
$x = $this->createMock(FeatureRepositoryInterface::class);
$x->expects($this->once())
->method('findOneByNameAndContext')
->willReturn($featureMock);
}
}
Before you were passing the result of |
@sebastianbergmann you're right! I completely forgot about that behavior. Thank you! |
…hod type, see sebastianbergmann/phpunit#3706) Remove unsupported tap coverage logger (see sebastianbergmann/phpunit#2683)
Mocking a method with an
object
return type throws a warning, egMethod resolve may not return value of type object
. This was not happening in 8.1. For example, let's say I have this interface:And let's say I have the following (contrived) test:
I would expect this to pass, but instead I get a warning
Method resolve may not return value of type object
. I'm guessing it has something to do with this commit.composer info | sort
:The text was updated successfully, but these errors were encountered: