Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called Query(string $query), that method takes an SQL query string on input. Can I create mock for this class (DB) and set different return values for different Query method calls that depends on input query string?
Is there any way to define different mock-expects for different input arguments? For example,
Share
The PHPUnit Mocking library (by default) determines whether an expectation matches based solely on the matcher passed to
expectsparameter and the constraint passed tomethod. Because of this, twoexpectcalls that only differ in the arguments passed towithwill fail because both will match but only one will verify as having the expected behavior. See the reproduction case after the actual working example.For you problem you need to use
->at()or->will($this->returnCallback(as outlined inanother question on the subject.Example:
Reproduces:
Reproduce why two ->with() calls don’t work:
Results in