sebastianbergmann/phpunit-mock-objects — pull request #138 by patStar (2013-09-12 13:10:36+00:00)
Title: Access arguments of invoked methods for further testing.

Hy!

This Pullrequest adds the possibility to access the arguments used for the invocation of a method. This allows to test parameters which are unstable or may change during the test run. One Example is a timestamp verification.

```php
class Test extends \PHPUnit_Framework_TestCase {

public function test()
{
$foo = new Foo();
$foo->bar = $this->getMock('Bar',array('handle'));

$with = $foo->bar->expects($this->any())->method('handle')->withAnyParameters()->will($this->returnValue(true));

$result = $foo->handle(123);

$this->assertEquals("Timestamp_".time(),$result); // This will fail sometimes!

$actualTimeWithinMethod = $with->getMatcher()->parametersMatcher->invocation->parameters[1];
$this->assertEquals("Timestamp_".$actualTimeWithinMethod,$result); // This will succeed, always!

}
}

class Foo{

public $bar;

public function handle($val){

$date = time();

// just simulating doing something time intensive
for($i = 0; $i<10000; $i++){
for($j = 0; $j<1000; $j++){
// ...
}
}

$this->bar->handle($val, $date);
return "Timestamp_".$date;
}
}
```

There are other use cases, where you cannot determine the parameters your mock is called with but you may need them later on. For example in game engines, where random numbers determine the output of a method and the output verification depends on the number randomly generated within the depth of the class.

In some cases, these tests hint to a bad architecture or simply wrong usage of method scopes. Nevertheless, old and bad code should be covered with unit tests before it can be refactored without bad dreams. So I think this modification is a usefull feature.

Thank you.