-
-
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 same method with different "with" and different "return" throw an exception #4398
Comments
Thank you for your report. Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting. Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue. |
@sebastianbergmann , I have sucessfully reproduced the issue ( // doctrine entities
class Order {}
class Customer {}
// doctrine repositories
class OrderRepository {}
class CustomerRepository {}
// Test Subject
class Order {
public function __construct(EntityManagerInterface $em) {
$order = $this->$em->getRepository(OrderRepository::class)->findBy(['id' => 1]);
}
}
class OrderTest extends TestCase
{
protected function setUp(): void {
$this->em = $this->createMock(EntityManagerInterface::class);
$this->customerRepositoryMock = $this->createMock(CustomerRepository::class);
$this->orderRepositoryMock = $this->createMock(OrderRepository::class);
$this->em->method('getRepository')->with(Customer::class)
->willReturn($this->customerRepositoryMock);
$this->em->method('getRepository')->with(Order::class)
->willReturn($this->orderRepositoryMock);
}
}
// Then try test a class that uses these mock and try to get anything from these repositories,
// it will throw.
// I hope I could make it reproducible for you. |
Lol. It's been 2y+, somehow I've missed this notification. It's irrelevant for me atm. 💪👍 |
Sorry, but something that uses Doctrine's entity manager is neither minimal nor self-contained. |
Latest
Isn't the: $this->mock->method('get')->with(C1::class)
->willReturn(1);
$this->mock->method('get')->with(C2::class)
->willReturn(2); incorrect way of mocking method and should be replaced with: $this->mock->method('get')->willReturnCallback(fn (string $x) => match (true) {
$x === C1::class => 1,
$x === C2::class => 2,
default => throw new LogicException(),
}); I wonder? |
Summary
Mocking same method with different "with" and different "return"
Current behavior
How to reproduce
Expected behavior
If "matcher" did not throw an exception on the second "match" (invoke), we should not throw an exception
The text was updated successfully, but these errors were encountered: