janmarek/mockista — pull request #23 by MartinMystikJonas (2014-05-23 08:28:42+00:00)
Title: Custom ArgsMatcher

New version of pull request #9 rebased to current master.

---

Default object args matching based on spl_object_hash is not practical in case of value object, which equality is based only on properties values. Custom argsMatchers can replace default argsMatcher to support different matching policy.

Example of CustomArgsMatcher we currently use:
```PHP
class ArgsMatcher extends \Mockista\ArgsMatcher {

protected function hashArg($arg) {
if($arg instanceof Entity) {
// entites are matched based on values of their properties
return md5(print_r($arg->getProperties()->toArray(), true));
} elseif($arg instanceof \DateTime) {
//two DateTime objects are equivalent it they represent same time
return md5(print_r($arg->format("Y-m-d_H:i:se")), true);
} elseif($arg instanceof \Nette\ArrayHash) {
// ArrayHash wrapper of array is matched based on it's content
return parent::hashArg((array)$arg);
} else {
// fallback for all other types
return parent::hashArg($arg);
}
}

}
```

Usage:
```PHP
$registry = new \Mockista\Registry(new CustomArgsMatcher());
// all mocks created by registry will use CustomArgsMatcher
```
```PHP
$mock = $registry->create();
$mock->setArgsMatcher(new CustomArgsMatcher());
// change argsMatcher only for specific mock
```
```PHP
$obj1 = new ValueObject(10);
$obj2 = new ValueObject(10);
$mock->expects('method')->with($obj1)->once();
$mock->method($obj2);
$mock->assertExpectations();
// assert is valid if CustomArgsMatcher compare ValueObject only by value
```