PHPunit: How to mock a method that has a parameter AND a returned value

I know that it's an old post, but it's on the top in searching for PHPUnit warning Method name matcher is already defined, cannot redefine, so I'll answer too.

There is other reason for such warning message. If you describe a mock behaviour in chaining method like this:

$research = $this->createMock(Research::class);
$research->expects($this->any())
    ->method('getId')
    ->willReturn(1)
    ->method('getAgent')
    ->willReturn(1);

You will get warning Method name matcher is already defined, cannot redefine. Just split it in separate statements and the warning will go away (tested on PHPUnit 7.5 and 8.3).

$research = $this->createMock(Research::class);

$research->expects($this->any())
    ->method('getId')
    ->willReturn(1);

$research->expects($this->any())
    ->method('getAgent')
    ->willReturn(1);

You need to use will instead of with for returnValue and friends.

$user->expects($this->once())
     ->method('removeItem')
     ->with($item)  // equalTo() is the default; save some keystrokes
     ->will($this->returnValue(true));    // <-- will instead of with
$this->assertTrue($hoard->removeItemFromUser($item, $user));