Reputation: 5567
Coming from a background in Google Mock, I'm surprised this doesn't work, unless I'm doing it wrong.
I just want to ensure that a method is never called with a specific class type but may be called for other class types. So here is the code I have that explains what I want:
$this->entityManagerMock
->expects($this->any())
->method('persist');
$this->entityManagerMock
->expects($this->never())
->method('persist')
->with($this->isInstanceOf('MySpecificClass'));
Now I get a message similar to this:
Doctrine\ORM\EntityManager::persist(DifferentClassType Object (...)) was not expected to be called.
When I'd expect that first expectation to handle it.
I tried this but the result was the same:
$this->entityManagerMock
->expects($this->any())
->method('persist')
->with($this->anything());
$this->entityManagerMock
->expects($this->never())
->method('persist')
->with($this->isInstanceOf('MySpecificClass'));
This is my first time using mocks in PHPUnit but it seems to me that with
is broken and/or not useful. I know that most web developers these days use TDD so there has to be a better way to do this.
Upvotes: 4
Views: 710
Reputation: 36532
As a work-around you can use returnCallback
:
$this->entityManagerMock
->expects($this->any())
->method('persist')
->will($this->returnCallback(function ($object) {
self::assertNotInstanceOf('MySpecificClass', $object);
}));
Upvotes: 2