It's very useful to have a non-static return value, e.g. by passing in a function to returns():
$DB->returns('escape', create_function('$step, $method, $args', 'return addslashes($args[0]);'));
The patch to implement this is quite trivial, but would not allow to really return a function (which is a valid use case).
Therefore, a new method should get added for this, e.g. returnsByFunction.
However, here's the patch I have created for this:
diff --git a/mock_objects.php b/mock_objects.php
index cc04836..4de8f2f 100644
--- a/mock_objects.php
+++ b/mock_objects.php
@@ -534,7 +534,9 @@ class SimpleReturn {
* @return mixed Whatever was stashed.
* @access public
*/
- function act() {
+ function act($step, $method, $args) {
+ if (is_callable($this->value))
+ return call_user_func($this->value, $step, $method, $args);
return $this->value;
}
}
Well, act() needs those params, too, of course:
the correct patch:
diff --git a/mock_objects.php b/mock_objects.php
index cc04836..4de8f2f 100644
--- a/mock_objects.php
+++ b/mock_objects.php
@@ -473,14 +473,14 @@ class SimpleCallSchedule {
if ($this->at[$method][$step]->isMatch($args)) {
$action = $this->at[$method][$step]->findFirstAction($args);
if (isset($action)) {
- return $action->act();
+ return $action->act($step, $method, $args);
}
}
}
if (isset($this->always[$method])) {
$action = $this->always[$method]->findFirstAction($args);
if (isset($action)) {
- return $action->act();
+ return $action->act($step, $method, $args);
}
}
$null = null;
@@ -534,7 +534,9 @@ class SimpleReturn {
* @return mixed Whatever was stashed.
* @access public
*/
- function act() {
+ function act($step, $method, $args) {
+ if (is_callable($this->value))
+ return call_user_func($this->value, $step, $method, $args);
return $this->value;
}
}