Skip to content

Commit

Permalink
mockednode will now mimic the node behaviour, that will stop executin…
Browse files Browse the repository at this point in the history
…g client methods when the stopWhen predicate returns true
  • Loading branch information
marcelog committed Mar 30, 2012
1 parent 82dbcc1 commit b067aee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/mg/PAGI/Node/MockedNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ protected function callClientMethods($methods, $stopWhen = null)
{
$client = $this->getClient();
$logger = $client->getLogger();
$result = null;
foreach ($methods as $callInfo) {
foreach ($callInfo as $name => $arguments) {
switch($name)
Expand All @@ -324,23 +325,47 @@ protected function callClientMethods($methods, $stopWhen = null)
default:
break;
}
$result = parent::callClientMethod($name, $arguments);
if ($stopWhen !== null) {
if ($stopWhen($result)) {
return $result;
}
}
}
}
return parent::callClientMethods($methods, $stopWhen);
return $result;
}

/**
* Execute a callback before invoking the real callback for valid input.
*
* @param \Closure $callback
*
* @return \PAGI\Node\MockedNode
*/
public function doBeforeValidInput(\Closure $callback)
{
$this->validInputCallback = $callback;
return $this;
}

/**
* Execute a callback before invoking the real callback for failed input.
*
* @param \Closure $callback
*
* @return \PAGI\Node\MockedNode
*/
public function doBeforeFailedInput(\Closure $callback)
{
$this->failedInputCallback = $callback;
return $this;
}

/**
* (non-PHPdoc)
* @see PAGI\Node.Node::beforeOnValidInput()
*/
protected function beforeOnValidInput()
{
if ($this->validInputCallback !== null) {
Expand All @@ -349,6 +374,10 @@ protected function beforeOnValidInput()
}
}

/**
* (non-PHPdoc)
* @see PAGI\Node.Node::beforeOnInputFailed()
*/
protected function beforeOnInputFailed()
{
if ($this->failedInputCallback !== null) {
Expand Down
19 changes: 15 additions & 4 deletions src/mg/PAGI/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,20 @@ public function isComplete()
return $this->state == self::STATE_COMPLETE;
}

/**
* Call a specific method on a client.
*
* @param string $name
* @param string[] $arguments
*
* @return IResult
*/
protected function callClientMethod($name, array $arguments = array())
{
$this->logDebug("$name(" . implode(",", $arguments) . ")");
return call_user_func_array(array($this->_client, $name), $arguments);
}

/**
* Calls methods in the PAGI client.
*
Expand All @@ -888,10 +902,7 @@ protected function callClientMethods($methods, $stopWhen = null)
$result = null;
foreach ($methods as $callInfo) {
foreach ($callInfo as $name => $arguments) {
$this->logDebug("$name(" . implode(",", $arguments) . ")");
$result = call_user_func_array(
array($this->_client, $name), $arguments
);
$result = $this->callClientMethod($name, $arguments);
if ($stopWhen !== null) {
if ($stopWhen($result)) {
return $result;
Expand Down

0 comments on commit b067aee

Please sign in to comment.