I'm unit testing my php code using PhpUnit. Many developers say that is bad practice to put expectation on query method, that is method that only returns something and does not change state of system ( CQS ). Expectations should be used only on command type methods. For Example:
$this->validatorMock->expects($this->once())
->method('isValid')
->with('foo')
->willReturn(true)
Is considered a bad practice because it couple test to tightly to implementation. Correct way:
$this->validatorMock->method('isValid')
->willReturn(true);
However what if I'm testing class that depends on results of validation of some sort eg.:
if ($this->validator->isValid($data) {
// some logic
} else {
// some other logic
}
Should I put expectation on that method or not? It's a query type method but it is important to test that it was called. I want to know that data was validated.
Of course isValid method itself is tested in seperate testClass ( one that tests validator class )