You can, but it's best to implement with PHPUnits the mocking API that I could come up with, it still looks pretty creepy. A.
Another way to resolve this issue is a slightly more readable way to create your own subclass of Trampoline and implement it there.
But for the call:
Assuming this class:
<?php class FancyMocking { function doThing($value) { } }
and that we have $x calls, and one of them should have $value > 200 :
<?php class FancyMockingTest extends PHPUnit_Framework_TestCase { public function testAtLeastOfMy200CallsShouldHaveAValueGreaterThan500() { $maxInvocations = 200; $mock = $this->getMock('FancyMocking'); $mock->expects($this->exactly($maxInvocations)) ->method('doThing') ->with($this->callback(function ($value) use ($maxInvocations) { static $invocationCount = 0; static $maxValue = 0; $maxValue = max($value, $maxValue); if (++$invocationCount == $maxInvocations * 2) { $this->assertGreaterThan(200, $maxValue, 'in 500 tries the max value didn\'t to over 200'); } return true; })) ->will($this->returnCallback(function ($value) { return $value >= 200; })); for($i = $maxInvocations - 2; $i; --$i) { $mock->doThing(50); } var_dump($mock->doThing(250)); var_dump($mock->doThing(50)); } }
This will give:
PHPUnit 3.7.9 by Sebastian Bergmann. .bool(true) bool(false) Time: 0 seconds, Memory: 2.75Mb OK (1 test, 2 assertions)
A call value with 250 returns true, and the whole test case works.
If this fails:
To do this, we will var_dump($mock->doThing(250)); on var_dump($mock->doThing(70)); and run it again:
PHPUnit 3.7.9 by Sebastian Bergmann. Fbool(false) Time: 0 seconds, Memory: 2.75Mb There was 1 failure: 1) FancyMockingTest::testAtLeastOfMy200CallsShouldHaveAValueGreaterThan500 Expectation failed for method name is equal to <string:doThing> when invoked 200 time(s) in 500 tries the max value didn't to over 200 Failed asserting that 70 is greater than 200. .../FancyMockingTest.php:29 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
A solution that does not use the PHPUnits API will be something like
class FancyMockingFakeImplementation extends FancyMocking , using this instead of the layout and writing custom logic.